From d358d00531154c34d15c3ae020279b61d3f62d6d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 14 Feb 2016 11:38:33 -0800 Subject: [PATCH] Ensure fp `castArray` shallow clones arrays. --- fp/_baseConvert.js | 10 ++++++++++ fp/_mapping.js | 6 +++--- test/test-fp.js | 47 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/fp/_baseConvert.js b/fp/_baseConvert.js index cb8975b2b..ba5abe706 100644 --- a/fp/_baseConvert.js +++ b/fp/_baseConvert.js @@ -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], diff --git a/fp/_mapping.js b/fp/_mapping.js index 57fbf28e5..5e817b04a 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -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: [ diff --git a/test/test-fp.js b/test/test-fp.js index 9a29fc866..d09e4e5be 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -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);