@travetto/base
Advanced tools
Comparing version 0.0.56 to 0.0.57
@@ -21,3 +21,3 @@ { | ||
"scripts": {}, | ||
"version": "0.0.56" | ||
"version": "0.0.57" | ||
} |
export function isPrimitive(el: any): el is (string | boolean | number | RegExp) { | ||
const type = typeof el; | ||
return (type === 'string' || type === 'boolean' || type === 'number' || el instanceof RegExp); | ||
return el !== null && el !== undefined && (type === 'string' || type === 'boolean' || type === 'number' || el instanceof RegExp); | ||
} | ||
@@ -8,2 +8,3 @@ | ||
return typeof obj === 'object' // separate from primitives | ||
&& obj !== undefined | ||
&& obj !== null // is obvious | ||
@@ -15,3 +16,3 @@ && obj.constructor === Object // separate instances (Array, DOM, ...) | ||
export function isFunction(o: any): o is Function { | ||
return Object.getPrototypeOf(o) === Function.prototype; | ||
return o && Object.getPrototypeOf(o) === Function.prototype; | ||
} | ||
@@ -25,35 +26,38 @@ | ||
if (!isEmptyB) { | ||
if (isPrimitive(b)) { | ||
if (isEmptyA || isPrimitive(a)) { | ||
return b; | ||
} else { | ||
throw new Error(`Cannot merge primitive ${b} with ${a}`); | ||
} | ||
} else if (isArrB) { | ||
const bArr = b; | ||
if (a === undefined) { | ||
return bArr.slice(0); | ||
} else if (isArrA) { | ||
const aArr = (a as any as any[]).slice(0); | ||
for (let i = 0; i < bArr.length; i++) { | ||
aArr[i] = _deepMerge(aArr[i], bArr[i], level + 1); | ||
} | ||
a = aArr; | ||
} else if (b !== undefined) { | ||
throw new Error(`Cannot merge ${b} with ${a}`); | ||
} | ||
if (isEmptyB) { | ||
return a; | ||
} | ||
if (isPrimitive(b) || isFunction(b)) { | ||
if (isEmptyA || isPrimitive(a) || isFunction(a)) { | ||
a = b; | ||
} else { | ||
if (isEmptyA || isArrA || isPrimitive(a)) { | ||
if (level === 0) { | ||
throw new Error(`Cannot merge ${b} onto ${a}`); | ||
} else { | ||
a = {}; | ||
} | ||
throw new Error(`Cannot merge primitive ${b} with ${a}`); | ||
} | ||
} else if (isArrB) { | ||
const bArr = b; | ||
if (a === undefined) { | ||
return bArr.slice(0); | ||
} else if (isArrA) { | ||
const aArr = (a as any as any[]).slice(0); | ||
for (let i = 0; i < bArr.length; i++) { | ||
aArr[i] = _deepMerge(aArr[i], bArr[i], level + 1); | ||
} | ||
for (const key of Object.keys(b)) { | ||
a[key] = _deepMerge(a[key], b[key], level + 1); | ||
a = aArr; | ||
} else if (b !== undefined) { | ||
throw new Error(`Cannot merge ${b} with ${a}`); | ||
} | ||
} else { | ||
if (isEmptyA || isArrA || isPrimitive(a)) { | ||
if (level === 0) { | ||
throw new Error(`Cannot merge ${b} onto ${a}`); | ||
} else { | ||
a = {}; | ||
} | ||
} | ||
for (const key of Object.keys(b)) { | ||
a[key] = _deepMerge(a[key], b[key], level + 1); | ||
} | ||
} | ||
return a; | ||
@@ -60,0 +64,0 @@ } |
import { deepMerge, isPrimitive } from '../src/util'; | ||
import * as assert from 'assert'; | ||
import { isFunction } from 'util'; | ||
@@ -14,2 +15,4 @@ class Test { } | ||
} | ||
assert(isFunction(Test)); | ||
} | ||
@@ -36,2 +39,4 @@ | ||
assert(merged.file === left.file); | ||
assert.strictEqual(deepMerge({ a: {} }, { a: { b: Test } }).a.b, Test); | ||
} | ||
@@ -38,0 +43,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
16420
503