object-assign-deep
Version:
Allows deep cloning of plain objects that contain primitives, nested plain objects, or nested plain arrays.
46 lines (35 loc) • 1.25 kB
JavaScript
;
/*
* ARRAY TEST
*
* [Description]
* Demonstrates that arrays are concatenated without removing duplicates, and are also recursed into to break
* references on any objects or arrays that are contained within.
*
* [Expected Output]
* {
* shallowArray: [ 'z', 'G', 'a' ],
* deepArray: [ 1, 2, 3, 10, 11, 12, 1, 2, 3, { deep: 'yes' }, 4, 5, 6 ] },
* }
*
*/
/* eslint no-console: 0 */
const objectAssignDeep = require(`../objectAssignDeep`);
const deeplyNestedObject = { deep: `yes` };
const objectA = {
shallowArray: [`a`, `x`, `y`, `y`, `y`],
deepArray: [1, 2, 3, 10, 11, 12],
};
const objectB = {
shallowArray: null,
deepArray: [1, 2, 3, deeplyNestedObject, 4, 5, 6],
};
const objectC = {
shallowArray: [`z`, `G`, `a`],
};
const resultWithArrayReplace = objectAssignDeep({}, objectA, objectB, objectC);
const resultWithArrayMerge = objectAssignDeep.withOptions({}, [objectA, objectB, objectC], { arrayBehaviour: `merge` });
deeplyNestedObject.hello = `This value should not be in the result because it was added to the object after cloning.`;
console.log(`Result with array REPLACE:`, resultWithArrayReplace);
console.log(``);
console.log(`Result with array MERGE:`, resultWithArrayMerge);