UNPKG

gnablib

Version:

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

2 lines 1.02 kB
/*! Copyright 2023 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 n}from"../safe/safe.js";export class TableName{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.name.length,t.set(this._bytes,1),t}static fromStr(e){const s=t.toBytes(e);return n("name-bytes",s).atLeast(1).atMost(63).throwNot(),new TableName(e,s)}static fromUtf8Bytes(e){n("utf8bytes",e).atLeast(1).atMost(63).throwNot();const s=t.fromBytes(e);return new TableName(s,e)}static fromBin(n,s=0){if(s>=n.length)return new e(0,void 0,"TableName.fromBin unable to find length byte");const o=n[s++];if(o>63||o<1)return new e(0,void 0,`TableName.fromBin name size in invalid (1 < 63 got ${o})`);const r=s+o;if(r>n.length)return new e(0,void 0,"TableName.fromBin missing name (not enough data)");const a=n.slice(s,r),i=t.fromBytes(a);return new e(o+1,new TableName(i,a))}}