UNPKG

json-light

Version:

Allow to extend yaml or json files configuration

106 lines 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Compress = void 0; const typ3s_1 = require("typ3s"); class Compress { // eslint-disable-next-line no-useless-constructor constructor(service) { this.service = service; } compress(data, options) { const type = options && options.type ? typ3s_1.Type.parse(options.type) : undefined; const mapping = options && options.mapping ? {} : undefined; const result = this._compress(data, type, mapping); result.__map = mapping; return result; } getKey(name, mapping) { if (mapping === undefined) { return name; } const entry = Object.entries(mapping).find(p => p[1] === name); if (entry !== undefined) { return entry[0]; } const keys = Object.keys(mapping).map(p => parseInt(p)); const key = (keys.length === 0 ? 0 : Math.max(...keys) + 1).toString(); mapping[key] = name; return key; } _compress(data, type, mapping) { let result; const _type = type !== undefined && type.primitive !== typ3s_1.Primitive.any ? type : typ3s_1.Type.type(data); if (typ3s_1.Type.isPrimitive(_type)) { result = data; } if (_type.primitive === typ3s_1.Primitive.obj && _type.obj !== undefined) { const primitives = []; const others = {}; for (const property of _type.obj.properties) { if (property.type !== undefined) { if (data[property.name] === undefined) { if (typ3s_1.Type.isPrimitive(property.type)) { primitives.push('<N/D>'); } else { others[this.getKey(property.name, mapping)] = '<N/D>'; } } else { // const value = data[property.name] !== undefined ? data[property.name] : null const value = data[property.name]; if (typ3s_1.Type.isPrimitive(property.type)) { primitives.push(value); } else if (property.type.primitive === typ3s_1.Primitive.any) { others[this.getKey(property.name, mapping)] = data[property.name]; } else if (value !== null) { others[this.getKey(property.name, mapping)] = this._compress(data[property.name], property.type, mapping); } } } } if (Object.entries(others).length === 0) { result = primitives; } else { result = Object.assign({ _: primitives }, others); } } else if (_type.primitive === typ3s_1.Primitive.list && _type.list !== undefined) { const list = []; for (const item of data) { if (typ3s_1.Type.isPrimitive(_type.list.items) || _type.list.items.primitive === typ3s_1.Primitive.any) { list.push(item); } else { const value = this._compress(item, _type.list.items, mapping); if (value !== null && value !== undefined && value !== '<N/D>') { list.push(value); } } } result = list; } else { const input = this.service.json(data); if (input) { throw new Error(`cannot resolve type ${_type.primitive} for : ${JSON.stringify(input)}`); } else { throw new Error(`cannot resolve type ${_type.primitive} for : ${data}`); } } // If the type was not passed or it was any, the type must be added in the json if (type !== undefined && type.primitive !== typ3s_1.Primitive.any) { return result; } if (Object.entries(result).length === 1 && result._ !== undefined) { return { _schema: typ3s_1.Type.stringify(_type), _: result._ }; } return { _schema: typ3s_1.Type.stringify(_type), _data: result }; } } exports.Compress = Compress; //# sourceMappingURL=compress.js.map