From ddafd4fe13268aea135cad754c940524f0b1f9f6 Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:03:42 -0700 Subject: [PATCH 1/6] Adds rchop --- lib/underscore.string.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/underscore.string.js b/lib/underscore.string.js index a559f930..b6e29df2 100644 --- a/lib/underscore.string.js +++ b/lib/underscore.string.js @@ -213,6 +213,22 @@ return step > 0 ? str.match(new RegExp('.{1,' + step + '}', 'g')) : [str]; }, + rchop: function(str, step){ + if (str == null) return []; + str = String(str); + step = ~~step; + + var i = str.length; + if (i <= step || step <= 0) return [str]; + + var parts = []; + while (i > 0) { + parts.unshift( str.slice(Math.max(0, i - step), i) ); + i -= step; + } + return parts; + }, + clean: function(str){ return _s.strip(str).replace(/\s+/g, ' '); }, @@ -641,6 +657,7 @@ // Aliases + _s.lchop = _s.chop; _s.strip = _s.trim; _s.lstrip = _s.ltrim; _s.rstrip = _s.rtrim; From 02ba8a6371fc99c2e3d58cbd600a78264ddd9364 Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:03:56 -0700 Subject: [PATCH 2/6] Test for rchop --- test/strings.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/strings.js b/test/strings.js index 77364f20..4dcfb41b 100644 --- a/test/strings.js +++ b/test/strings.js @@ -192,6 +192,17 @@ $(document).ready(function() { ok(_(12345).chop(1).length === 5, 'output [1, 2, 3, 4, 5]'); }); + test('String: rchop', function(){ + ok(_('whitespace').rchop(2).length === 5, 'output [wh, it, es, pa, ce]'); + ok(_('whitespace').rchop(3).length === 4, 'output [w, hit, esp, ace]'); + ok(_('whitespace').rchop()[0].length === 10, 'output [whitespace]'); + ok(_('whitespace').rchop(-1)[0].length === 10, 'output [whitespace]'); + ok(_('whitespace').rchop(NaN)[0].length === 10, 'output [whitespace]'); + ok(_('whitespace').rchop(null)[0].length === 10, 'output [whitespace]'); + ok(_('whitespace').rchop({})[0].length === 10, 'output [whitespace]'); + ok(_(12345).rchop(1).length === 5, 'output [1, 2, 3, 4, 5]'); + }); + test('String: clean', function(){ equal(_.clean(' foo bar '), 'foo bar'); equal(_.clean(''), ''); From 827eb4f368ccaa35b78e564aaa3f018055b72dfb Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:04:34 -0700 Subject: [PATCH 3/6] 'let' is reserved in Harmony --- test/speed.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/speed.js b/test/speed.js index 9ceeea76..0cee3f51 100644 --- a/test/speed.js +++ b/test/speed.js @@ -59,11 +59,11 @@ }); JSLitmus.test('succ', function(){ - var let = 'a', alphabet = []; + var ch = 'a', alphabet = []; for (var i=0; i < 26; i++) { - alphabet.push(let); - let = _(let).succ(); + alphabet.push(ch); + ch = _(ch).succ(); } return alphabet; From 489622cc3643af5169fea0012178fa7d54f244ec Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:15:27 -0700 Subject: [PATCH 4/6] A faster rchop using a RegExp --- lib/underscore.string.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/underscore.string.js b/lib/underscore.string.js index b6e29df2..1637709e 100644 --- a/lib/underscore.string.js +++ b/lib/underscore.string.js @@ -217,16 +217,7 @@ if (str == null) return []; str = String(str); step = ~~step; - - var i = str.length; - if (i <= step || step <= 0) return [str]; - - var parts = []; - while (i > 0) { - parts.unshift( str.slice(Math.max(0, i - step), i) ); - i -= step; - } - return parts; + return step > 0 ? str.match(new RegExp('(.{1,' + step + '})(?=(?:.{' + step + '})*$)', 'g')) : [str]; }, clean: function(str){ From 5585f9ff0251a239b31dc6f28a999fa9c55469c8 Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:15:44 -0700 Subject: [PATCH 5/6] Adds rchop to readme. --- README.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.markdown b/README.markdown index e98b4c2f..210447eb 100644 --- a/README.markdown +++ b/README.markdown @@ -116,6 +116,13 @@ _.chop('whitespace', 3) => ['whi','tes','pac','e'] ``` +**rchop** _.rchop(string, step) + +```javascript +_.chop('whitespace', 3) +=> ['w', 'hit', 'esp', 'ace'] +``` + **clean** _.clean(str) Compress some whitespaces to one. From 91358cd370362c44be052050cce4c393d85dce87 Mon Sep 17 00:00:00 2001 From: dsc Date: Sun, 28 Sep 2014 01:20:46 -0700 Subject: [PATCH 6/6] Typo in readme. --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 210447eb..5d63032d 100644 --- a/README.markdown +++ b/README.markdown @@ -119,7 +119,7 @@ _.chop('whitespace', 3) **rchop** _.rchop(string, step) ```javascript -_.chop('whitespace', 3) +_.rchop('whitespace', 3) => ['w', 'hit', 'esp', 'ace'] ```