UNPKG

subflow

Version:

Safety your data flow in TypeScript with flow-based primitives.

3 lines (2 loc) 7.23 kB
"use strict";var _=Object.defineProperty;var L=(t,e,h)=>e in t?_(t,e,{enumerable:!0,configurable:!0,writable:!0,value:h}):t[e]=h;var p=(t,e,h)=>L(t,typeof e!="symbol"?e+"":e,h);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=Symbol("FLOW_TYPE"),r=Symbol("value"),s=Symbol("customMethods"),x=(t,e)=>typeof t=="object"&&t!==null&&a in t&&t[a]===e,A=t=>x(t,"error");class y{static add(e){this.errors.push(e)}static view(){return this.errors}static clear(){this.errors=[]}}p(y,"errors",[]);const l=t=>{const e={timestamp:Date.now(),traceId:crypto.randomUUID(),...t},h={_value:e.value,[a]:"error",getError:()=>e,get:()=>e.value};return y.add(e),new Proxy(h,{get(c,g){return g in h?h[g]:()=>{}}})};function W(t){return typeof t=="object"&&t!==null&&a in t?A(t):!1}const E={[a]:"boolean",not(){return i(!this[r],this[s])},and(t){return i(this[r]&&t,this[s])},or(t){return i(this[r]||t,this[s])},nor(t){return i(!this[r]||!t,this[s])},xor(t){return i(this[r]!==t,this[s])},notEqual(t){return i(this[r]!==t,this[s])},equal(t){return i(this[r]===t,this[s])},flowString(){return o(this[r].toString())},flowNumber(){return n(this[r]?1:0)}},i=(t,e)=>typeof t!="boolean"?l({type:"boolean",value:t,message:"Value must be a boolean",code:"BOOLEAN_FLOW_ERROR"}):b("boolean",t,e),u=(t,e)=>Array.isArray(t)?b("array",t,e):l({type:"array",value:t,message:"Value must be an array",code:"ARRAY_FLOW_ERROR"}),j={[a]:"object",keys(){return u(Object.keys(this[r]))},values(){return u(Object.values(this[r]))},entries(){return u(Object.entries(this[r]))},has(t){return i(this[r].hasOwnProperty(t))},set(t,e){return f({...this[r],[t]:e},this[s])},delete(t){const e={...this[r]};return delete e[t],f(e,this[s])},flowString(){return o(JSON.stringify(this[r]))}},f=(t,e)=>i(!!t).and(typeof t=="object").not().get()?l({type:"object",value:t,message:"Value must be an object",code:"OBJECT_FLOW_ERROR"}):b("object",t,e),M={[a]:"array",push(...t){return u([...this[r],...t],this[s])},pop(){const t=this[r];return u(t.slice(0,-1),this[s])},shift(){return u(this[r].slice(1),this[s])},unshift(...t){return u([...t,...this[r]],this[s])},join(t){return o(this[r].join(t))},map(t){return u(this[r].map(t),this[s])},filter(t){return u(this[r].filter(t),this[s])},reduce(t,e){return this[r].reduce(t,e)},sort(t){return u(this[r].toSorted(t),this[s])},reverse(){return u(this[r].reduce((t,e)=>[e,...t],[]),this[s])},concat(...t){return u(this[r].concat(...t),this[s])},slice(t,e){return u(this[r].slice(t,e),this[s])},splice(t,e,...h){return u(this[r].reduce((c,g,m)=>m===t?[...c,...h]:m>t&&m<t+e?c:[...c,g],[]),this[s])},indexOf(t,e){return n(this[r].indexOf(t,e))},findLastIndex(t){const e=this[r].findLastIndex(h=>h===t);return n(e)},includes(t,e){return i(this[r].includes(t,e))},find(t){return this[r].find(t)},findIndex(t){return n(this[r].findIndex(t))},forEach(t){return this[r].forEach(t),u(this[r],this[s])},every(t){return i(this[r].every(t))},some(t){return i(this[r].some(t))},flowString(){return o(this[r].toString())},flowLocaleString(){return o(this[r].toLocaleString())},flowStringfy(){return o(JSON.stringify(this[r]))},flowObject(){try{return f(Object.fromEntries(Object.entries(this[r])))}catch(t){return l({message:"Value must be entriesable of array",code:"ARRAY_FLOW_ERROR",type:"array",value:this[r],cause:t})}},flowObjectEntries(){try{return f(Object.fromEntries(this[r]))}catch(t){return l({message:"Value must be entriesable of array",code:"ARRAY_FLOW_ERROR",type:"array",value:this[r],cause:t})}},flowBoolean(){return i(this[r].length>0)},flowNumber(){return n(this[r].length)}},R={[a]:"string",toUpper(){return o(this[r].toUpperCase(),this[s])},toLower(){return o(this[r].toLowerCase(),this[s])},length(){return n(this[r].length)},reverse(){return o(this[r].split("").reverse().join(""),this[s])},replace(t,e){return o(this[r].replace(t,e),this[s])},trim(){return o(this[r].trim(),this[s])},padStart(t,e){return o(this[r].padStart(t,e),this[s])},padEnd(t,e){return o(this[r].padEnd(t,e),this[s])},startsWith(t){return i(this[r].startsWith(t))},endsWith(t){return i(this[r].endsWith(t))},includes(t){return i(this[r].includes(t))},indexOf(t){return n(this[r].indexOf(t))},lastIndexOf(t){return n(this[r].lastIndexOf(t))},charAt(t){return o(this[r].charAt(t),this[s])},charCodeAt(t){return n(this[r].charCodeAt(t))},concat(...t){return o(this[r].concat(...t),this[s])},split(t){return u(this[r].split(t))},slice(t,e){return o(this[r].slice(t,e),this[s])},substring(t,e){return o(this[r].substring(t,e),this[s])},equals(t){return i(this[r]===t)},notEqual(t){return i(this[r]!==t)},flowNumber(){return n(Number(this[r]))},flowFloatNumber(){return n(Number(this[r]))},flowBoolean(){return i(!!this[r])},flowArray(){return u(this[r].split(""))},flowObject(){return f(JSON.parse(this[r]))}},o=(t,e)=>typeof t!="string"?l({type:"string",value:t,message:"Value must be a string",code:"STRING_FLOW_ERROR"}):b("string",t,e),F={[a]:"number",add(t){return n(this[r]+t,this[s])},subtract(t){return n(this[r]-t,this[s])},multiply(t){return n(this[r]*t,this[s])},divide(t){return n(this[r]/t,this[s])},modulo(t){return n(this[r]%t,this[s])},power(t){return n(this[r]**t,this[s])},sqrt(){return n(Math.sqrt(this[r]),this[s])},round(){return n(Math.round(this[r]),this[s])},floor(){return n(Math.floor(this[r]),this[s])},ceil(){return n(Math.ceil(this[r]),this[s])},abs(){return n(Math.abs(this[r]),this[s])},random(){return n(Math.random(),this[s])},min(t){return n(Math.min(this[r],t),this[s])},max(t){return n(Math.max(this[r],t),this[s])},clamp(t,e){return n(Math.min(Math.max(this[r],t),e),this[s])},sign(){return n(Math.sign(this[r]),this[s])},toFixed(t){return o(this[r].toFixed(t))},toExponential(t){return o(this[r].toExponential(t))},toPrecision(t){return o(this[r].toPrecision(t))},lessThan(t){return i(this[r]<t)},greaterThan(t){return i(this[r]>t)},lessThanOrEqual(t){return i(this[r]<=t)},greaterThanOrEqual(t){return i(this[r]>=t)},flowBoolean(){return i(this[r]!==0)},flowLocaleString(t,e){return o(this[r].toLocaleString(t,e))},flowString(t){return o(this[r].toString(t))}},n=(t,e)=>typeof t!="number"?l({type:"number",value:t,message:"Value must be a number",code:"NUMBER_FLOW_ERROR"}):b("number",t,e),S={[a]:"bigint"},I=(t,e)=>typeof t!="bigint"?l({type:"bigint",value:t,message:"Value must be a bigint",code:"BIGINT_FLOW_ERROR"}):b("bigint",t,e),d=Object.freeze({isError:!1,get(){return this[r]},getError(){}}),w={number:Object.freeze({...d,...F}),string:Object.freeze({...d,...R}),boolean:Object.freeze({...d,...E}),bigint:Object.freeze({...d,...S}),array:Object.freeze({...d,...M}),object:Object.freeze({...d,...j})},O=new WeakMap,N=(t,e)=>{if(!e)return w[t];if(O.has(e))return O.get(e);const h={...w[t],...e},c=Object.freeze(h);return O.set(e,c),c},b=(t,e,h)=>{const c=N(t,h);return Object.freeze({...c,[r]:e,[a]:t,[s]:h})};exports.ErrorManager=y;exports.arrayFlow=u;exports.arrayMethods=M;exports.bigIntMethods=S;exports.bigintFlow=I;exports.booleanFlow=i;exports.booleanMethods=E;exports.createFlow=b;exports.errorFlow=l;exports.isError=W;exports.numberFlow=n;exports.numberMethods=F;exports.objectFlow=f;exports.objectMethods=j;exports.stringFlow=o;exports.stringMethods=R; //# sourceMappingURL=index.cjs.js.map