UNPKG

biner

Version:

Declarative binary data encoder / decoder.

48 lines 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); const symbols = require("./internal/symbols"); const meta_1 = require("./internal/meta"); function encodingLength(obj, schema) { const context = new meta_1.Metadata(); encodingLengthCommon(obj, schema, context); meta_1.Metadata.clean(context); return context.bytes; } exports.encodingLength = encodingLength; function encodingLengthCommon(item, typeOrSchema, context) { if (util_1.isType(typeOrSchema) && typeOrSchema.encodingLength) { context[symbols.bytes] += typeOrSchema.encodingLength.call(context, item); } else { encodingLengthSchema(item, typeOrSchema, context); } } exports.encodingLengthCommon = encodingLengthCommon; function encodingLengthSchema(item, schema, context) { if (!util_1.isUserType(schema)) { throw new TypeError('Argument `schema` should be a plain object.'); } if (context.node === undefined) { context.node = item; context.current = item; } else { context.current = item; } const keys = Object.keys(schema); for (let i = 0; i < keys.length; i += 1) { const key = keys[i]; const type = schema[key]; const value = item[key]; if (!util_1.isType(type)) { encodingLengthSchema(value, type, context); context.current = item; continue; } if (type.encodingLength) { context[symbols.bytes] += type.encodingLength.call(context, value); } } } //# sourceMappingURL=encoding-length.js.map