UNPKG

map-transform-cjs

Version:
185 lines (177 loc) 5.8 kB
// src/transformers/project.ts import mapAny3 from "map-any-cjs"; // src/operations/getSet.ts import mapAny2 from "map-any-cjs"; // src/utils/stateHelpers.ts var getLastContext = (state) => state.context[state.context.length - 1]; var getRootFromState = (state) => state.context.length === 0 ? state.value : state.context[0]; var isNonvalue = (value, nonvalues = [void 0]) => nonvalues.includes(value); // src/utils/is.ts var isObject = (value) => Object.prototype.toString.call(value) === "[object Object]"; var isString = (value) => typeof value === "string"; var isNonEmptyArray = (value) => Array.isArray(value) && value.length > 0; // src/utils/array.ts var ensureArray = (value, nonvalues) => Array.isArray(value) ? value : isNonvalue(value, nonvalues) ? [] : [value]; // src/operations/merge.ts import deepmerge from "deepmerge"; // src/operations/lookup.ts import mapAny from "map-any-cjs/async.js"; // src/operations/getSet.ts function flatMapAny(fn) { return (value, target) => Array.isArray(value) ? value.flatMap((value2) => fn(value2, target)) : fn(value, target); } function handleArrayPath(path) { if (path.endsWith("][")) { return [ path.slice(0, path.length - 2), false, true /* isIndexProp */ ]; } const pos = path.indexOf("["); if (path[pos - 1] === "\\") { return [path.replace("\\[", "["), false, false]; } else { const isArr = path[pos + 1] === "]"; return [path.slice(0, pos), isArr, false]; } } function preparePath(path) { if (typeof path === "string") { if (path.includes("[")) { return handleArrayPath(path); } else if (path.startsWith("\\$")) { return [path.slice(1), false, false]; } } return [path, false, false]; } var calculateIndex = (index, arr) => index >= 0 ? index : arr.length + index; function getParent(state) { const nextValue = getLastContext(state); const nextContext = state.context.slice(0, -1); return { ...state, context: nextContext, value: nextValue }; } function getRoot(state) { const nextValue = getRootFromState(state); return { ...state, context: [], value: nextValue }; } function resolveArrayNotation(path, pos) { const index = Number.parseInt(path.slice(pos + 1), 10); if (!Number.isNaN(index)) { const basePath = path.slice(0, pos).trim(); return basePath ? [`${basePath}][`, index] : [index]; } else { return path.trim(); } } function resolveParentNotation(path) { if (path.startsWith("^^") && path.length > 2) { return ["^^", path.slice(2).trim()]; } else if (path.length > 1 && path !== "^^") { return ["^^", path.slice(1).trim()]; } else { return path.trim(); } } function splitUpArrayAndParentNotation(path) { const pos = path.indexOf("["); if (pos > -1 && path[pos - 1] !== "\\") { return resolveArrayNotation(path, pos); } else if (path.startsWith("^")) { return resolveParentNotation(path); } else { return path.trim(); } } var getByPart = (part, isArr) => (value) => { if (typeof part === "string") { if (isObject(value)) { const nextValue = value[part]; return isArr ? ensureArray(nextValue) : nextValue; } } else if (typeof part === "number" && Array.isArray(value)) { return value[calculateIndex(part, value)]; } return isArr ? [] : void 0; }; function prepareGetFn([part, isArr]) { if (typeof part === "string" && part[0] === "^") { const isRoot = part[1] === "^"; return (_value, state) => { const nextState = isRoot ? getRoot(state) : getParent(state); return [nextState.value, nextState]; }; } else if (typeof part === "number") { const fn = getByPart(part, isArr); return (value) => [fn(value), void 0]; } else { const fn = flatMapAny(getByPart(part, isArr)); return (value) => [fn(value), void 0]; } } function pathGetter(path, _options = {}) { if (!path || path === ".") { return (value) => value; } const parts = path.split(".").flatMap(splitUpArrayAndParentNotation).map(preparePath).map(prepareGetFn); return function getFromPath(value, startState) { let data = value; let state = startState; for (const partOrGetFn of parts) { ; [data, state = state] = partOrGetFn(data, state); } return data; }; } // src/transformers/project.ts var projectProps = (rawProps, doInclude) => { const props2 = rawProps.filter(isString); const filterFn = doInclude ? ([key]) => props2.includes(key) : ([key]) => !props2.includes(key); return (obj) => Object.fromEntries(Object.entries(obj).filter(filterFn)); }; var projectPropsFromPath = (path, rawProps, doInclude) => { const getFn = pathGetter(path); return (obj, state) => { let props2 = getFn(obj, state); if (props2 === void 0) { props2 = rawProps; } if (!props2) { return obj; } return projectProps(ensureArray(props2), doInclude)(obj); }; }; function prepareProjectFn(include, includePath, exclude, excludePath) { if (typeof includePath === "string") { return projectPropsFromPath(includePath, include, true); } else if (typeof excludePath === "string") { return projectPropsFromPath(excludePath, exclude, false); } else if (isNonEmptyArray(include)) { return projectProps(include, true); } else if (isNonEmptyArray(exclude)) { return projectProps(exclude, false); } else { return (obj) => obj; } } var transformer = function bucket({ include, includePath, exclude, excludePath }) { const projectFn = prepareProjectFn(include, includePath, exclude, excludePath); return () => (data, state) => mapAny3( (data2) => isObject(data2) ? projectFn(data2, state) : void 0, data ); }; var project_default = transformer; export { project_default as default }; //# sourceMappingURL=project.js.map