UNPKG

t0n

Version:

Collection of elegant typescript resources for web artisans

2 lines 3.42 kB
var i=class r{static{this.UNDEFINED=Symbol("UNDEFINED");}static resolve(t,e){let n=r.UNDEFINED;try{n=t();}catch{}return new r(n,e)}static defined(t){return !this.undefined(t)&&!this.isUnset(t)}static undefined(t){return t===r.UNDEFINED||this.isUnset(t)}static isUnset(t){return t==null}constructor(t,e){this.value=t,this._default=e;}or(t){return r.defined(this.value)?this:r.resolve(t,this._default)}default(t){return this._default=t,this}tap(t){let e=this.get();return t?.(e),this}get(){return r.undefined(this.value)?this._default:this.value}getOrThrow(t){let e=this.get();if(r.undefined(e))throw t??new Error("Value is null or undefined");return e}};var s=class{static encode(t){let e=typeof t;return t=this.toString(t,e),(t?.byteLength||t.length)<1?"":Buffer.from(t).toString("base64")}static decode(t){if(typeof t!="string")throw new TypeError("Expected input to be a string");if(!/^[A-Za-z0-9+/]*={0,2}$/.test(t))throw new Error("Invalid Base64 string");try{return Buffer.from(t,"base64").toString("utf8")}catch{throw new Error("Invalid base64 characters")}}static toString(t,e){return e??=typeof t,Buffer.isBuffer(t)||e==="string"?t:Array.isArray(t)||["function","symbol"].includes(e)?t.toString():t===null||["number","bigint","boolean","undefined"].includes(e)?String(t):e==="object"&&!(t instanceof Uint8Array)&&!(t instanceof ArrayBuffer)?JSON.stringify(t):t}},u=class{static encode(t){return s.encode(t)?.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")||""}static decode(t){let e=t.length%4;return e&&(t+="=".repeat(4-e)),s.decode(t.replace(/-/g,"+").replace(/_/g,"/"))}};var o=class{constructor(t={}){this.data={};this.data={...t};}all(){return {...this.data}}has(t){return t in this.data}add(t={}){this.data={...this.data,...t};}replace(t={}){this.data={...t};}get(t,e){return this.has(t)?this.data[t]:e}set(t,e){if(this.has(t)&&typeof e!=typeof this.data[t])throw new TypeError(`Type mismatch for key ${t}. Expected ${typeof this.data[t]}, got ${typeof e}`);this.data[t]=e;}remove(t){delete this.data[t];}clear(){this.data={};}get length(){return this.keys().length}get size(){return this.length}keys(){return Object.keys(this.data)}values(){return Object.values(this.data)}toArray(){return Object.entries(this.data)}jsonSerialize(){return this.all()}toJson(t=0){return JSON.stringify(this.all(),null,t)}[Symbol.iterator](){return Object.entries(this.data)[Symbol.iterator]()}};var a=class r{static timestamp(t=null){return t==null?0:typeof t=="number"?t:t instanceof Date?Math.floor(t.getTime()/1e3):0}static minuteToSecond(t=null){return r.timestamp(t)||Math.floor(Date.now()/1e3)}static strToDate(t){return new Date(t)}static now(){return Math.floor(Date.now()/1e3)}static addSeconds(t,e){return t+e}static addMinutes(t,e){return t+e*60}static addHours(t,e){return t+e*3600}static addDays(t,e){return t+e*86400}};var l=class{static memory(){return this._memory||(this._memory=new o),this._memory}static has(t){return i.UNDEFINED!==this.get(t,i.UNDEFINED)}static get(t,e){if(this.memory().has(t))return this.memory().get(t,e);let n=process.env[t];return n!==void 0?n:e}static set(t,e){this.memory().set(t,e);}static remove(t){this.memory().remove(t);}};function T(r){if(typeof r=="number"&&Number.isInteger(r))return r;let e=JSON.stringify(r).replace(/\D/g,"");return e?parseInt(e.slice(0,16),10):0} export{i as Attempt,s as Base64,o as DataBag,l as Envir,a as Timestamp,u as UrlSafeBase64,T as toInt};