gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
1 lines • 3.24 kB
JavaScript
import{RangeProblem as t}from"../error/probs/RangeProblem.js";import{TypeProblem as r}from"../error/probs/TypeProblem.js";function i(t){return null==t?Number.NaN:+t}function o(t){return null==t?Number.NaN:Math.trunc(+t)}class s{constructor(t,o,s,h=i){this.noun=t,this._uVal=o,this._coercer=h,this._prob=void 0,s&&(this._prob=r.UnexpVal(t,o,s))}get value(){return void 0===this._nVal&&(this._nVal=this._coercer(this._uVal)),this._nVal}throwNot(){if(void 0===this._prob)return this;throw new Error(this._prob.toString())}cast(){if(void 0===this._prob)return this.value;throw new Error(this._prob.toString())}is(){return void 0===this._prob}coerce(){return this._prob=void 0,this}atMost(r){return void 0!==this._prob||this.value<=r||(this._prob=t.Lte(this.noun,this.value,r)),this}lt(r){return void 0!==this._prob||this.value<r||(this._prob=t.Lt(this.noun,this.value,r)),this}atLeast(r){return void 0!==this._prob||this.value>=r||(this._prob=t.Gte(this.noun,this.value,r)),this}gt(r){return void 0!==this._prob||this.value>r||(this._prob=t.Lt(this.noun,this.value,r)),this}unsigned(){return void 0!==this._prob||this.value>=0||(this._prob=t.Gte(this.noun,this.value,0)),this}natural(){return void 0!==this._prob||this.value>=1||(this._prob=t.Gt(this.noun,this.value,0)),this}}class h{constructor(t,i,o){this.noun=t,this._uVal=i,this._prob=void 0,this._sVal="",o&&(this._prob=r.UnexpVal(t,i,o))}get value(){return this._sVal}throwNot(){if(void 0===this._prob)return this;throw new Error(this._prob.toString())}cast(){if(void 0===this._prob)return this.coerce().value;throw new Error(this._prob.toString())}is(){return void 0===this._prob}coerce(){return this._prob=void 0,null==this._uVal||(this._sVal=""+this._uVal),this}}class n{constructor(t,i,o){this.noun=t,this._uVal=i,this._prob=void 0,this._bVal=void 0,o&&(this._prob=r.UnexpVal(t,i,o))}get value(){return void 0===this._bVal&&(this._bVal=!!this._uVal),this._bVal}throwNot(){if(void 0===this._prob)return this;throw new Error(this._prob.toString())}cast(){if(void 0===this._prob)return this.value;throw new Error(this._prob.toString())}is(){return void 0===this._prob}coerce(){return this._prob=void 0,this}}class e{constructor(t,r){this.noun=t,this._lVal=r,this._prob=void 0}throwNot(){if(void 0===this._prob)return this;throw new Error(this._prob.toString())}is(){return void 0===this._prob}exactly(r){return void 0!==this._prob||this._lVal.length!==r&&(this._prob=t.Eq(this.noun+".length",this._lVal.length,r)),this}atMost(r){return void 0!==this._prob||this._lVal.length>r&&(this._prob=t.Lte(this.noun+".length",this._lVal.length,r)),this}atLeast(r){return void 0!==this._prob||this._lVal.length<r&&(this._prob=t.Gte(this.noun+".length",this._lVal.length,r)),this}}export function sNum(t,r){const i="number"!=typeof r||Number.isNaN(r)?"number":void 0;return new s(t,r,i)}export function sInt(t,r){const i=Number.isSafeInteger(r)?void 0:"integer";return new s(t,r,i,o)}export function sFloat(t,r){const i="number"!=typeof r||Number.isNaN(r)?"float":void 0;return new s(t,r,i)}export function sStr(t,r){return new h(t,r,"string"!=typeof r?"string":void 0)}export function sBool(t,r){return new n(t,r,"boolean"!=typeof r?"boolean":void 0)}export function sLen(t,r){return new e(t,r)}