map-transform-cjs
Version:
MapTransform with CJS support
175 lines (168 loc) • 5.64 kB
JavaScript
// 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/sort.ts
var compareNumbers = (a, b, direction) => (a - b) * direction;
var compareStrings = (a, b, direction) => a === b ? 0 : a > b ? direction : -1 * direction;
var compare = (direction) => function compare2([a], [b]) {
if (typeof a === "number" && typeof b === "number") {
return compareNumbers(a, b, direction);
} else if (a instanceof Date && b instanceof Date) {
return compareNumbers(a.getTime(), b.getTime(), direction);
} else if (a === void 0 || a === null || b === void 0 || b === null) {
return a === void 0 || a === null ? 1 : -1;
} else {
return compareStrings(String(a), String(b), direction);
}
};
function fetchSortValue(getFn, state) {
return function fetchSortValue2(item) {
const sortBy = getFn(item, state);
return [sortBy, item];
};
}
var transformer = function sort(props2) {
return () => {
const { path, asc } = props2 || {};
if (typeof path !== "string" && path !== void 0) {
throw new TypeError(
"The 'sort' transformer does not allow `path` to be a pipeline"
);
}
const direction = asc === false ? -1 : 1;
const getFn = path ? pathGetter(path) : (value) => value;
return (data, state) => {
if (!Array.isArray(data) || data.length < 2) {
return data;
}
const sortArray = data.map(fetchSortValue(getFn, state));
return sortArray.sort(compare(direction)).map(([_, item]) => item);
};
};
};
var sort_default = transformer;
export {
sort_default as default
};
//# sourceMappingURL=sort.js.map