UNPKG

gnablib

Version:

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

2 lines 1.6 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{ParseProblem as t}from"../error/index.js";import{sLen as r}from"../safe/safe.js";const o=Symbol.for("nodejs.util.inspect.custom"),s=["false","true"];export class Bool{constructor(t,r){this._arr=t,this._pos=r,this.size8=1,this.serialBits=1}static from(t){return new Bool(Uint8Array.of(t?1:0),0)}static mount(t,o=0){return r("arr",t).atLeast(o+1).throwNot(),new Bool(t,o)}static deserial(t){return new Bool(Uint8Array.of(t.readNumberBE(1)),0)}clone(){return new Bool(this._arr.slice(this._pos,this._pos+1),0)}toString(){return s[1&this._arr[this._pos]]}valueOf(){return!!(1&this._arr[this._pos])}serial(t){t.mustPushNumberBE(this._arr[this._pos],1)}get[Symbol.toStringTag](){return"Bool"}[o](){return`Bool(${this.toString()})`}static parse(r,o){const s=["true"],e=["false"];(null==o?void 0:o.preventNumeric)||(s.push("1"),e.push("0")),(null==o?void 0:o.allowYes)&&(s.push("yes"),e.push("no")),(null==o?void 0:o.allowOn)&&(s.push("on"),e.push("off")),r.trimStart().trimEnd();const i=r.toString().toLowerCase();for(const t of s)if(i===t)return r.shrink(t.length),!0;for(const t of e)if(i===t)return r.shrink(t.length),!1;return new t("src","expecting "+s.join("/")+" or "+e.join("/"),i)}}export class BoolMut extends Bool{static from(t){return new BoolMut(Uint8Array.of(t?1:0),0)}static mount(t,o=0){return r("arr",t).atLeast(o+1).throwNot(),new BoolMut(t,o)}static deserial(t){return new BoolMut(Uint8Array.of(t.readNumberBE(1)),0)}set(t){this._arr[this._pos]&=254,this._arr[this._pos]+=+t}[o](){return`BoolMut(${this.toString()})`}}