Skip to content

Add reverse method #124

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ Example:
var stuff = "My name is JP\nJavaScript is my fav language\r\nWhat is your fav language?"
var lines = S(stuff).lines()

console.dir(lines)
console.dir(lines)
/*
[ 'My name is JP',
'JavaScript is my fav language',
Expand All @@ -523,7 +523,7 @@ console.dir(lines)

### - pad(len, [char])

Pads the string in the center with specified character. `char` may be a string or a number, defaults is a space.
Pads the string in the center with specified character. `char` may be a string or a number, defaults is a space.

Example:

Expand Down Expand Up @@ -629,6 +629,18 @@ Example:
S.restorePrototype();
```

### - reverse ###

Returns reversed string.

Example:

```javascript
S('backwards').reverse().s; // 'sdrawkcab'
S('backwards').reverse().reverse().s; // 'backwards'
S(undefined).reverse() // undefined
S(null).reverse() // null
```

### - right(n) ###

Expand Down Expand Up @@ -955,7 +967,7 @@ S('&lt;div&gt;hi&lt;/div&gt;').unescapeHTML().s; //<div>hi</div>

### - wrapHTML() ###

wrapHTML helps to avoid concatenation of element with string.
wrapHTML helps to avoid concatenation of element with string.
the string will be wrapped with HTML Element and their attributes.

Example:
Expand Down Expand Up @@ -1059,7 +1071,7 @@ If you contribute to this library, just modify `string.js`, `string.test.js`, an
- [*] [Nathan Friedly](https://github.com/nfriedly)
- [*] [Alison Rowland](https://github.com/arowla)



Roadmap to v2.0
---------------
Expand Down
34 changes: 24 additions & 10 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
var s = this.s;
var startPos = s.indexOf(left);
var endPos = s.indexOf(right, startPos + left.length);
if (endPos == -1 && right != null)
if (endPos == -1 && right != null)
return new this.constructor('')
else if (endPos == -1 && right == null)
return new this.constructor(s.substring(startPos + left.length))
else
else
return new this.constructor(s.slice(startPos + left.length, endPos));
},

Expand Down Expand Up @@ -154,7 +154,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
.replace(/&([^;\W]+;?)/g, function (m, e) {
var ee = e.replace(/;$/, '');
var target = ENTITIES[e] || (e.match(/;$/) && ENTITIES[ee]);

if (typeof target === 'number') {
return String.fromCharCode(target);
}
Expand Down Expand Up @@ -239,7 +239,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
return this.right(-N);
}
},

lines: function() { //convert windows newlines to unix newlines then convert to an Array of lines
return this.replaceAll('\r\n', '\n').s.split('\n');
},
Expand Down Expand Up @@ -355,6 +355,20 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
return new this.constructor(s);
},

reverse: function() {
var i, o = '', ss = this.s;

if (ss === null || ss === undefined) {
return new this.constructor('');
}

for (i = ss.length - 1; i >= 0; i--) {
o += ss[i];
}

return new this.constructor(o);
},

strip: function() {
var ss = this.s;
for(var i= 0, n=arguments.length; i<n; i++) {
Expand Down Expand Up @@ -451,9 +465,9 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>

trim: function() {
var s;
if (typeof __nsp.trim === 'undefined')
if (typeof __nsp.trim === 'undefined')
s = this.s.replace(/(^\s*|\s*$)/g, '')
else
else
s = this.s.trim()
return new this.constructor(s);
},
Expand Down Expand Up @@ -537,19 +551,19 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
var shouldQualify = hasVal(qualifier)
if (typeof dataArray[i] == 'number')
shouldQualify &= encloseNumbers;

if (shouldQualify)
buildString.push(qualifier);

if (dataArray[i] !== null && dataArray[i] !== undefined) {
var d = new S(dataArray[i]).replaceAll(qualifier, rep).s;
buildString.push(d);
} else
} else
buildString.push('')

if (shouldQualify)
buildString.push(qualifier);

if (delim)
buildString.push(delim);
}
Expand Down
Loading