mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 09:27:49 +00:00
Ensure fp castArray shallow clones arrays.
This commit is contained in:
@@ -47,6 +47,7 @@ function baseConvert(util, name, func, options) {
|
||||
'cloneDeep': util.cloneDeep,
|
||||
'curry': util.curry,
|
||||
'forEach': util.forEach,
|
||||
'isArray': util.isArray,
|
||||
'isFunction': util.isFunction,
|
||||
'iteratee': util.iteratee,
|
||||
'keys': util.keys,
|
||||
@@ -58,6 +59,7 @@ function baseConvert(util, name, func, options) {
|
||||
cloneDeep = helpers.cloneDeep,
|
||||
curry = helpers.curry,
|
||||
each = helpers.forEach,
|
||||
isArray = helpers.isArray,
|
||||
isFunction = helpers.isFunction,
|
||||
keys = helpers.keys,
|
||||
rearg = helpers.rearg,
|
||||
@@ -130,6 +132,14 @@ function baseConvert(util, name, func, options) {
|
||||
};
|
||||
|
||||
var wrappers = {
|
||||
'castArray': function(castArray) {
|
||||
return function() {
|
||||
var value = arguments[0];
|
||||
return isArray(value)
|
||||
? castArray(cloneArray(value))
|
||||
: castArray.apply(undefined, arguments);
|
||||
};
|
||||
},
|
||||
'iteratee': function(iteratee) {
|
||||
return function() {
|
||||
var func = arguments[0],
|
||||
|
||||
@@ -39,9 +39,9 @@ exports.aliasToReal = {
|
||||
/** Used to map ary to method names. */
|
||||
exports.aryMethod = {
|
||||
1: [
|
||||
'attempt', 'ceil', 'create', 'curry', 'curryRight', 'floor', 'fromPairs',
|
||||
'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', 'over',
|
||||
'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext',
|
||||
'attempt', 'castArray', 'ceil', 'create', 'curry', 'curryRight', 'floor',
|
||||
'fromPairs', 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin',
|
||||
'over', 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext',
|
||||
'spread', 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words'
|
||||
],
|
||||
2: [
|
||||
|
||||
@@ -773,6 +773,48 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('fp.castArray');
|
||||
|
||||
(function() {
|
||||
QUnit.test('should shallow clone array values', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var array = [1],
|
||||
actual = fp.castArray(array);
|
||||
|
||||
assert.deepEqual(actual, array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
});
|
||||
|
||||
QUnit.test('should not shallow clone non-array values', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var object = { 'a': 1 },
|
||||
actual = fp.castArray(object);
|
||||
|
||||
assert.deepEqual(actual, [object]);
|
||||
assert.strictEqual(actual[0], object);
|
||||
});
|
||||
|
||||
QUnit.test('should convert by name', function(assert) {
|
||||
assert.expect(4);
|
||||
|
||||
var array = [1],
|
||||
object = { 'a': 1 },
|
||||
castArray = convert('castArray', _.castArray),
|
||||
actual = castArray(array);
|
||||
|
||||
assert.deepEqual(actual, array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
|
||||
actual = castArray(object);
|
||||
assert.deepEqual(actual, [object]);
|
||||
assert.strictEqual(actual[0], object);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('fp.curry and fp.curryRight');
|
||||
|
||||
_.each(['curry', 'curryRight'], function(methodName) {
|
||||
@@ -823,9 +865,8 @@
|
||||
function Foo() {}
|
||||
Foo.prototype = { 'b': 2 };
|
||||
|
||||
var object = { 'a': 1 };
|
||||
|
||||
var extend = convert('extend', _.extend),
|
||||
var object = { 'a': 1 },
|
||||
extend = convert('extend', _.extend),
|
||||
value = _.clone(object),
|
||||
actual = extend(value)(new Foo);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user