mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-14 04:37:50 +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,
|
'cloneDeep': util.cloneDeep,
|
||||||
'curry': util.curry,
|
'curry': util.curry,
|
||||||
'forEach': util.forEach,
|
'forEach': util.forEach,
|
||||||
|
'isArray': util.isArray,
|
||||||
'isFunction': util.isFunction,
|
'isFunction': util.isFunction,
|
||||||
'iteratee': util.iteratee,
|
'iteratee': util.iteratee,
|
||||||
'keys': util.keys,
|
'keys': util.keys,
|
||||||
@@ -58,6 +59,7 @@ function baseConvert(util, name, func, options) {
|
|||||||
cloneDeep = helpers.cloneDeep,
|
cloneDeep = helpers.cloneDeep,
|
||||||
curry = helpers.curry,
|
curry = helpers.curry,
|
||||||
each = helpers.forEach,
|
each = helpers.forEach,
|
||||||
|
isArray = helpers.isArray,
|
||||||
isFunction = helpers.isFunction,
|
isFunction = helpers.isFunction,
|
||||||
keys = helpers.keys,
|
keys = helpers.keys,
|
||||||
rearg = helpers.rearg,
|
rearg = helpers.rearg,
|
||||||
@@ -130,6 +132,14 @@ function baseConvert(util, name, func, options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var wrappers = {
|
var wrappers = {
|
||||||
|
'castArray': function(castArray) {
|
||||||
|
return function() {
|
||||||
|
var value = arguments[0];
|
||||||
|
return isArray(value)
|
||||||
|
? castArray(cloneArray(value))
|
||||||
|
: castArray.apply(undefined, arguments);
|
||||||
|
};
|
||||||
|
},
|
||||||
'iteratee': function(iteratee) {
|
'iteratee': function(iteratee) {
|
||||||
return function() {
|
return function() {
|
||||||
var func = arguments[0],
|
var func = arguments[0],
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ exports.aliasToReal = {
|
|||||||
/** Used to map ary to method names. */
|
/** Used to map ary to method names. */
|
||||||
exports.aryMethod = {
|
exports.aryMethod = {
|
||||||
1: [
|
1: [
|
||||||
'attempt', 'ceil', 'create', 'curry', 'curryRight', 'floor', 'fromPairs',
|
'attempt', 'castArray', 'ceil', 'create', 'curry', 'curryRight', 'floor',
|
||||||
'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin', 'over',
|
'fromPairs', 'invert', 'iteratee', 'memoize', 'method', 'methodOf', 'mixin',
|
||||||
'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext',
|
'over', 'overEvery', 'overSome', 'rest', 'reverse', 'round', 'runInContext',
|
||||||
'spread', 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words'
|
'spread', 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words'
|
||||||
],
|
],
|
||||||
2: [
|
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');
|
QUnit.module('fp.curry and fp.curryRight');
|
||||||
|
|
||||||
_.each(['curry', 'curryRight'], function(methodName) {
|
_.each(['curry', 'curryRight'], function(methodName) {
|
||||||
@@ -823,9 +865,8 @@
|
|||||||
function Foo() {}
|
function Foo() {}
|
||||||
Foo.prototype = { 'b': 2 };
|
Foo.prototype = { 'b': 2 };
|
||||||
|
|
||||||
var object = { 'a': 1 };
|
var object = { 'a': 1 },
|
||||||
|
extend = convert('extend', _.extend),
|
||||||
var extend = convert('extend', _.extend),
|
|
||||||
value = _.clone(object),
|
value = _.clone(object),
|
||||||
actual = extend(value)(new Foo);
|
actual = extend(value)(new Foo);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user