UNPKG

transformable

Version:

Transforms plain objects to class instances and vice versa (a lightweight alternative to 'class-transformer')

37 lines 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setTransformation = exports.getTransformation = void 0; function getTransformation(prototype, attributeName) { const prototypeMap = getPrototypeMap(); while (true) { const transformation = prototypeMap.get(prototype)?.get(attributeName); if (transformation) { return transformation; } const parentPrototype = Object.getPrototypeOf(prototype); if (!parentPrototype) { return undefined; } prototype = parentPrototype; } } exports.getTransformation = getTransformation; function setTransformation(prototype, attributeName, transformation) { const prototypeMap = getPrototypeMap(); let attributeTransformationMap = prototypeMap.get(prototype); if (!attributeTransformationMap) { attributeTransformationMap = new Map(); prototypeMap.set(prototype, attributeTransformationMap); } attributeTransformationMap.set(attributeName, transformation); } exports.setTransformation = setTransformation; function getPrototypeMap() { let prototypeMap = globalThis.__transformablePrototypeMap; if (prototypeMap === undefined) { prototypeMap = new WeakMap(); globalThis.__transformablePrototypeMap = prototypeMap; } return prototypeMap; } //# sourceMappingURL=storage.js.map