core-functions
Advanced tools
Comparing version 2.0.13 to 2.0.14
@@ -17,3 +17,4 @@ 'use strict'; | ||
copy: copy, | ||
getPropertyValue: getPropertyValue | ||
getPropertyValue: getPropertyValue, | ||
copyNamedProperties: copyNamedProperties | ||
}; | ||
@@ -178,2 +179,46 @@ | ||
return value; | ||
} | ||
/** | ||
* Copies ONLY the (simple or compound) named properties, which are listed in the given propertyNames array, from the | ||
* given source object to a new destination object. Note that the value of any compound named property in the given | ||
* propertyNames array will be copied from the source and stored in the new destination object under the compound | ||
* property name if compact is true. | ||
* @param {Object} src - the source object from which to copy the named properties | ||
* @param {string[]} propertyNames - the list of named properties to be copied | ||
* @param {boolean|undefined} [compact] - whether to create a flatter, more-compact destination object, which will use | ||
* any compound property names as is and eliminate any unnecessary intermediate objects or rather create a more- | ||
* conventional, less-compact destination object, which will only have simple property names and all necessary intermediate objects | ||
* @param {boolean|undefined} [deep] - executes a deep copy of each property value if the given deep flag is true, otherwise only does a shallow copy | ||
* @param {boolean|undefined} [omitPropertyIfUndefined] - whether or not to omit any named property that has an undefined value from the destination object | ||
* @returns {Object} a new object containing copies of only the named properties from the source object | ||
*/ | ||
function copyNamedProperties(src, propertyNames, compact, deep, omitPropertyIfUndefined) { | ||
if (!src || typeof src !== 'object') { | ||
return src === undefined ? undefined : src === null ? null : {}; | ||
} | ||
const dest = {}; | ||
for (let i = 0; i < propertyNames.length; ++i) { | ||
const propertyName = trim(propertyNames[i]); | ||
const propertyValue = getPropertyValue(src, propertyName); | ||
if (!omitPropertyIfUndefined || propertyValue !== undefined) { | ||
if (compact) { | ||
dest[propertyName] = copy(propertyValue, deep); | ||
} else { | ||
const names = propertyName.split(".").map(n => trim(n)).filter(name => isNotBlank(name)); | ||
if (names.length > 0) { | ||
let d = dest; | ||
for (let j = 0; j < names.length - 1; ++j) { | ||
const name = names[j]; | ||
if (!d[name]) { | ||
d[name] = {}; | ||
} | ||
d = d[name]; | ||
} | ||
d[names[names.length - 1]] = copy(propertyValue, deep); | ||
} | ||
} | ||
} | ||
} | ||
return dest; | ||
} |
{ | ||
"name": "core-functions", | ||
"version": "2.0.13", | ||
"version": "2.0.14", | ||
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -1,2 +0,2 @@ | ||
# core-functions v2.0.13 | ||
# core-functions v2.0.14 | ||
@@ -106,2 +106,5 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including | ||
### 2.0.14 | ||
- Added `copyNamedProperties` function to `objects.js` module | ||
### 2.0.13 | ||
@@ -108,0 +111,0 @@ - Added `getPropertyValue` function to `objects.js` module |
@@ -197,2 +197,39 @@ 'use strict'; | ||
test('copy with non-objects & empty object & empty array', t => { | ||
// Non-objects | ||
t.deepEqual(Objects.copy(undefined, false), undefined, 'shallow copy of undefined is undefined'); | ||
t.deepEqual(Objects.copy(undefined, true), undefined, 'deep copy of undefined is undefined'); | ||
t.deepEqual(Objects.copy(null, false), null, 'shallow copy of null is null'); | ||
t.deepEqual(Objects.copy(null, true), null, 'deep copy of null is null'); | ||
t.deepEqual(Objects.copy('', false), '', `shallow copy of '' is ''`); | ||
t.deepEqual(Objects.copy('', true), '', `deep copy of '' is ''`); | ||
t.deepEqual(Objects.copy('abc', false), 'abc', `shallow copy of 'abc' is 'abc'`); | ||
t.deepEqual(Objects.copy('abc', true), 'abc', `deep copy of 'abc' is 'abc'`); | ||
t.deepEqual(Objects.copy(123, false), 123, 'shallow copy of 123 is 123'); | ||
t.deepEqual(Objects.copy(123, true), 123, 'deep copy of 123 is 123'); | ||
t.deepEqual(Objects.copy(123.456, false), 123.456, 'shallow copy of 123.456 is 123.456'); | ||
t.deepEqual(Objects.copy(123.456, true), 123.456, 'deep copy of 123.456 is 123.456'); | ||
t.deepEqual(Objects.copy(true, false), true, 'shallow copy of true is true'); | ||
t.deepEqual(Objects.copy(true, true), true, 'deep copy of true is true'); | ||
t.deepEqual(Objects.copy(false, false), false, 'shallow copy of false is false'); | ||
t.deepEqual(Objects.copy(false, false), false, 'deep copy of false is false'); | ||
// Empty object | ||
t.deepEqual(Objects.copy({}, false), {}, 'shallow copy of {} is {}'); | ||
t.deepEqual(Objects.copy({}, true), {}, 'deep copy of {} is {}'); | ||
// Empty array | ||
t.deepEqual(Objects.copy([], false), [], 'shallow copy of [] is []'); | ||
t.deepEqual(Objects.copy([], true), [], 'deep copy of [] is []'); | ||
t.end(); | ||
}); | ||
test('copy', t => { | ||
@@ -732,2 +769,3 @@ function a() { | ||
t.equal(Objects.getPropertyValue({}, 'a.b.c'), undefined, `{}.a.b.c must be undefined`); | ||
t.equal(Objects.getPropertyValue([], 'a.b.c'), undefined, `[].a.b.c must be undefined`); | ||
@@ -742,2 +780,76 @@ const o = {a: 1, b: {c: 'c', d: {e: 'e'}}}; | ||
t.end(); | ||
}); | ||
test('copyNamedProperties - compact', t => { | ||
const compact = true; | ||
const deep = true; | ||
const omit = true; | ||
t.deepEqual(Objects.copyNamedProperties(undefined, ['a.b.c'], compact, deep, omit), undefined, `(undefined, ['a.b.c'], compact, deep, omit) must be undefined`); | ||
t.deepEqual(Objects.copyNamedProperties(null, ['a.b.c'], compact, deep, omit), null, `(null, ['a.b.c'], compact, deep, omit) must be null`); | ||
t.deepEqual(Objects.copyNamedProperties({}, ['a.b.c'], compact, deep, omit), {}, `({}, ['a.b.c'], compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties({}, ['a.b.c'], compact, deep, !omit), {'a.b.c': undefined}, `({}, ['a.b.c'], compact, deep, !omit) must be {'a.b.c': undefined}`); | ||
t.deepEqual(Objects.copyNamedProperties([], ['a.b.c'], compact, deep, omit), {}, `([] ['a.b.c'], compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties([], ['a.b.c'], compact, deep, !omit), {'a.b.c': undefined}, `([], ['a.b.c'], compact, deep, !omit) must be {'a.b.c': undefined}`); | ||
const o = {a: 1, b: {c: 'c', d: {e: 'e'}}}; | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a'], compact, deep, !omit), {a: 1}, `(o, [a], compact, deep, !omit) must be {a: 1}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b'], compact, deep, !omit), {b: {c: 'c', d: {e: 'e'}}}, `(o, [b], compact, deep, !omit) must be {b: {c: 'c', d: {e: 'e'}}}`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], compact, deep, !omit).b, o.b, `(o, [b], compact, deep, !omit).b must NOT be o.b`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b, o.b, `(o, [b], !deep, !omit).b must NOT be o.b`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], compact, deep, !omit).b.c, o.b.c, `(o, [b], compact, deep, !omit).b.c must equal o.b.c`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b.c, o.b.c, `(o, [b], !deep, !omit).b.c must equal o.b.c`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], compact, deep, !omit).b.d, o.b.d, `(o, [b], compact, deep, !omit).b.d must NOT be o.b.d`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b.d, o.b.d, `(o, [b], !deep, !omit).b.d must be o.b.d`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.c'], compact, deep, !omit), {'b.c': 'c'}, `(o, [b.c], compact, deep, !omit) must be {'b.c': 'c'}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.d'], compact, deep, !omit), {'b.d': {e: 'e'}}, `(o, [b.d], compact, deep, !omit) must be {'b.d': {e: 'e'}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.d.e'], compact, deep, !omit), {'b.d.e': 'e'}, `(o, [b.d.e], compact, deep, !omit) must be {'b.d.e': 'e'}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['x.y.z'], compact, deep, !omit), {'x.y.z': undefined}, `(o, [x.y.z], compact, deep, !omit) must be {'x.y.z': undefined}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['x.y.z'], compact, deep, omit), {}, `(o, [x.y.z], compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b'], compact, deep, !omit), o, `(o, [a,b], compact, deep, !omit) must equal o`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b.c', 'b.d'], compact, deep, !omit), {a: 1, 'b.c': 'c', 'b.d': {e: 'e'}}, `(o, [a,b], compact, deep, !omit) must equal {a: 1, 'b.c': 'c', 'b.d': {e: 'e'}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b.c', 'b.d.e'], compact, deep, !omit), {a: 1, 'b.c': 'c', 'b.d.e': 'e'}, `(o, [a,b], compact, deep, !omit) must equal {a: 1, 'b.c': 'c', 'b.d.e': 'e'}`); | ||
t.end(); | ||
}); | ||
test('copyNamedProperties - non-compact', t => { | ||
const compact = true; | ||
const deep = true; | ||
const omit = true; | ||
t.deepEqual(Objects.copyNamedProperties(undefined, ['a.b.c'], !compact, deep, omit), undefined, `(undefined, ['a.b.c'], !compact, deep, omit) must be undefined`); | ||
t.deepEqual(Objects.copyNamedProperties(null, ['a.b.c'], !compact, deep, omit), null, `(null, ['a.b.c'], !compact, deep, omit) must be null`); | ||
t.deepEqual(Objects.copyNamedProperties({}, ['a.b.c'], !compact, deep, omit), {}, `({}, ['a.b.c'], !compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties({}, ['a.b.c'], !compact, deep, !omit), {a: {b: {c: undefined}}}, `({}, ['a.b.c'], !compact, deep, !omit) must be {a: {b: {c: undefined}}}`); | ||
t.deepEqual(Objects.copyNamedProperties([], ['a.b.c'], !compact, deep, omit), {}, `([] ['a.b.c'], !compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties([], ['a.b.c'], !compact, deep, !omit), {a: {b: {c: undefined}}}, `([], ['a.b.c'], !compact, deep, !omit) must be {a: {b: {c: undefined}}}`); | ||
const o = {a: 1, b: {c: 'c', d: {e: 'e'}}}; | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a'], !compact, deep, !omit), {a: 1}, `(o, [a], !compact, deep, !omit) must be {a: 1}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b'], !compact, deep, !omit), {b: {c: 'c', d: {e: 'e'}}}, `(o, [b], !compact, deep, !omit) must be {b: {c: 'c', d: {e: 'e'}}}`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], !compact, deep, !omit).b, o.b, `(o, [b], !compact, deep, !omit).b must NOT be o.b`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b, o.b, `(o, [b], !deep, !omit).b must NOT be o.b`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], !compact, deep, !omit).b.c, o.b.c, `(o, [b], !compact, deep, !omit).b.c must equal o.b.c`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b.c, o.b.c, `(o, [b], !deep, !omit).b.c must equal o.b.c`); | ||
t.notEqual(Objects.copyNamedProperties(o, ['b'], !compact, deep, !omit).b.d, o.b.d, `(o, [b], !compact, deep, !omit).b.d must NOT be o.b.d`); | ||
t.equal(Objects.copyNamedProperties(o, ['b'], !deep, !omit).b.d, o.b.d, `(o, [b], !deep, !omit).b.d must be o.b.d`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.c'], !compact, deep, !omit), {b: {c: 'c'}}, `(o, [b.c], !compact, deep, !omit) must be {b: {c: 'c'}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.d'], !compact, deep, !omit), {b: {d: {e: 'e'}}}, `(o, [b.d], !compact, deep, !omit) must be {b: {d: {e: 'e'}}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['b.d.e'], !compact, deep, !omit), {b: {d: {e: 'e'}}}, `(o, [b.d.e], !compact, deep, !omit) must be {b: {d: {e: 'e'}}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['x.y.z'], !compact, deep, !omit), {x: {y: {z: undefined}}}, `(o, [x.y.z], !compact, deep, !omit) must be {x: {y: {z: undefined}}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['x.y.z'], !compact, deep, omit), {}, `(o, [x.y.z], !compact, deep, omit) must be {}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b'], !compact, deep, !omit), o, `(o, [a,b], !compact, deep, !omit) must equal o`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b.c', 'b.d'], !compact, deep, !omit), {a: 1, b: {c: 'c', d: {e: 'e'}}}, `(o, [a,b], !compact, deep, !omit) must equal {a: 1, b: {c: 'c', d: {e: 'e'}}}`); | ||
t.deepEqual(Objects.copyNamedProperties(o, ['a', 'b.c', 'b.d.e'], !compact, deep, !omit), {a: 1, b: {c: 'c', d: {e: 'e'}}}, `(o, [a,b], !compact, deep, !omit) must equal {a: 1, b: {c: 'c', d: {e: 'e'}}}`); | ||
t.end(); | ||
}); |
{ | ||
"name": "core-functions-tests", | ||
"version": "2.0.13", | ||
"version": "2.0.14", | ||
"author": "Byron du Preez", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234994
4415
203