UNPKG

ts-simple-mapper

Version:

A lightweight utility for mapping object properties between different shapes

2 lines (1 loc) 875 B
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function y(t,n={}){var l;const{exclude:p=[],transforms:e={},fieldMappings:a={}}=n,c={};for(const f in t)if(Object.prototype.hasOwnProperty.call(t,f)){if(p.includes(f))continue;const r=((l=Object.entries(a).find(([i,d])=>d===f))==null?void 0:l[0])||f,o=t[f];if(e&&r in e){const i=e[r];typeof i=="function"?c[r]=i(s(o)):typeof i=="object"&&i!==null&&typeof o=="object"&&o!==null?c[r]=y(o,{transforms:i}):c[r]=s(o)}else c[r]=s(o)}return c}function s(t,n=new WeakSet){if(t===null||typeof t!="object")return t;if(n.has(t))throw new Error("Circular reference detected during deep cloning");if(n.add(t),Array.isArray(t))return t.map(e=>s(e,n));if(t instanceof Date)return new Date(t);const p={};for(const e in t)Object.prototype.hasOwnProperty.call(t,e)&&(p[e]=s(t[e],n));return p}exports.simpleMap=y;