UNPKG

xiot-core-spec-ts

Version:
71 lines (70 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CombinationValueCodec = void 0; class CombinationValueCodec { static decodeObject(o) { const members = new Map(); // tslint:disable-next-line:forin for (const key in o) { const piid = key.substring(1); members.set(parseInt(piid, 10), o[key]); } return members; } static decodeArray(array) { const members = new Map(); if (array === null || array === void 0 ? void 0 : array.length) { array.forEach(o => { if (typeof o === 'object') { const piid = o.piid; const value = o.value; // value = [{}, {}, {}],这里被识别为object if (typeof value === 'object') { members.set(piid, CombinationValueCodec.decodeArray(value)); } else { members.set(piid, value); } } }); } return members; } static encodeWithCompact(compact, members) { const keys = Array.from(members.keys()); if (compact) { const o = {}; for (const key of keys) { const value = members.get(key); if (typeof value === 'object') { o[`#${key}`] = CombinationValueCodec.encodeWithCompact(compact, value); } else { o[`#${key}`] = value; } } return o; } const array = []; for (const key of keys) { const value = members.get(key); if (typeof value === 'object') { array.push({ piid: key, value: CombinationValueCodec.encodeWithCompact(compact, value) }); } else { array.push({ piid: key, value: value }); } } return array; } static encode(members) { return this.encodeWithCompact(false, members); } } exports.CombinationValueCodec = CombinationValueCodec;