8000 Add `split` and `replace` to `LodashWrapper`. [closes #1016] · lodash/lodash@eb1b7b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb1b7b9

Browse files
octrefjdalton
authored andcommitted
Add split and replace to LodashWrapper. [closes #1016]
1 parent 135bc2d commit eb1b7b9

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

lodash.src.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@
858858
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
859859
* and `unshift`
860860
*
861+
* These `String` methods are also available:
862+
* `split` and `replace`
863+
*
861864
* The wrapper methods that support shortcut fusion are:
862865
* `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
863866
* `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
@@ -11707,22 +11710,29 @@
1170711710
};
1170811711
});
1170911712

11710-
// Add `Array.prototype` functions to `lodash.prototype`.
11711-
arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
11713+
// Add `Array.prototype` and `String.prototype` functions to `lodash.prototype`.
11714+
arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift',
11715+
'split', 'replace'], function(methodName) {
1171211716
var arrayFunc = arrayProto[methodName],
11717+
stringFunc = stringProto[methodName],
11718+
isStringFunc = /^(?:split|replace)$/.test(methodName),
1171311719
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
1171411720
fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName),
1171511721
retUnwrapped = /^(?:join|pop|shift)$/.test(methodName);
1171611722

11717-
// Avoid array-like object bugs with `Array#shift` and `Array#splice` in
11718-
// IE < 9, Firefox < 10, Narwhal, and RingoJS.
11719-
var func = !fixObjects ? arrayFunc : function() {
11720-
var result = arrayFunc.apply(this, arguments);
11721-
if (this.length === 0) {
11722-
delete this[0];
11723-
}
11724-
return result;
11725-
};
11723+
if (isStringFunc) {
11724+
var func = stringFunc;
11725+
} else {
11726+
// Avoid array-like object bugs with `Array#shift` and `Array#splice` in
11727+
// IE < 9, Firefox < 10, Narwhal, and RingoJS.
11728+
var func = !fixObjects ? arrayFunc : function() {
11729+
var result = arrayFunc.apply(this, arguments);
11730+
if (this.length === 0) {
11731+
delete this[0];
11732+
}
11733+
return result;
11734+
};
11735+
}
1172611736

1172711737
lodash.prototype[methodName] = function() {
1172811738
var args = arguments;

test/test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15653,6 +15653,25 @@
1565315653

1565415654
/*--------------------------------------------------------------------------*/
1565515655

15656+
QUnit.module('lodash(...).replace');
15657+
15658+
(function() {
15659+
test('should support string replace', 2, function() {
15660+
if (!isNpm) {
15661+
var string = 'hi hidash',
15662+
wrapped = _(string);
15663+
15664+
deepEqual(wrapped.replace('hi', 'lo').value(), 'lo hidash');
15665+
deepEqual(wrapped.replace(/hi/g, 'lo').value(), 'lo lodash');
15666+
}
15667+
else {
15668+
skipTest(2);
15669+
}
15670+
});
15671+
}());
15672+
15673+
/*--------------------------------------------------------------------------*/
15674+
1565615675
QUnit.module('lodash(...).reverse');
1565715676

1565815677
(function() {
@@ -15822,6 +15841,40 @@
1582215841

1582315842
/*--------------------------------------------------------------------------*/
1582415843

15844+
QUnit.module('lodash(...).split');
15845+
15846+
(function() {
15847+
test('should support string split', 1, function() {
15848+
if (!isNpm) {
15849+
var string = 'hi ya',
15850+
wrapped = _(string),
15851+
actual = ['hi', 'ya'];
15852+
15853+
deepEqual(wrapped.split(' ').value(), actual);
15854+
}
15855+
else {
15856+
skipTest(1);
15857+
}
15858+
});
15859+
}());
15860+
15861+
(function() {
15862+
test('should allow mixed string and array prototype methods', 1, function() {
15863+
if (!isNpm) {
15864+
var string = 'hi ya',
15865+
wrapped = _(string),
15866+
actual = 'hi,ya';
15867+
15868+
deepEqual(wrapped.split(' ').join(','), actual);
15869+
}
15870+
else {
15871+
skipTest(1);
15872+
}
15873+
});
15874+
}());
15875+
15876+
/*--------------------------------------------------------------------------*/
15877+
1582515878
QUnit.module('lodash(...).unshift');
1582615879

1582715880
(function() {

0 commit comments

Comments
 (0)
0