Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stripLeft and stripRight method #133

Merged
merged 2 commits into from
Mar 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,27 @@ S(' 1 2 3--__--4 5 6-7__8__9--0').strip(' ', '_', '-').s; //'1234567890'
S('can words also be stripped out?').strip('words', 'also', 'be').s; //'can stripped out?'
```

### - stripLeft([chars]) ###
Returns a new string in which all chars have been stripped from the beginning of the string (default whitespace characters).

Example:

```javascript
S(' hello ').stripLeft().s; //'hello '
S('abcz').stripLeft('a-z').s; //'bcz'
S('www.example.com').stripLeft('w.').s; //'example.com'
```

### - stripRight([chars]) ###
Returns a new string in which all chars have been stripped from the end of the string (default whitespace characters).

Example:

```javascript
S(' hello ').stripRight().s; //' hello'
S('abcz').stripRight('a-z').s; //'abc'
```


### - stripPunctuation()

Expand Down
61 changes: 61 additions & 0 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,38 @@ string.js - Copyright (C) 2012-2014, JP Richardson <jprichardson@gmail.com>
return new this.constructor(ss);
},

stripLeft: function (chars) {
var regex;
var pattern;
var ss = ensureString(this.s);

if (chars === undefined) {
pattern = /^\s+/g;
}
else {
regex = escapeRegExp(chars);
pattern = new RegExp("^[" + regex + "]+", "g");
}

return new this.constructor(ss.replace(pattern, ""));
},

stripRight: function (chars) {
var regex;
var pattern;
var ss = ensureString(this.s);

if (chars === undefined) {
pattern = /\s+$/g;
}
else {
regex = escapeRegExp(chars);
pattern = new RegExp("[" + regex + "]+$", "g");
}

return new this.constructor(ss.replace(pattern, ""));
},

right: function(N) {
if (N >= 0) {
var s = this.s.substr(this.s.length - N, N);
Expand Down Expand Up @@ -757,6 +789,35 @@ string.js - Copyright (C) 2012-2014, JP Richardson <jprichardson@gmail.com>
amp: '&'
};

function escapeRegExp (s) {
// most part from https://github.com/skulpt/skulpt/blob/ecaf75e69c2e539eff124b2ab45df0b01eaf2295/src/str.js#L242
var c;
var i;
var ret = [];
var re = /^[A-Za-z0-9]+$/;
s = ensureString(s);
for (i = 0; i < s.length; ++i) {
c = s.charAt(i);

if (re.test(c)) {
ret.push(c);
}
else {
if (c === "\\000") {
ret.push("\\000");
}
else {
ret.push("\\" + c);
}
}
}
return ret.join("");
}

function ensureString(string) {
return string == null ? '' : '' + string;
}

//from underscore.string
var reversedEscapeChars = {};
for(var key in escapeChars){ reversedEscapeChars[escapeChars[key]] = key; }
Expand Down
49 changes: 49 additions & 0 deletions test/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,55 @@
})
})

describe('- stripLeft(chars)', function () {

it('should return the new string with all occurences of `chars` removed from left', function () {
T (S('hello').stripLeft().s === 'hello');
T (S('hello').stripLeft('').s === 'hello');
T (S(' hello ').stripLeft().s === 'hello ');
T (S('foo ').stripLeft().s === 'foo ');
T (S('').stripLeft().s === '');
T (S(null).stripLeft().s === '');
T (S(undefined).stripLeft().s === '');
T (S('aazz').stripLeft('a').s === 'zz');
T (S('yytest').stripLeft('t').s === 'yytest');
T (S('xxxyyxx').stripLeft('x').s === 'yyxx');
T (S('abcz').stripLeft('a-z').s === 'bcz');
T (S('z alpha z').stripLeft('a-z').s === ' alpha z');
T (S('_-foobar-_').stripLeft('_-').s === 'foobar-_');

T (S('_.foo-_').stripLeft('_.').s === 'foo-_');
T (S('?foo ').stripLeft('?').s === 'foo ');
T (S('[$]hello-^').stripLeft('^[a-z]$').s === 'hello-^');

T (S(123).stripLeft(1).s === '23');
});
});

describe('- stripRight(chars)', function () {

it('should return the new string with all occurences of `chars` removed from right', function () {
T (S('hello').stripRight().s === 'hello');
T (S('hello').stripRight('').s === 'hello');
T (S(' hello ').stripRight().s === ' hello');
T (S(' foo').stripRight().s === ' foo');
T (S('').stripRight().s === '');
T (S(null).stripRight().s === '');
T (S(undefined).stripRight().s === '');
T (S('aazz').stripRight('z').s === 'aa');
T (S('xxxyyxx').stripRight('x').s === 'xxxyy');
T (S('abcz').stripRight('a-z').s === 'abc');
T (S('z alpha z').stripRight('a-z').s === 'z alpha ');
T (S('_-foobar-_').stripRight('_-').s === '_-foobar');

T (S('_.foo_.').stripRight('_.').s === '_.foo');
T (S(' foo?').stripRight('?').s === ' foo');
T (S('[$]hello-^').stripRight('^[a-z]$').s === '[$]hello');

T (S(123).stripRight(3).s === '12');
});
});

describe('+ restorePrototype()', function() {
it('should restore the original String prototype', function() {
T (typeof ' hi'.endsWith === 'undefined');
Expand Down