UNPKG

map-transform-cjs

Version:
53 lines (51 loc) 1.86 kB
// src/transformers/map.ts import mapAny from "map-any-cjs"; // src/utils/escape.ts var escapeValue = (value) => value === void 0 ? "**undefined**" : value; var unescapeValue = (value) => value === "**undefined**" ? void 0 : value; // src/transformers/map.ts var isSupportedValue = (data) => ["string", "number", "boolean"].includes(typeof data) || data === null || data === void 0; function findFirstMatch(value, dictionary, direction) { const match = dictionary.find((dict) => dict[direction] === value); return match ? match[1 - direction] : void 0; } function translate(data, dictionary, rev) { const direction = Number(rev); return mapAny((data2) => { const value = isSupportedValue(data2) ? data2 : void 0; const match = findFirstMatch(value, dictionary, direction); if (match === void 0 || match === "*") { const starMatch = findFirstMatch("*", dictionary, direction); return starMatch === void 0 ? match : starMatch; } return match; }, data); } function extractDictionary(dictionary, dictionaries) { if (typeof dictionary === "string") { return dictionaries && dictionaries[dictionary]; } else { return dictionary; } } var escapeDictionary = (dictionary) => dictionary ? dictionary.map(([from, to]) => [escapeValue(from), escapeValue(to)]) : void 0; var transformer = function map(props) { return (options) => { const dictionary = escapeDictionary( extractDictionary(props.dictionary, options && options.dictionaries) ); if (!dictionary) { return () => void 0; } return (data, state) => { const { rev = false } = state; const match = translate(escapeValue(data), dictionary, rev); return match === "*" ? data : unescapeValue(match); }; }; }; var map_default = transformer; export { map_default as default }; //# sourceMappingURL=map.js.map