mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 16:47:49 +00:00
Add split and replace to LodashWrapper. [closes #1016]
This commit is contained in:
@@ -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;
|
||||
|
||||
53
test/test.js
53
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() {
|
||||
|
||||
Reference in New Issue
Block a user