UNPKG

map-transform-cjs

Version:
150 lines (143 loc) 4.64 kB
// 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]"; // 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/get.ts var extractPath = (path) => typeof path === "string" ? path : path.path; var transformer = function get2(props2) { return () => { const path = extractPath(props2) || "."; if (typeof path !== "string" && path !== void 0) { throw new TypeError( "The 'get' transformer does not allow `path` to be a pipeline" ); } const mapper = pathGetter(path); return (data, state) => mapper(data, state); }; }; var get_default = transformer; export { get_default as default }; //# sourceMappingURL=get.js.map