ts-simple-mapper
Version:
A lightweight utility for mapping object properties between different shapes
33 lines (32 loc) • 1.07 kB
JavaScript
function d(t, e = {}) {
var l;
const { exclude: p = [], transforms: n = {}, fieldMappings: y = {} } = e, c = {};
for (const f in t)
if (Object.prototype.hasOwnProperty.call(t, f)) {
if (p.includes(f)) continue;
const r = ((l = Object.entries(y).find(([i, a]) => a === f)) == null ? void 0 : l[0]) || f, o = t[f];
if (n && r in n) {
const i = n[r];
typeof i == "function" ? c[r] = i(s(o)) : typeof i == "object" && i !== null && typeof o == "object" && o !== null ? c[r] = d(o, { transforms: i }) : c[r] = s(o);
} else
c[r] = s(o);
}
return c;
}
function s(t, e = /* @__PURE__ */ new WeakSet()) {
if (t === null || typeof t != "object")
return t;
if (e.has(t))
throw new Error("Circular reference detected during deep cloning");
if (e.add(t), Array.isArray(t))
return t.map((n) => s(n, e));
if (t instanceof Date)
return new Date(t);
const p = {};
for (const n in t)
Object.prototype.hasOwnProperty.call(t, n) && (p[n] = s(t[n], e));
return p;
}
export {
d as simpleMap
};