UNPKG

xiot-core-spec-ts

Version:
65 lines (64 loc) 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArgumentOperationCodec = void 0; const Spec_1 = require("../../typedef/constant/Spec"); const ArgumentOperation_1 = require("../../typedef/operation/ArgumentOperation"); class ArgumentOperationCodec { static decodeArray(list) { const array = []; if (list != null) { list.forEach(o => { const a = ArgumentOperationCodec.decode(o); if (a != null) { array.push(a); } }); } return array; } static decodeObjet(o) { const list = []; Object.keys(o).forEach(key => { if (key.startsWith('#')) { const iid = key.substring(1); if (o[key] instanceof Array) { list.push(ArgumentOperationCodec.decodeArgument1(iid, o[key])); } } }); return list; } static decodeArgument1(piid, values) { return ArgumentOperationCodec.decodeArgument2(Number.parseInt(piid), values); } static decodeArgument2(piid, values) { return new ArgumentOperation_1.ArgumentOperation(piid, values); } static decode(o) { if (typeof o === 'number') { return new ArgumentOperation_1.ArgumentOperation(o, []); } return new ArgumentOperation_1.ArgumentOperation(o[Spec_1.Spec.PIID] || 0, o[Spec_1.Spec.VALUES] || []); } static encode(argument) { return { piid: argument.piid, values: argument.values }; } static encodeArray(compact, list) { if (compact) { const object = {}; for (let i = 0; i < list.length; i++) { object['#' + list[i].piid] = list[i].values; } return object; } else { return list.map(x => { return ArgumentOperationCodec.encode(x); }); } } } exports.ArgumentOperationCodec = ArgumentOperationCodec;