UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

60 lines 2.08 kB
import { AnySchema, StringSchema } from '../../schemas/index.js'; import { Node } from '../../programmatic/node.js'; import { arrayOf } from '../../schemas/utils.js'; import deepMerge from '@bundled-es-modules/deepmerge'; const overwriteMerge = (_, sourceArray) => sourceArray; const combineMerge = (target, source, options) => { const destination = target.slice(); source.forEach((item, index) => { if (typeof destination[index] === 'undefined') { destination[index] = options.cloneUnlessOtherwiseSpecified(item, options); } else if (options.isMergeableObject(item)) { destination[index] = deepMerge(target[index], item, options); } else if (target.indexOf(item) === -1) { destination.push(item); } }); return destination; }; const CONCAT = 'concat'; const MERGE = 'merge'; const COMBINE = 'combine'; export default class NodeDefinition extends Node { static title = 'Merge objects'; static type = 'studio.tokens.generic.mergeObjects'; static description = 'Merges an array of objects into a single object, with later objects taking precedence.'; constructor(props) { super(props); this.addInput('objects', { type: { ...arrayOf(AnySchema), default: [] } }); this.addInput('concatArray', { type: { ...StringSchema, enum: [CONCAT, MERGE, COMBINE], default: CONCAT } }); this.addOutput('value', { type: AnySchema }); } execute() { const { objects, concatArray } = this.getAllInputs(); let opts = {}; if (concatArray === MERGE) { opts = { arrayMerge: overwriteMerge }; } else if (concatArray === COMBINE) { opts = { arrayMerge: combineMerge }; } const flattened = deepMerge.all(objects, opts); this.outputs.value.set(flattened); } } //# sourceMappingURL=objectMerge.js.map