From eb1b7b914a2228d19bcb908717660c25f3bd9474 Mon Sep 17 00:00:00 2001 From: octref Date: Fri, 6 Mar 2015 21:52:10 -0500 Subject: [PATCH] Add `split` and `replace` to `LodashWrapper`. [closes #1016] --- lodash.src.js | 32 ++++++++++++++++++++----------- test/test.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index c26e300a2..d94baa339 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -858,6 +858,9 @@ * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`, * and `unshift` * + * These `String` methods are also available: + * `split` and `replace` + * * The wrapper methods that support shortcut fusion are: * `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`, * `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`, @@ -11707,22 +11710,29 @@ }; }); - // Add `Array.prototype` functions to `lodash.prototype`. - arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) { + // Add `Array.prototype` and `String.prototype` functions to `lodash.prototype`. + arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift', + 'split', 'replace'], function(methodName) { var arrayFunc = arrayProto[methodName], + stringFunc = stringProto[methodName], + isStringFunc = /^(?:split|replace)$/.test(methodName), chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru', fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName), retUnwrapped = /^(?:join|pop|shift)$/.test(methodName); - // Avoid array-like object bugs with `Array#shift` and `Array#splice` in - // IE < 9, Firefox < 10, Narwhal, and RingoJS. - var func = !fixObjects ? arrayFunc : function() { - var result = arrayFunc.apply(this, arguments); - if (this.length === 0) { - delete this[0]; - } - return result; - }; + if (isStringFunc) { + var func = stringFunc; + } else { + // Avoid array-like object bugs with `Array#shift` and `Array#splice` in + // IE < 9, Firefox < 10, Narwhal, and RingoJS. + var func = !fixObjects ? arrayFunc : function() { + var result = arrayFunc.apply(this, arguments); + if (this.length === 0) { + delete this[0]; + } + return result; + }; + } lodash.prototype[methodName] = function() { var args = arguments; diff --git a/test/test.js b/test/test.js index a6b875fa8..f3f578ac6 100644 --- a/test/test.js +++ b/test/test.js @@ -15653,6 +15653,25 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash(...).replace'); + + (function() { + test('should support string replace', 2, function() { + if (!isNpm) { + var string = 'hi hidash', + wrapped = _(string); + + deepEqual(wrapped.replace('hi', 'lo').value(), 'lo hidash'); + deepEqual(wrapped.replace(/hi/g, 'lo').value(), 'lo lodash'); + } + else { + skipTest(2); + } + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash(...).reverse'); (function() { @@ -15822,6 +15841,40 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash(...).split'); + + (function() { + test('should support string split', 1, function() { + if (!isNpm) { + var string = 'hi ya', + wrapped = _(string), + actual = ['hi', 'ya']; + + deepEqual(wrapped.split(' ').value(), actual); + } + else { + skipTest(1); + } + }); + }()); + + (function() { + test('should allow mixed string and array prototype methods', 1, function() { + if (!isNpm) { + var string = 'hi ya', + wrapped = _(string), + actual = 'hi,ya'; + + deepEqual(wrapped.split(' ').join(','), actual); + } + else { + skipTest(1); + } + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash(...).unshift'); (function() {