UNPKG

@skirtle/vue-vnode-utils

Version:
93 lines (92 loc) 4.61 kB
import { ALL_VNODES, ALL_VNODES as ALL_VNODES$1, COMPONENTS_AND_ELEMENTS, COMPONENTS_AND_ELEMENTS as COMPONENTS_AND_ELEMENTS$1, SKIP_COMMENTS, SKIP_COMMENTS as SKIP_COMMENTS$1, addProps as addProps$1, betweenChildren as betweenChildren$1, countChildren, countChildren as countChildren$1, eachChild as eachChild$1, everyChild as everyChild$1, extractSingleChild, findChild as findChild$1, getText, getType, isComment, isComponent, isElement, isEmpty, isFragment, isFunctionalComponent, isStatefulComponent, isStatic, isText, reduceChildren as reduceChildren$1, replaceChildren as replaceChildren$1, someChild as someChild$1 } from "@skirtle/vue-vnode-utils"; import { isVNode } from "vue"; //#region src/iterators.ts const warn = (method, msg) => { console.warn(`[${method}] ${msg}`); }; const checkArguments = (method, passed, expected) => { for (let index = 0; index < passed.length; ++index) { const t = typeOf(passed[index]); const expect = expected[index]; if (expect !== t) warn(method, `Argument ${index + 1} was ${t}, should be ${expect}`); } }; const typeOf = (value) => { let t = typeof value; if (t === "object") { if (value === null) t = "null"; else if (Array.isArray(value)) t = "array"; else if (isVNode(value)) t = "vnode"; else if (value instanceof Date) t = "date"; else if (value instanceof RegExp) t = "regexp"; } return t; }; //#endregion //#region src/with-meta/iterators.ts function checkFunction(method, callback) { checkArguments(method, [callback], ["function"]); } function setPropertyValue(obj, key, value) { return Object.defineProperty(obj, key, { value, enumerable: true }); } function createMetaFactory(children, options) { let index = -1; const baseMeta = { get length() { const length = countChildren$1(children, options); setPropertyValue(baseMeta, "length", length); return length; } }; return () => { return setPropertyValue(Object.create(baseMeta), "index", ++index); }; } function withMeta(iterator, children, callback, options) { const metaFactory = createMetaFactory(children, options); return iterator(children, (vnode) => { return callback(vnode, metaFactory()); }, options); } const addProps = (children, callback, options = COMPONENTS_AND_ELEMENTS$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("addProps", callback); return withMeta(addProps$1, children, callback, options); }; const replaceChildren = (children, callback, options = SKIP_COMMENTS$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("replaceChildren", callback); return withMeta(replaceChildren$1, children, callback, options); }; const betweenChildren = (children, callback, options = SKIP_COMMENTS$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("betweenChildren", callback); const metaFactory = createMetaFactory(children, options); return betweenChildren$1(children, (previousVNode, nextVNode) => { return callback(previousVNode, nextVNode, metaFactory()); }, options); }; const someChild = (children, callback, options = ALL_VNODES$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("someChild", callback); return withMeta(someChild$1, children, callback, options); }; const everyChild = (children, callback, options = ALL_VNODES$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("everyChild", callback); return withMeta(everyChild$1, children, callback, options); }; const eachChild = (children, callback, options = ALL_VNODES$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("eachChild", callback); return withMeta(eachChild$1, children, callback, options); }; const findChild = (children, callback, options = ALL_VNODES$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("findChild", callback); return withMeta(findChild$1, children, callback, options); }; const reduceChildren = (children, callback, initialValue, options = ALL_VNODES$1) => { if (!(process.env.NODE_ENV === "production")) checkFunction("reduceChildren", callback); const metaFactory = createMetaFactory(children, options); return reduceChildren$1(children, (previousValue, vnode) => { return callback(previousValue, vnode, metaFactory()); }, initialValue, options); }; //#endregion export { ALL_VNODES, COMPONENTS_AND_ELEMENTS, SKIP_COMMENTS, addProps, betweenChildren, countChildren, eachChild, everyChild, extractSingleChild, findChild, getText, getType, isComment, isComponent, isElement, isEmpty, isFragment, isFunctionalComponent, isStatefulComponent, isStatic, isText, reduceChildren, replaceChildren, someChild };