mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57: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`,
|
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
|
||||||
* and `unshift`
|
* and `unshift`
|
||||||
*
|
*
|
||||||
|
* These `String` methods are also available:
|
||||||
|
* `split` and `replace`
|
||||||
|
*
|
||||||
* The wrapper methods that support shortcut fusion are:
|
* The wrapper methods that support shortcut fusion are:
|
||||||
* `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
|
* `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
|
||||||
* `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
|
* `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
|
||||||
@@ -11707,22 +11710,29 @@
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add `Array.prototype` functions to `lodash.prototype`.
|
// Add `Array.prototype` and `String.prototype` functions to `lodash.prototype`.
|
||||||
arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
arrayEach(['concat', 'join', 'pop', 'push', 'shift', 'sort', 'splice', 'unshift',
|
||||||
|
'split', 'replace'], function(methodName) {
|
||||||
var arrayFunc = arrayProto[methodName],
|
var arrayFunc = arrayProto[methodName],
|
||||||
|
stringFunc = stringProto[methodName],
|
||||||
|
isStringFunc = /^(?:split|replace)$/.test(methodName),
|
||||||
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
|
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
|
||||||
fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName),
|
fixObjects = !support.spliceObjects && /^(?:pop|shift|splice)$/.test(methodName),
|
||||||
retUnwrapped = /^(?:join|pop|shift)$/.test(methodName);
|
retUnwrapped = /^(?:join|pop|shift)$/.test(methodName);
|
||||||
|
|
||||||
// Avoid array-like object bugs with `Array#shift` and `Array#splice` in
|
if (isStringFunc) {
|
||||||
// IE < 9, Firefox < 10, Narwhal, and RingoJS.
|
var func = stringFunc;
|
||||||
var func = !fixObjects ? arrayFunc : function() {
|
} else {
|
||||||
var result = arrayFunc.apply(this, arguments);
|
// Avoid array-like object bugs with `Array#shift` and `Array#splice` in
|
||||||
if (this.length === 0) {
|
// IE < 9, Firefox < 10, Narwhal, and RingoJS.
|
||||||
delete this[0];
|
var func = !fixObjects ? arrayFunc : function() {
|
||||||
}
|
var result = arrayFunc.apply(this, arguments);
|
||||||
return result;
|
if (this.length === 0) {
|
||||||
};
|
delete this[0];
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
lodash.prototype[methodName] = function() {
|
||||||
var args = arguments;
|
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');
|
QUnit.module('lodash(...).reverse');
|
||||||
|
|
||||||
(function() {
|
(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');
|
QUnit.module('lodash(...).unshift');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user