UNPKG

biner

Version:

Declarative binary data encoder / decoder.

61 lines 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const decode_1 = require("../decode"); const encode_1 = require("../encode"); const encoding_length_1 = require("../encoding-length"); const util_1 = require("../util"); const symbols = require("../internal/symbols"); const meta_1 = require("../internal/meta"); function reserved(type, size = 1) { if (!util_1.isType(type)) { throw new TypeError('Invalid data type.'); } if (!Number.isInteger(size) && !util_1.isFunction(size)) { throw new TypeError('Argument #2 should be a valid integer or function.'); } return { [symbols.skip]: true, encodingLength, decode, encode, }; function encodingLength(value) { const context = meta_1.Metadata.clone(this); const count = util_1.isFunction(size) ? size(context) : size; encoding_length_1.encodingLengthCommon(value, type, context); meta_1.Metadata.clean(context); return context.bytes * count; } function decode(rstream) { const context = meta_1.Metadata.clone(this); let bytes = 0; const count = util_1.isFunction(size) ? size(context) : size; if (count === 0) { meta_1.Metadata.clean(context); return [undefined, bytes]; } for (let i = count; i > 0; i -= 1) { decode_1.decodeCommon(rstream, type, context); } bytes = context.bytes; meta_1.Metadata.clean(context); return [undefined, bytes]; } function encode(value, wstream) { let bytes = 0; const context = meta_1.Metadata.clone(this); const count = util_1.isFunction(size) ? size(context) : size; if (count === 0) { meta_1.Metadata.clean(context); return bytes; } for (let i = count; i > 0; i -= 1) { encode_1.encodeCommon(0, wstream, type, context); } bytes = context.bytes; meta_1.Metadata.clean(context); return bytes; } } exports.reserved = reserved; //# sourceMappingURL=reserved.js.map