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 String slicing #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Found something interesting? Please add it here :-)
- All server-side platforms including NodeJS.

## Roadmap
- Add `String` slicing
- Write unit tests

## Release History
Expand Down
7 changes: 5 additions & 2 deletions _s.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
var VERSION = '0.2.0';

var _s = function (arr, expr) {
var isStr = /string/.test(Object.prototype.toString.call(arr).toLowerCase());
arr = isStr ? Array.prototype.slice.call(arr) : arr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a good idea to alter the parameters. Define a new variable for that purpose.


var exprParts = expr.split(':');

if (exprParts.length > 0) {
Expand Down Expand Up @@ -82,7 +85,7 @@

//return sliced array if there is no `step` value
if (isNaN(step)) {
return slicedArr;
return isStr ? slicedArr.join('') : slicedArr;
}

var alteredArray = [];
Expand All @@ -91,7 +94,7 @@
alteredArray[alteredArray.length] = slicedArr[i];
}

return alteredArray;
return isStr ? alteredArray.join('') : alteredArray;
} else {
throw Error('Bad expression for _s.')
}
Expand Down