Skip to content

Commit 0b742fc

Browse files
committed
add stripLeft and stripRight method
1 parent c7bc7d2 commit 0b742fc

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

lib/string.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,38 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
355355
return new this.constructor(ss);
356356
},
357357

358+
stripLeft: function (chars) {
359+
var regex;
360+
var pattern;
361+
var ss = ensureString(this.s);
362+
363+
if (chars === undefined) {
364+
pattern = /^\s+/g;
365+
}
366+
else {
367+
regex = escapeRegExp(chars);
368+
pattern = new RegExp("^[" + regex + "]+", "g");
369+
}
370+
371+
return new this.constructor(ss.replace(pattern, ""));
372+
},
373+
374+
stripRight: function (chars) {
375+
var regex;
376+
var pattern;
377+
var ss = ensureString(this.s);
378+
379+
if (chars === undefined) {
380+
pattern = /\s+$/g;
381+
}
382+
else {
383+
regex = escapeRegExp(chars);
384+
pattern = new RegExp("[" + regex + "]+$", "g");
385+
}
386+
387+
return new this.constructor(ss.replace(pattern, ""));
388+
},
389+
358390
right: function(N) {
359391
if (N >= 0) {
360392
var s = this.s.substr(this.s.length - N, N);
@@ -757,6 +789,35 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
757789
amp: '&'
758790
};
759791

792+
function escapeRegExp (s) {
793+
// most part from https://github.com/skulpt/skulpt/blob/ecaf75e69c2e539eff124b2ab45df0b01eaf2295/src/str.js#L242
794+
var c;
795+
var i;
796+
var ret = [];
797+
var re = /^[A-Za-z0-9]+$/;
798+
s = ensureString(s);
799+
for (i = 0; i < s.length; ++i) {
800+
c = s.charAt(i);
801+
802+
if (re.test(c)) {
803+
ret.push(c);
804+
}
805+
else {
806+
if (c === "\\000") {
807+
ret.push("\\000");
808+
}
809+
else {
810+
ret.push("\\" + c);
811+
}
812+
}
813+
}
814+
return ret.join("");
815+
}
816+
817+
function ensureString(string) {
818+
return string == null ? '' : '' + string;
819+
}
820+
760821
//from underscore.string
761822
var reversedEscapeChars = {};
762823
for(var key in escapeChars){ reversedEscapeChars[escapeChars[key]] = key; }

test/string.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,55 @@
427427
})
428428
})
429429

430+
describe('- stripLeft(chars)', function () {
431+
432+
it('should return the new string with all occurences of `chars` removed from left', function () {
433+
T (S('hello').stripLeft().s === 'hello');
434+
T (S('hello').stripLeft('').s === 'hello');
435+
T (S(' hello ').stripLeft().s === 'hello ');
436+
T (S('foo ').stripLeft().s === 'foo ');
437+
T (S('').stripLeft().s === '');
438+
T (S(null).stripLeft().s === '');
439+
T (S(undefined).stripLeft().s === '');
440+
T (S('aazz').stripLeft('a').s === 'zz');
441+
T (S('yytest').stripLeft('t').s === 'yytest');
442+
T (S('xxxyyxx').stripLeft('x').s === 'yyxx');
443+
T (S('abcz').stripLeft('a-z').s === 'bcz');
444+
T (S('z alpha z').stripLeft('a-z').s === ' alpha z');
445+
T (S('_-foobar-_').stripLeft('_-').s === 'foobar-_');
446+
447+
T (S('_.foo-_').stripLeft('_.').s === 'foo-_');
448+
T (S('?foo ').stripLeft('?').s === 'foo ');
449+
T (S('[$]hello-^').stripLeft('^[a-z]$').s === 'hello-^');
450+
451+
T (S(123).stripLeft(1).s === '23');
452+
});
453+
});
454+
455+
describe('- stripRight(chars)', function () {
456+
457+
it('should return the new string with all occurences of `chars` removed from right', function () {
458+
T (S('hello').stripRight().s === 'hello');
459+
T (S('hello').stripRight('').s === 'hello');
460+
T (S(' hello ').stripRight().s === ' hello');
461+
T (S(' foo').stripRight().s === ' foo');
462+
T (S('').stripRight().s === '');
463+
T (S(null).stripRight().s === '');
464+
T (S(undefined).stripRight().s === '');
465+
T (S('aazz').stripRight('z').s === 'aa');
466+
T (S('xxxyyxx').stripRight('x').s === 'xxxyy');
467+
T (S('abcz').stripRight('a-z').s === 'abc');
468+
T (S('z alpha z').stripRight('a-z').s === 'z alpha ');
469+
T (S('_-foobar-_').stripRight('_-').s === '_-foobar');
470+
471+
T (S('_.foo_.').stripRight('_.').s === '_.foo');
472+
T (S(' foo?').stripRight('?').s === ' foo');
473+
T (S('[$]hello-^').stripRight('^[a-z]$').s === '[$]hello');
474+
475+
T (S(123).stripRight(3).s === '12');
476+
});
477+
});
478+
430479
describe('+ restorePrototype()', function() {
431480
it('should restore the original String prototype', function() {
432481
T (typeof ' hi'.endsWith === 'undefined');

0 commit comments

Comments
 (0)