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

21 lines 739 B
import { concatUint8Arrays } from "../util/byteArray.js"; import { decodeUint8, encodeUint8 } from "./number.js"; 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 concatUint8Arrays(encodeUint8(0x1), v); } //# sourceMappingURL=optional.js.map