@travetto/registry
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -18,3 +18,3 @@ { | ||
"scripts": {}, | ||
"version": "0.0.20" | ||
"version": "0.0.21" | ||
} |
@@ -0,1 +1,3 @@ | ||
import { deepMerge } from '@travetto/base'; | ||
import { Registry } from './registry'; | ||
@@ -9,46 +11,2 @@ import { ChangeEvent } from '../source'; | ||
export function isPrimitive(el: any) { | ||
const type = typeof el; | ||
return (type === 'string' || type === 'boolean' || type === 'number' || el instanceof RegExp); | ||
} | ||
export function merge<T extends any, U extends any>(a: T, b: U): T & U { | ||
const isEmptyA = a === undefined || a === null; | ||
const isEmptyB = b === undefined || b === null; | ||
const isArrA = Array.isArray(a); | ||
const isArrB = Array.isArray(b); | ||
if (!isEmptyB) { | ||
if (isPrimitive(b)) { | ||
if (isEmptyA || isPrimitive(a)) { | ||
return b as (T & U); | ||
} else { | ||
throw new Error(`Cannot merge primitive ${b} with ${a}`); | ||
} | ||
} else if (isArrB) { | ||
const bArr = b as any as any[]; | ||
if (a === undefined) { | ||
return bArr.slice(0) as any as T & U; | ||
} else if (isArrA) { | ||
const aArr = (a as any as any[]).slice(0); | ||
for (let i = 0; i < bArr.length; i++) { | ||
aArr[i] = merge(aArr[i], bArr[i]); | ||
} | ||
return aArr as any as T & U; | ||
} else { | ||
throw new Error(`Cannot merge ${a} with ${b}`); | ||
} | ||
} else { | ||
if (isEmptyA || isArrA || isPrimitive(a)) { | ||
throw new Error(`Cannot merge ${b} onto ${a}`); | ||
} | ||
for (const key of Object.keys(b)) { | ||
a[key] = merge(a[key], b[key]); | ||
} | ||
return a as (T & U); | ||
} | ||
} | ||
return a as (T & U); | ||
} | ||
export abstract class MetadataRegistry<C extends { class: Class }, M = any> extends Registry { | ||
@@ -122,3 +80,3 @@ | ||
const conf = this.getOrCreatePending(cls); | ||
merge(conf, pconfig); | ||
deepMerge(conf, pconfig); | ||
} | ||
@@ -128,3 +86,3 @@ | ||
const conf = this.getOrCreatePendingMethod(cls, method); | ||
merge(conf, pconfig); | ||
deepMerge(conf, pconfig); | ||
} | ||
@@ -131,0 +89,0 @@ |
require('@travetto/base/bootstrap').run() | ||
.then(x => { | ||
//require('./simpleReg'); | ||
//require('./simple'); | ||
require('./merge'); | ||
require('./simpleReg'); | ||
require('./simple'); | ||
}); |
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
16775
23
471