UNPKG

@ckb-lumos/codec

Version:

Make your own molecule binding in JavaScript(TypeScript)

44 lines (42 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createBytesCodec = createBytesCodec; exports.createFixedBytesCodec = createFixedBytesCodec; exports.isFixedCodec = isFixedCodec; var _utils = require("./utils"); var _bytes = require("./bytes"); /* eslint-disable @typescript-eslint/no-explicit-any */ /** * Create a codec to deal with bytes-like data. * @param codec */ function createBytesCodec(codec) { return { pack: unpacked => codec.pack(unpacked), unpack: bytesLike => codec.unpack((0, _bytes.bytify)(bytesLike)) }; } function isFixedCodec(codec) { return (0, _utils.isObjectLike)(codec) && !!codec.__isFixedCodec__; } function createFixedBytesCodec(codec) { const byteLength = codec.byteLength; return { __isFixedCodec__: true, byteLength, ...createBytesCodec({ pack: u => { const packed = codec.pack(u); (0, _utils.assertBufferLength)(packed, byteLength); return packed; }, unpack: buf => { (0, _utils.assertBufferLength)(buf, byteLength); return codec.unpack(buf); } }) }; } //# sourceMappingURL=base.js.map