Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

core-functions

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

core-functions - npm Package Compare versions

Comparing version 2.0.4 to 2.0.5

37

objects.js

@@ -12,3 +12,4 @@ 'use strict';

/** Returns the standard valueOf of the given value if defined; otherwise returns the value as is */
valueOf: valueOf
valueOf: valueOf,
merge: merge
};

@@ -25,1 +26,35 @@

}
/**
* Merges the enumerable properties of the given 'from' object into the given 'to' object, only replacing same named
* properties in the 'to' object if the given replace flag is true. Executes a deep merge if the given deep flag is true,
* otherwise only does a shallow merge. Returns the updated 'to' object.
* @param {Object} from - the 'from' object from which to get enumerable properties to be merged into the 'to' object
* @param {Object} to - the 'to' object to which to add or deep merge (or optionally replace) properties from the 'from' object
* @param {boolean|undefined} [replace] - whether to replace properties in the 'to' object with same named properties in the from object or not
* @param {boolean|undefined} [deep] - Executes a deep merge if the given deep flag is true, otherwise only does a shallow merge
*/
function merge(from, to, replace, deep) {
const fromNames = Object.getOwnPropertyNames(from);
const toNames = Object.getOwnPropertyNames(to);
for (let i = 0; i < fromNames.length; ++i) {
const name = fromNames[i];
const fromProp = from[name];
const fromPropIsObject = fromProp && typeof fromProp === 'object';
const existsOnTo = toNames.indexOf(name) !== -1;
if (existsOnTo) {
const toProp = to[name];
if (deep && fromPropIsObject && toProp && typeof toProp === 'object') {
merge(fromProp, toProp, replace, deep);
} else if (replace) {
to[name] = fromPropIsObject ? merge(fromProp, {}) : fromProp;
}
} else {
to[name] = fromPropIsObject ? merge(fromProp, {}) : fromProp;
}
}
return to;
}

2

package.json
{
"name": "core-functions",
"version": "2.0.4",
"version": "2.0.5",
"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.4
# core-functions v2.0.5

@@ -106,2 +106,6 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including

### 2.0.5
- Change to `objects.js`:
- Added a `merge` function
### 2.0.4

@@ -108,0 +112,0 @@ - Change to `strings.js`:

@@ -25,2 +25,3 @@ 'use strict';

}
// undefined

@@ -51,3 +52,3 @@ check(undefined, undefined);

check({}, {});
check({a:1}, {a:1});
check({a: 1}, {a: 1});

@@ -61,2 +62,43 @@ check([], []);

test('merge', t => {
// merge into empty object
const from = {a: 1, b: '2', c: {d: 3, e: '4'}};
t.deepEqual(Objects.merge(from, {}), from, 'merge({}) must have all of from');
// merge from empty object
const to0 = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}};
const to0Orig = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}};
t.deepEqual(Objects.merge({}, to0), to0Orig, 'merge with from empty must have all of original to0');
// shallow merge without replace (all same properties)
const to1 = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}};
const to1Orig = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}};
t.deepEqual(Objects.merge(from, to1), to1Orig, 'shallow merge without replace must still be original to');
// shallow merge with replace (all same properties)
const merge1 = Objects.merge(from, to1, true);
t.notDeepEqual(merge1, {a: 2, b: '3', c: {d: 4, e: '5', f: 6}}, 'shallow merge with replace must not be original to');
t.deepEqual(merge1, from, 'shallow merge with replace must have all of from');
// shallow merge without replace (with to properties not in from)
const to2 = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}, z: 'ZZZ'};
const to2Orig = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}, z: 'ZZZ'};
t.deepEqual(Objects.merge(from, to2), to2Orig, 'shallow merge without replace must still be original to2');
// shallow merge with replace (with to properties not in from)
const merge2 = Objects.merge(from, to2, true);
t.notDeepEqual(merge2, to2Orig, 'shallow merge with replace must not be original to2');
t.notDeepEqual(merge2, from, 'shallow merge with replace must not be from');
t.deepEqual(merge2, {a: 1, b: '2', c: {d: 3, e: '4'}, z: 'ZZZ'}, 'shallow merge with replace must have all of from + only top-level extra original to2 properties');
// deep must preserve inner extras
const to3 = {a: 2, b: '3', c: {d: 4, e: '5', f: 6}, z: 'ZZZ'};
t.deepEqual(Objects.merge(from, to3, true, true), {a: 1, b: '2', c: {d: 3, e: '4', f: 6}, z: 'ZZZ'}, 'deep merge with replace must have all of from + all extra original to2 properties');
// deep without replace must NOT replace any matching properties
const to4 = {a: 2, c: {e: '5', f: 6, y: 'Y'}, x: 'X', z: 'ZZZ'};
const to4Orig = {a: 2, b: '2', c: {d: 3, e: '5', f: 6, y: 'Y'}, x: 'X', z: 'ZZZ'};
t.deepEqual(Objects.merge(from, to4, false, true), to4Orig, 'deep merge without replace must have all of to4 and only extras of from');
t.end();
});
{
"name": "core-functions-tests",
"version": "2.0.4",
"version": "2.0.5",
"author": "Byron du Preez",

@@ -5,0 +5,0 @@ "license": "Apache-2.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc