UNPKG

gnablib

Version:

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

2 lines 10.6 kB
/*! Copyright 2024 the gnablib contributors MPL-1.1 */ import{AtMostError as t}from"../error/AtMostError.js";import{ContentError as s}from"../error/ContentError.js";import{LTError as r}from"../error/LTError.js";import{sFloat as o,sNum as e}from"../safe/safe.js";import{parseDec as i}from"../primitive/number/xtFloat.js";import{parseDec as n}from"../primitive/number/xtInt.js";const a=Symbol.for("nodejs.util.inspect.custom"),h=24,u=12,c=864e8,g=36e8,_=6e7,d=1e6,m=146097,l=3506328,y=134117046,f=8640,D=367200,w=4406400;class N{constructor(t,s){this._storage=t,this._hPos=s}get hour(){return this._storage[this._hPos]}get minute(){return this._storage[this._hPos+1]}get second(){return this._storage[this._hPos+2]}get microsecond(){return this._storage[this._hPos+3]<<16|this._storage[this._hPos+4]<<8|this._storage[this._hPos+5]}toString(){let t="";const s=this.day,r=this.hour,o=this.minute,e=this.second,i=this.microsecond;s>0&&(t+=s+"d"),r>0&&(t+=r+"h"),o>0&&(t+=o+"i");const n=i.toString();return e>0?(t+=e,i>0&&(t+="."+("000000"+n).substring(n.length)),t+="s"):i>0&&(t+="0."+("000000"+n).substring(n.length)+"s"),t}toIso8601(){const t=this.day,s=this.hour,r=this.minute,o=this.second,e=this.microsecond,i=t?t+"D":"";let n="";if(s>0&&(n+=s+"H"),r>0&&(n+=r+"M"),o>0){if(n+=o,e>0){const t=e.toString();n+="."+("000000"+t).substring(t.length)}n+="S"}else if(e>0){const t=e.toString();n+="0."+("000000"+t).substring(t.length)+"S"}return n.length>0&&(n="T"+n),i+n}toJSON(){return this.toString()}toTimeLike(){const t=this._otherHours()+this.hour,s=this.minute,r=this.second,o=this.microsecond;let e="";const i=r.toString();if(t>0){const r=s.toString();e=(t>500?500:t)+":"+("00"+r).substring(r.length)+":"+("00"+i).substring(i.length)}else e=s>0?s+":"+("00"+i).substring(i.length):i;if(o>0){const t=o.toString();e+="."+("000000"+t).substring(t.length)}return e}serialize(t){t.mustPushNumberBE(this._storage[this._hPos],5),t.mustPushNumberBE(this._storage[this._hPos+1],6),t.mustPushNumberBE(this._storage[this._hPos+2],6),t.mustPushNumberBE(this.microsecond,20)}_validate(){e("hour",this._storage[this._hPos]).lt(h).throwNot(),e("minute",this._storage[this._hPos+1]).lt(60).throwNot(),e("second",this._storage[this._hPos+2]).lt(60).throwNot(),e("microsecond",this.microsecond).lt(d).throwNot()}_dh_misu(){return[this.day*h+this._storage[this._hPos],this._storage[this._hPos+1]*_+this._storage[this._hPos+2]*d+this.microsecond]}static _loadHISU(t,s,r,o,e,i){e[i]=t,e[i+1]=s,e[i+2]=r,e[i+3]=o>>16,e[i+4]=o>>8,e[i+5]=o}static _convertHISUPartsToUs(t){let s=0;return t.h&&(e("hours",t.h).unsigned().throwNot(),s+=t.h*g),t.i&&(e("minutes",t.i).unsigned().throwNot(),s+=t.i*_),t.s&&(e("seconds",t.s).unsigned().throwNot(),s+=t.s*d),s=Math.round(s),t.us&&(e("microseconds",t.us).unsigned().throwNot(),s+=0|t.us),s}static _parseDHIS(t){const r=t.getReset(),o={d:0,h:0,i:0,s:0};for(;;){const s=t.indexOfAny(["d","D"]);if(s>0){if(o.d=i(t.substring(0,s)),Number.isNaN(o.d))break;t.shrink(s+1)}const r=t.indexOfAny(["h","H"]);if(r>0){if(o.h=i(t.substring(0,r)),Number.isNaN(o.h))break;t.shrink(r+1)}const e=t.indexOfAny(["i","I"]);if(e>0){if(o.i=i(t.substring(0,e)),Number.isNaN(o.i))break;t.shrink(e+1)}const n=t.indexOfAny(["s","S"]);if(n>0){if(o.s=i(t.substring(0,n)),Number.isNaN(o.s))break;t.shrink(n+1)}if(t.trimStart().length>0)break;return o}throw r(),new s("Expecting dhis components","duration",t)}static _deserHISU(t,s,r){const o=t.readNumberBE(5),e=t.readNumberBE(6),i=t.readNumberBE(6),n=t.readNumberBE(20);N._loadHISU(o,e,i,n,s,r)}}export class DurationExact extends N{static get zero(){return b}static get max(){return S}get day(){return(this._storage[this._hPos-4]<<24|this._storage[this._hPos-3]<<16|this._storage[this._hPos-2]<<8|this._storage[this._hPos-1])>>>0}_otherHours(){return this.day*h}toString(){const t=super.toString();return 0===t.length?"0s":t}toIso8601(){let t=super.toIso8601();return 0==t.length&&(t="0D"),"P"+t}toDhMiso(){return[this.day*h+this._storage[this._hPos],this._storage[this._hPos+1]*_+this._storage[this._hPos+2]*d+this.microsecond]}serialize(t){t.mustPushNumberBE(this.day,27),super.serialize(t)}get serialSizeBits(){return DurationExact.serialBits}validate(){if(e("day",this.day).atMost(y).throwNot(),this._validate(),this.gt(S))throw new r("duration",this,S);return this}gt(t){const s=this._hPos+6;for(let r=0;r<s;r++){if(this._storage[r]>t._storage[r])return!0;if(this._storage[r]<t._storage[r])return!1}return!1}add(t){let[s,r]=this.toDhMiso();const[o,e]=t.toDhMiso();if(s+=o,r+=e,r>=g&&(r-=g,s+=1),s>=3218809104)return S;const i=s/h|0,n=s%h,a=r%d;r=r/d|0;const u=r%60;r=r/60|0;const c=r%60,_=new Uint8Array(DurationExact.storageBytes);return DurationExact._loadDHISU(i,n,c,u,a,_),new DurationExact(_,4)}sub(t){let[s,r]=this.toDhMiso();const[o,e]=t.toDhMiso();if(s-=o,r-=e,r<0&&(r+=g,s-=1),s<0)return b;const i=s/h|0,n=s%h,a=r%d;r=r/d|0;const u=r%60;r=r/60|0;const c=r%60,_=new Uint8Array(DurationExact.storageBytes);return DurationExact._loadDHISU(i,n,c,u,a,_),new DurationExact(_,4)}get[Symbol.toStringTag](){return"DurationExact"}[a](){return`DurationExact(${this.toString()})`}static _loadDHISU(t,s,r,o,e,i){i[0]=t>>24,i[1]=t>>16,i[2]=t>>8,i[3]=t,N._loadHISU(s,r,o,e,i,4)}static new(s){let r=N._convertHISUPartsToUs(s),o=0;s.d&&(e("days",s.d).unsigned().atMost(y).throwNot(),o=s.d);const i=o%1;0!=i&&(r+=Math.round(i*c),o-=i),r>=c&&(o+=Math.floor(r/c),r%=c);const n=r%d,a=r/d%60|0,h=r/g|0,u=(r/_|0)%60,m=new Uint8Array(DurationExact.storageBytes);DurationExact._loadDHISU(o,h,u,a,n,m);const l=new DurationExact(m,4);if(o==y&&r>0)throw new t("duration",l,S);return l}static fromUs(t){e("microseconds",t).unsigned().throwNot();let s=0,r=0,o=0,i=0,n=0;n=t%d,i=(t=Math.floor(t/d))%60,o=(t=Math.floor(t/60))%60,r=(t=Math.floor(t/60))%h,s=t=Math.floor(t/h),e("days",s).atMost(y).throwNot();const a=new Uint8Array(DurationExact.storageBytes);return DurationExact._loadDHISU(s,r,o,i,n,a),new DurationExact(a,4)}static fromTimeLike(t){const r=t.split(":");let o=0,a=0,u=0;switch(r.length){case 1:u=i(r[0]);break;case 2:a=n(r[0]),u=i(r[1]);break;case 3:o=n(r[0]),a=n(r[1]),u=i(r[2]);break;default:throw new s("Expecting h*:mm:ss.uuuuuu","time-like",t)}e("hours",o).atMost(500).throwNot(),e("minutes",a).lt(60).throwNot(),e("seconds",u).lt(60).throwNot();const c=o/h|0;o%=h;const g=u%1*d|0;u|=0;const _=new Uint8Array(DurationExact.storageBytes);return DurationExact._loadDHISU(c,o,a,u,g,_),new DurationExact(_,4)}static parse(t){const s=N._parseDHIS(t);return DurationExact.new(s)}static deserialize(t){const s=new Uint8Array(DurationExact.storageBytes),r=t.readNumberBE(27);return s[0]=r>>24,s[1]=r>>16,s[2]=r>>8,s[3]=r,N._deserHISU(t,s,4),new DurationExact(s,4)}}DurationExact.serialBits=64,DurationExact.storageBytes=10;const b=DurationExact.fromUs(0),S=DurationExact.new({d:y});export class Duration extends N{static get zero(){return E}static get max(){return x}get year(){return this._storage[0]<<16|this._storage[1]<<8|this._storage[2]}get month(){return(this._storage[3]<<8|this._storage[4])/720}get day(){return this._storage[this._hPos-3]<<16|this._storage[this._hPos-2]<<8|this._storage[this._hPos-1]}_otherHours(){return this.year*u+this.month>=.75?501:this.day*h}toString(){let t="";const s=this.year,r=this.month;return s>0&&(t+=s+"y"),r>0&&(t+=r+"m"),t+=super.toString(),0===t.length?"0s":t}toIso8601(){let t="";const s=this.year,r=this.month;return s>0&&(t+=s+"Y"),r>0&&(t+=r+"M"),t+=super.toIso8601(),0==t.length&&(t="0D"),"P"+t}serialize(t){const s=(this._storage[3]<<8|this._storage[4])+this.year*f;t.mustPushNumberBE(s,32),t.mustPushNumberBE(this.day,19),super.serialize(t)}get serialSizeBits(){return Duration.serialBits}validate(){return e("year",this.year).atMost(D).throwNot(),o("month",this.month).lt(u).throwNot(),e("day",this.day).lt(m).throwNot(),this._validate(),this}toYMDhMiso(){return[this.year,this.month,this.day*h+this._storage[this._hPos],this._storage[this._hPos+1]*_+this._storage[this._hPos+2]*d+this.microsecond]}_ymf_dh_misu(){return[this.year*f+(this._storage[3]<<8|this._storage[4]),...this._dh_misu()]}gt(t){const s=this._hPos+6;for(let r=0;r<s;r++){if(this._storage[r]>t._storage[r])return!0;if(this._storage[r]<t._storage[r])return!1}return!1}add(t){let[s,r,o]=this._ymf_dh_misu();if(t instanceof Duration){const[e,i,n]=t._ymf_dh_misu();s+=e,r+=i,o+=n}else{const[s,e]=t._dh_misu();r+=s,o+=e}if(o>=g&&(o-=g,r+=1),r>=l&&(r-=l,s+=3456e3),s>=3172608e3)return x;const e=s%f,i=(s-e)/f,n=r/h|0,a=r%h,u=o%d;o=o/d|0;const c=o%60;o=o/60|0;const _=o%60,m=new Uint8Array(Duration.storageBytes);return Duration._loadYMfD(i,e,n,m),N._loadHISU(a,_,c,u,m,8),new Duration(m,8)}sub(t){let[s,r,o]=this._ymf_dh_misu();if(t instanceof Duration){const[e,i,n]=t._ymf_dh_misu();s-=e,r-=i,o-=n}else{const[s,e]=t._dh_misu();r-=s,o-=e}if(o<0&&(o+=g,r-=1),r<0&&(r+=l,s-=3456e3),s<0)return E;const e=s%f,i=(s-e)/f,n=r/h|0,a=r%h,u=o%d;o=o/d|0;const c=o%60;o=o/60|0;const _=o%60,m=new Uint8Array(Duration.storageBytes);return Duration._loadYMfD(i,e,n,m),N._loadHISU(a,_,c,u,m,8),new Duration(m,8)}get[Symbol.toStringTag](){return"Duration"}[a](){return`Duration(${this.toString()})`}static _loadYMfD(t,s,r,o){o[0]=t>>16,o[1]=t>>8,o[2]=t,o[3]=s>>8,o[4]=s,o[5]=r>>16,o[6]=r>>8,o[7]=r}static new(s){let r=0;s.y&&(e("years",s.y).unsigned().atMost(D).throwNot(),r=s.y*u),s.m&&(e("months",s.m).unsigned().throwNot(),r+=s.m);let o=N._convertHISUPartsToUs(s),i=0;if(s.d){e("days",s.d).unsigned().throwNot(),i=s.d;const t=i%1;0!=t&&(o+=Math.round(t*c),i-=t),i=s.d}if(o>=c&&(i+=Math.floor(o/c),o%=c),i>m){const t=i/m|0;i%=m,r+=400*t*u}const n=r/u|0,a=Math.round(r%u*720);if(r>w)throw new t("years+months+days",`${n}y${r%u}m${i}d`,D+"y");const h=o%d,l=o/d%60|0,y=o/g|0,f=(o/_|0)%60,b=new Uint8Array(Duration.storageBytes);Duration._loadYMfD(n,a,i,b),N._loadHISU(y,f,l,h,b,8);const S=new Duration(b,8);if(r==w&&(i>0||o>0))throw new t("duration",S,x);return S}static parse(t){const r=t.getReset();let o=0,e=0;for(;;){const s=t.indexOfAny(["y","Y"]);if(s>0){if(o=i(t.substring(0,s)),Number.isNaN(o))break;t.shrink(s+1)}const r=t.indexOfAny(["m","M"]);if(r>0){if(e=i(t.substring(0,r)),Number.isNaN(e))break;t.shrink(r+1)}try{const s=N._parseDHIS(t);return Duration.new(Object.assign({y:o,m:e},s))}catch(t){break}}throw r(),new s("Expecting ymdhis components","duration",t)}static deserialize(t){const s=new Uint8Array(Duration.storageBytes),r=t.readNumberBE(32),o=r/f|0,e=r%f,i=t.readNumberBE(19);return Duration._loadYMfD(o,e,i,s),N._deserHISU(t,s,8),new Duration(s,8)}}Duration.serialBits=88,Duration.storageBytes=14;const E=Duration.new({}),x=Duration.new({y:D});