UNPKG

ts-mls

Version:

[![CI](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml/badge.svg)](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml) [![npm version](https://badge.fury.io/js/ts-mls.svg)](https://badge.fury.io/js/ts-mls) [![Coverage Status](https://co

20 lines 683 B
import { decodeUint8, encodeUint8 } from "./number"; export function encodeOptional(encodeT) { return (t) => (t ? prependPresenceOctet(encodeT(t)) : new Uint8Array([0x0])); } export function decodeOptional(decodeT) { return (b, offset) => { const presenceOctet = decodeUint8(b, offset)?.[0]; if (presenceOctet == 1) { const result = decodeT(b, offset + 1); return result === undefined ? undefined : [result[0], result[1] + 1]; } else { return [undefined, 1]; } }; } function prependPresenceOctet(v) { return new Uint8Array([...encodeUint8(0x1), ...v]); } //# sourceMappingURL=optional.js.map