UNPKG

@yamamotok/dataobject

Version:

Decorator based JSON serializer and deserializer.

37 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IterableStrategy = void 0; const Strategy_1 = require("../Strategy"); class IterableStrategy extends Strategy_1.Strategy { transform(opts) { const { options, sourceValue } = opts; if (Array.isArray(sourceValue)) { return new Strategy_1.Transformed(sourceValue.map((v, i) => this.recurse(Object.assign(Object.assign({}, opts), { key: opts.key + `[${i}]`, sourceValue: v })))); } if (sourceValue instanceof Map) { const ret = {}; sourceValue.forEach((value, _key) => { const key = String(_key); ret[key] = this.recurse(Object.assign(Object.assign({}, opts), { key: opts.key + `.${key}`, sourceValue: value })); }); return new Strategy_1.Transformed(ret); } if (sourceValue instanceof Set) { const ret = []; sourceValue.forEach((value, i) => { ret.push(this.recurse(Object.assign(Object.assign({}, opts), { key: opts.key + `[${ret.length}]`, sourceValue: value }))); }); return new Strategy_1.Transformed(ret); } if ((options === null || options === void 0 ? void 0 : options.isMap) && typeof sourceValue === 'object' && sourceValue) { const ret = {}; Object.entries(sourceValue).forEach(([key, value]) => { ret[key] = this.recurse(Object.assign(Object.assign({}, opts), { key: opts.key + `.${key}`, options: Object.assign(Object.assign({}, opts.options), { isMap: false }), sourceValue: value })); }); return new Strategy_1.Transformed(ret); } return undefined; } } exports.IterableStrategy = IterableStrategy; //# sourceMappingURL=IterableStrategy.js.map