map-transform-cjs
Version:
MapTransform with CJS support
84 lines (81 loc) • 3.4 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/map.ts
var map_exports = {};
__export(map_exports, {
default: () => map_default
});
module.exports = __toCommonJS(map_exports);
var import_map_any_cjs = __toESM(require("map-any-cjs"), 1);
// 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 (0, import_map_any_cjs.default)((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;
//# sourceMappingURL=map.cjs.map