From b3ccd87b3bf8e6b3dcc24a0a76cf660ebec3c98e Mon Sep 17 00:00:00 2001 From: Reza Farhadian Date: Thu, 25 Sep 2014 21:18:04 +0330 Subject: [PATCH 1/2] Add `String` slicing --- README.md | 1 - _s.js | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 28d9939..5555851 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/_s.js b/_s.js index 343f214..2f97c75 100644 --- a/_s.js +++ b/_s.js @@ -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; + var exprParts = expr.split(':'); if (exprParts.length > 0) { @@ -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 = []; @@ -91,7 +94,7 @@ alteredArray[alteredArray.length] = slicedArr[i]; } - return alteredArray; + return isStr ? alteredArray.join('') : alteredArray; } else { throw Error('Bad expression for _s.') } From cf277106031a3d6ed28b369783786fe2a86b95f0 Mon Sep 17 00:00:00 2001 From: Reza Farhadian Date: Fri, 26 Sep 2014 11:54:13 +0330 Subject: [PATCH 2/2] Define a new variable for String slicing --- _s.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/_s.js b/_s.js index 2f97c75..7e89b52 100644 --- a/_s.js +++ b/_s.js @@ -21,7 +21,13 @@ var _s = function (arr, expr) { var isStr = /string/.test(Object.prototype.toString.call(arr).toLowerCase()); - arr = isStr ? Array.prototype.slice.call(arr) : arr; + + var length = arr.length; + + if (isStr) { + var str = Array.prototype.slice.call(arr); + length = str.length; + } var exprParts = expr.split(':'); @@ -34,7 +40,7 @@ var arr = arr.slice(0); //return empty array if given array is empty - if (arr.length == 0) { + if (length == 0) { return []; } @@ -46,12 +52,12 @@ //set default for `from` if they are not defined if (isNaN(from)) { // default value depends on reverse or not - from = step < 0 ? arr.length : 0; + from = step < 0 ? length : 0; } //change `from` if we have negative `from` if (from < 0) { - from = arr.length + from + 1; + from = length + from + 1; } //set default for `to` if they are not defined @@ -61,31 +67,31 @@ to = from + 1; } else { // default value depends on reverse or not - to = step < 0 ? 0 : arr.length; + to = step < 0 ? 0 : length; } } //change `to` if we have negative `to` if (to < 0) { - to = arr.length + to + 1; + to = length + to + 1; } //reverse the array if we have negative `step` if (step < 0) { - arr = arr.reverse(); + isStr ? str = str.reverse() : arr = arr.reverse(); - to = arr.length - to; - from = arr.length - from; + to = length - to; + from = length - from; step = Math.abs(step); } //slice the array with `from` and `to` variables - var slicedArr = arr.slice(from, to); + var slicedArr = isStr ? str.slice(from, to) : arr.slice(from, to); //return sliced array if there is no `step` value if (isNaN(step)) { - return isStr ? slicedArr.join('') : slicedArr; + return slicedArr; } var alteredArray = []; @@ -94,7 +100,7 @@ alteredArray[alteredArray.length] = slicedArr[i]; } - return isStr ? alteredArray.join('') : alteredArray; + return alteredArray; } else { throw Error('Bad expression for _s.') }