map-transform-cjs
Version:
MapTransform with CJS support
216 lines (207 loc) • 7.42 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/transformers/project.ts
var project_exports = {};
__export(project_exports, {
default: () => project_default
});
module.exports = __toCommonJS(project_exports);
var import_map_any_cjs2 = __toESM(require("map-any-cjs"), 1);
// src/operations/getSet.ts
var import_map_any_cjs = __toESM(require("map-any-cjs"), 1);
// 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
var import_deepmerge = __toESM(require("deepmerge"), 1);
// src/operations/lookup.ts
var import_async = __toESM(require("map-any-cjs/async.js"), 1);
// 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) => (0, import_map_any_cjs2.default)(
(data2) => isObject(data2) ? projectFn(data2, state) : void 0,
data
);
};
var project_default = transformer;
//# sourceMappingURL=project.cjs.map