Skip to content

Commit 3252a03

Browse files
committed
Merge pull request #118 from az7arul/startsWith-endsWith-multiArg
Added multiple argument support for startsWith and endsWith methods
2 parents 48096b8 + 0bc4cee commit 3252a03

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/string.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
169169
return new this.constructor(s);
170170
},
171171

172-
endsWith: function(suffix) {
173-
var l = this.s.length - suffix.length;
174-
return l >= 0 && this.s.indexOf(suffix, l) === l;
172+
endsWith: function() {
173+
var suffixes = Array.prototype.slice.call(arguments, 0);
174+
for (var i = 0; i < suffixes.length; ++i) {
175+
var l = this.s.length - suffixes[i].length;
176+
if (l >= 0 && this.s.indexOf(suffixes[i], l) === l) return true;
177+
}
178+
return false;
175179
},
176180

177181
escapeHTML: function() { //from underscore.string
@@ -380,8 +384,12 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
380384
return new this.constructor(sl);
381385
},
382386

383-
startsWith: function(prefix) {
384-
return this.s.lastIndexOf(prefix, 0) === 0;
387+
startsWith: function() {
388+
var prefixes = Array.prototype.slice.call(arguments, 0);
389+
for (var i = 0; i < prefixes.length; ++i) {
390+
if (this.s.lastIndexOf(prefixes[i], 0) === 0) return true;
391+
}
392+
return false;
385393
},
386394

387395
stripPunctuation: function() {

test/string.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,16 @@
138138
})
139139
})
140140

141-
describe('- endsWith(suffix)', function() {
141+
describe('- endsWith(suffixe1[, suffix2, ..])', function() {
142142
it("should return true if the string ends with the input string", function() {
143143
T (S("hello jon").endsWith('jon'));
144144
F (S('ffffaaa').endsWith('jon'));
145145
T (S("").endsWith(''));
146146
T (S("hi").endsWith(''));
147147
T (S("hi").endsWith('hi'));
148+
T (S("test.jpeg").endsWith('png', 'jpg', 'jpeg'));
149+
T (S("Chunky Bacon").endsWith(''));
150+
F (S("Chunky Bacon").endsWith("nk", "aco"));
148151
})
149152
})
150153

@@ -460,14 +463,16 @@
460463
})
461464
})
462465

463-
describe('- startsWith(prefix)', function() {
466+
describe('- startsWith(prefix1 [, prefix2, ...])', function() {
464467
it("should return true if the string starts with the input string", function() {
465468
T (S("JP is a software engineer").startsWith("JP"));
466469
F (S('wants to change the world').startsWith("politicians"));
467470
T (S("").startsWith(""));
468471
T (S("Hi").startsWith(""));
469472
T (S("JP").startsWith("JP"));
470-
})
473+
T (S("Chunky Bacon").startsWith("JP", "Chunk"));
474+
F (S("Lorem Ipsum").startsWith("Ip", "Sum"));
475+
});
471476
})
472477

473478
describe('- stripPunctuation()', function() {

0 commit comments

Comments
 (0)