UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 1.01 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{utf8 as t}from"../codec/Utf8.js";import{FromBinResult as e}from"../primitive/FromBinResult.js";import{sLen as o}from"../safe/safe.js";export class ColName{constructor(t,e){this.name=t,this._bytes=e}toJSON(){return this.name}toBin(){const t=new Uint8Array(1+this._bytes.length);return t[0]=this._bytes.length,t.set(this._bytes,1),t}static fromStr(e){const n=t.toBytes(e);return o("name",e).atLeast(1).atMost(63).throwNot(),new ColName(e,n)}static fromUtf8Bytes(e){o("utf8bytes",e).atLeast(1).atMost(63).throwNot();const n=t.fromBytes(e);return new ColName(n,e)}static fromBin(o,n=0){if(n>=o.length)return new e(0,void 0,"ColName.fromBin unable to find length byte");const s=o[n++];if(s>63||s<1)return new e(0,void 0,`ColName.fromBin name size in invalid (1 < 63 got ${s})`);const r=n+s;if(r>o.length)return new e(0,void 0,"ColName.fromBin missing name (not enough data)");const i=o.slice(n,r),a=t.fromBytes(i);return new e(s+1,new ColName(a,i))}}