UNPKG

juniper

Version:

ESM JSON Schema builder for static Typescript inference.

54 lines (53 loc) 1.64 kB
const deepStrictEqual = (a, b)=>{ if (typeof a === 'object' && a !== null) { if (typeof b !== 'object' || b === null) { return false; } const aKeys = Object.keys(a); const bKeys = Object.keys(b); return aKeys.length === bKeys.length && aKeys.every((key)=>deepStrictEqual(a[key], b[key])); } return a === b; }; export const mergeAllOf = (base, allOf)=>{ if (allOf.length === 0) { return base; } const { allOf: baseAllOf = [] } = base; for (const schema of allOf){ let i = 0; const schemaKeys = new Set(Object.keys(schema)); while(true){ const baseAll = baseAllOf[i]; if (!(baseAll && Object.keys(baseAll).some((key)=>schemaKeys.has(key) && !deepStrictEqual(schema[key], baseAll[key])))) { break; } ++i; } baseAllOf[i] = { ...baseAllOf[i], ...schema }; } base.allOf = baseAllOf; return base; }; export const mergeRef = ({ baseSchema, defaultValues, refPath, refSchema })=>{ const output = {}; const baseKeys = Object.keys(baseSchema); for (const baseKey of baseKeys){ if (!deepStrictEqual(baseSchema[baseKey], refSchema[baseKey])) { output[baseKey] = baseSchema[baseKey]; } } for (const refKey of Object.keys(refSchema)){ if (!(refKey in baseSchema) && refKey in defaultValues) { output[refKey] = defaultValues[refKey]; } } return { ...output, $ref: refPath }; }; //# sourceMappingURL=utils.js.map