gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.48 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{utf8 as e}from"../codec/Utf8.js";import{fpb64 as t}from"../codec/ieee754-fpb.js";import{FromBinResult as n}from"../primitive/FromBinResult.js";import{asBE as i}from"../endian/platform.js";import{DateTimeLocal as r}from"../datetime/dt.js";import{BitWriter as o}from"../primitive/BitWriter.js";import{toGlScaleBytes as s}from"../primitive/number/xtUint.js";import{I64 as m}from"../primitive/number/I64.js";import{U64 as f}from"../primitive/number/U64.js";export function unknownToBin(n){let l;if("string"==typeof n){const t=e.toBytes(n),i=s(t.length);l=new Uint8Array(i.length+t.length),l.set(i),l.set(t,i.length)}else if("boolean"==typeof n)l=new Uint8Array(2),l[0]=1,l[1]=n?1:0;else if("number"==typeof n)if(Number.isInteger(n)){const e=i.intMinBytes(n);l=new Uint8Array(1+e.length),l[0]=e.length,l.set(e,1)}else{const e=t.toBytes(n);l=new Uint8Array(1+e.length),l[0]=e.length,l.set(e,1)}else if(null==n)l=new Uint8Array(1);else if(n instanceof r){l=new Uint8Array(1+n.serialSizeBits/8);const e=o.mount(l);e.pushNumberBE(n.serialSizeBits/8,1),n.serialize(e)}else if(n instanceof m){const e=n.toBytesBE();l=new Uint8Array(1+e.length),l[0]=e.length,l.set(e,1)}else{if(!(n instanceof f))throw new Error("Bad data "+typeof n);{const e=n.toBytesBE();l=new Uint8Array(1+e.length),l[0]=e.length,l.set(e,1)}}return l}export function unknownFromBin(e,t){if(0===e[t])return new n(1,void 0);throw new Error("TODO: Finish")}