gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.56 kB
JavaScript
/*! Copyright 2024 the gnablib contributors MPL-1.1 */
import{ContentError as t}from"../../error/ContentError.js";import{sLen as e,sNum as s}from"../../safe/safe.js";const r=Symbol.for("nodejs.util.inspect.custom"),i="Sexagesimal";export class Sexagesimal{constructor(t){this._v=t}toString(){return this.valueOf().toString()}toPadString(){const t=this.valueOf().toString();return("00"+t).substring(t.length)}toJSON(){return this._v[0]}valueOf(){return this._v[0]}serialize(t){t.mustPushNumberBE(this._v[0],Sexagesimal.serialBits)}get serialSizeBits(){return n.serialBits}validate(){return s("value",this._v[0]).lt(60).throwNot(),this}get[Symbol.toStringTag](){return i}[r](){return`${i}(${this.toString()})`}static setupStor(t){return t?(e("storage",t).atLeast(n.storageBytes).throwNot(),t):new Uint8Array(n.storageBytes)}static new(t,e){s("sexa",t).unsigned().lt(60).throwNot();const r=n.setupStor(e);return r[0]=t,new Sexagesimal(r)}static parseIntoStorage(e,r,i,n="input"){e.trimStart();const a=e.match(/^(\d+)\s*$/);if(null==a)throw new t("expecting unsigned integer-string",n,e);const[,o]=a;if(i&&2!=o.length)throw new t("expecting 2 digit unsigned integer-string",n,e);const g=parseInt(o,10);s("value",g).unsigned().lt(60).throwNot(),r[0]=g,e.shrink(o.length)}static parse(t,e=!1,s){const r=n.setupStor(s);return n.parseIntoStorage(t,r,e,"sexagesimal"),new Sexagesimal(r)}static deserialize(t,e){const s=n.setupStor(e);return s[0]=t.readNumberBE(this.serialBits),new Sexagesimal(s)}}Sexagesimal.storageBytes=1,Sexagesimal.serialBits=6;const n=Sexagesimal;