UNPKG

sum-type

Version:

A simple library for complex logic

3 lines (2 loc) 8.88 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SumType={})}(this,(function(t){"use strict";const e=(t,...e)=>{if(0==e.length)throw new Error("You must provide an initial value and a non-empty spread of functions.");return e.reduce(((t,e)=>e(t)),t)},n=t=>e=>{if(Array.isArray(e)){const r=e,i=r.filter((e=>!t.includes(e)));if(i.length)throw new Error("Tags provided do not match original definition. The tags: ("+i.join("|")+") could not be found in the list: ("+t.join("|")+")");return n(r)}return t.reduce(((t,n)=>Object.assign(t,{[n]:e})),{})},r=(t,e)=>t>0?((t,e)=>Array(t).fill(e))(t,e).join(""):"";function i(t,e){const n=Object(e)===e,r=null==e,i=n&&Array.isArray(e),a=n&&!i,o=n&&"function"==typeof e,s=!n,c=a&&e.type&&e.tag,u=c&&"value"in e,l=c&&!u;return t({notPrimative:n,isPrimative:s,isString:"string"==typeof e,isNil:r,isArray:i,isObject:a,isPojo:a&&"Object"==e.constructor.name,isDate:a&&e instanceof Date,isError:a&&e instanceof Error,value:e,isFunction:o,isStag:c,valueStag:u,emptyStag:l})}const a=t=>i(function t(n){return a=>{const{value:o,isPojo:s,isObject:c,isArray:u,isDate:l,isError:f,valueStag:g,emptyStag:p,isString:y,isFunction:h}=a,m=r(n,""),d=r(n-1,""),v=r(n+1,""),b="";return g?o.type+"."+o.tag+"("+(void 0===o.value?"":i(t(n+1),o.value))+(n>0?b+d:"")+b+m+")":p?o.type+"."+o.tag+"()":s?0==Object.keys(o).length?"{}":e(o,Object.entries,(e=>e.map((([e,r])=>b+v+'"'+e+'":'+i(t(n+1),r).replace(b+v,"")))),(t=>b+m+"{"+t+b+m+"}")):u?"0"==o.length?"[]":b+m+"["+b+m+m+o.map((e=>i(t(n+1),e))).join(", ")+b+m+"]":l?"new "+o.constructor.name+'("'+o.toISOString()+'")':f?"new "+o.constructor.name+'("'+o.message+'")':h?o+"":c?"new "+o.constructor.name+"()":y?JSON.stringify(o):""+o}}(0),t);function o(){return a(this)}function s(t){return t.type}function c(t){return t.tag}const u={toString:o,inspect:o,[Symbol.for("nodejs.util.inspect.custom")]:o};function l(t,e,n){return Object.assign(Object.create(u),{type:t,tag:e,value:n})}function f(t,e){return Object.assign(Object.create(u),{type:t,tag:e})}const g=t=>t.reduce(((t,[e,n])=>({...t,[e]:n})),{}),p=["ExtraTags","MissingTags","InstanceNull","InstanceWrongType","InstanceShapeInvalid","VisitorNotAFunction","NotAType","TagsShapeInvalid"].reduce(((t,e)=>(t[e]=n=>l(t.type,e,n),t.tags.push(e),t)),{type:"StaticSumTypeError",tags:[],traits:{}});function y(t){return t.tags}const h={ExtraTags:function(t){return["Your tag function must have exactly the same"," keys as the type: "+t.T.type+". ","The following tags should not have been present:",t.extraKeys.join(", ")].join(" ")},MissingTags:function(t){},InstanceNull:function(t){return"Null is not a valid member of the type "+t.T.type},InstanceWrongType:function(t){},InstanceShapeInvalid:function(t){return[a(t.x),"is not a valid Member of the type:",t.T.type+". ","Please review the definition of "+t.T.type].join(" ")},VisitorNotAFunction:function(t){return t.context+" expected a visitor function but instead received "+a(t.visitor)},NotAType:function(t){return t.context+" expected a Type ({ type: string, tags: string[] }) but received "+a(t.T)},TagsShapeInvalid:(t,e)=>"fold("+s(t)+") tags provided were not the right shape. Expected { [tag]: f } but received "+a(e)};function m(t){const e=t();return E("fold",e),function(t){!function(t,e){if(null==e||Array.isArray(e)||"object"!=typeof e){return v(b.TagsShapeInvalid(t,e))}}(e,t);const n=Object.keys(t),r=y(e),i=[[n,e],[r,t]].map((function(t){const e=t[0],n=t[1];return e.filter((function(t){return!(t in n)}))})),a=i[0],o=i[1];return o.length>0?v(b.MissingTags({T:e,tags:t,missingKeys:o})):a.length>0?v(b.ExtraTags({T:e,tags:t,extraKeys:a})):function(n){return x(e,t,n)&&t[c(n)](n.value)}}}const d=t=>{return(e=h,t=>e[t.tag](t.value))(t);var e};function v(t){throw new Error(c(t)+": "+d(t))}const b=p;function E(t,e){return null!=e&&null!=e&&"string"==typeof e.type&&Array.isArray(e.tags)&&"traits"in e?null:v(b.NotAType({context:t,T:e}))}function T(t){return"function"!=typeof t.visitor?v(b.VisitorNotAFunction({context:t.context,visitor:t.visitor})):null}function N(t,e){return null!=e&&s(e)==t.type&&y(t).includes(c(e))?null:v(b.InstanceShapeInvalid({x:e,T:t}))}function x(t,e,n){return null==n?v(b.InstanceNull({T:t,tags:e,x:n})):s(n)!==t.type?v(b.InstanceWrongType({T:t,tags:e,x:n})):!!y(t).includes(c(n))||v(b.InstanceShapeInvalid({T:t,tags:e,x:n}))}function S(t){return m((()=>t))}const j=t=>{const e=S(t);return n=>{const r=e(n);return e=>{N(t,e);const n=r(e);return t[c(e)](n)}}},O=t=>{const e=S(t);return n=>{const r=e(n);return e=>{if(x(t,n,e),"value"in e){const i=r(e);return x(t,n,i)&&i}return e}}},A=(t,e)=>({type:t,traits:{},tags:e,...g(e.map((e=>[e,(...n)=>n.length?l(t,e,n[0]):f(t,e)])))});function Y(t){return A(t,["Y","N"])}const I=Y("sumType.Either"),w=t=>{const e=S(t),{left:n,right:r}=t.traits["sum-type/bifunctor"];return(t,i)=>e({[n]:t,[r]:i})},W=t=>(E("bifold",t),w(t)),B=t=>(E("bimap",t),(t=>{const e=j(t),{left:n,right:r}=t.traits["sum-type/bifunctor"];return(t,i)=>e({[n]:t,[r]:i})})(t)),M=t=>(E("getOr",t),e=>n=>(N(t,n),(t=>e=>w(t)((()=>e),(t=>t)))(t)(e)(n))),P=t=>(E("concatWith",t),e=>(T({context:"concatWith",visitor:e}),n=>(N(t,n),r=>(N(t,r),(t=>e=>n=>r=>{const{left:i,right:a}=t.traits["sum-type/bifunctor"];return c(n)==i?n:c(r)==i?r:t[a](e(n.value)(r.value))})(t)(e)(n)(r))))),F=({left:t,right:e})=>n=>(n.traits["sum-type/bifunctor"]={left:t,right:e},n.left=e=>n[t](e),n.right=t=>n[e](t),n.bifold=W(n),n.bimap=B(n),n.getOr=M(n),n.getWith=(t=>(e,n)=>W(t)((()=>e),(t=>n(t))))(n),n.tagBy=(t=>(S(t),(e,n)=>r=>(T({context:"tagBy",visitor:n}),n(r)?t.right(r):t.left(e))))(n),n.encase=(t=>e=>n=>{try{return t.right(e(n))}catch(e){return t.left(e)}})(n),n.toBoolean=(t=>t.bifold((()=>!1),(()=>!0)))(n),n.fromNullable=(t=>e=>null==e?t.left(e):t.right(e))(n),n.all=(t=>e=>{const n=e.filter((e=>!t.toBoolean(e)));return n.length>0?t.left(n.map((t=>t.value))):t.right(e.map(t.getOr(null)))})(n),n.any=(t=>e=>e.some(t.toBoolean)?t.right(e.filter(t.toBoolean).map(t.getOr(null))):e.find((e=>!t.toBoolean(e))))(n),n.concatWith=P(n),n);function _(t){if(t.traits["sum-type/decorated"])return t;{const e=n(y(t)),r=j(t),i=O(t),a=S(t);return y(t).forEach((n=>{[[n,n,n],[n,"_"+n,"_"+n+"_"]].forEach((([n,o,s])=>{t["is"+o]=t["is"+o]||(e=>(N(t,e),c(e)===n)),t["map"+o]=t["map"+o]||(t=>r({...e((t=>t)),[n]:t})),t["get"+s+"Or"]=t["get"+s+"Or"]||(t=>a({...e((()=>t)),[n]:t=>t})),t["get"+s+"With"]=t["get"+s+"With"]||((t,r)=>a({...e((()=>t)),[n]:t=>r(t)})),t["chain"+o]=t["chain"+o]||(t=>r=>i({...e((()=>r)),[n]:t})(r)),t["assert"+o]=t["assert"+o]||a({...e(I.N),[n]:I.Y}),t[n.toLowerCase()+"s"]=t[n+"s"]||(e=>e.reduce(((e,r)=>e.concat(t["is"+n](r)?[r.value]:[])),[]))}))})),t.fold=t.fold||a,t.mapAll=t.mapAll||r,t.chainAll=t.chainAll||i,t.traits["sum-type/decorated"]=!0,t}}const k=t=>{return e(Y(t),_,F({left:"N",right:"Y"}),(r="Y",t=>(t.traits["sum-type/functor"]=r,t.of=t[r],t.map=t.map||(e=>(T({context:"map",visitor:e}),j(t)({...n(t.tags)((t=>t)),[r]:e}))),t)),(t=>e=>{e.traits["sum-type/monad"]=t,e.of=e[t];const r=O(e);return e.chain=i=>(T({context:"chain",visitor:i}),a=>r({...n(e.tags)((t=>e[c(a)](t))),[t]:t=>i(t)})(a)),e})("Y"));var r},D=t=>e(t,k,(t=>{const e=t.N;return t.N=()=>e(),t.traits["sum-type/maybe"]=!0,t})),K=(t,e)=>_(A(t,e)),V=k("sumType.Either"),{Y:J,N:C,bifold:L,getOr:q,getWith:z,bimap:G,map:H,mapY:Q,mapN:R,assertY:U,assertN:X,chainN:Z,chainY:$,tagBy:tt,chain:et,toBoolean:nt,encase:rt,fromNullable:it,all:at,any:ot,isY:st,isN:ct,ys:ut,ns:lt,concatWith:ft}=V,gt=it;t.Eall=at,t.Eany=ot,t.EassertN=X,t.EassertY=U,t.Ebifold=L,t.Ebimap=G,t.Echain=et,t.EchainN=Z,t.EchainY=$,t.EconcatWith=ft,t.Eencase=rt,t.EfromNullable=it,t.EgetOr=q,t.EgetWith=z,t.EisN=ct,t.EisY=st,t.Either=V,t.Emap=H,t.EmapN=R,t.EmapY=Q,t.Ens=lt,t.EtagBy=tt,t.EtoBoolean=nt,t.Eys=ut,t.N=C,t.StaticSumTypeError=p,t.Y=J,t.all=at,t.any=ot,t.assertN=X,t.assertY=U,t.bifold=L,t.bimap=G,t.boundToString=o,t.chain=et,t.chainAll=O,t.chainN=Z,t.chainY=$,t.concatWith=ft,t.decorate=_,t.decoratedEither=V,t.either=k,t.emptyInstance=f,t.encase=rt,t.errMessage=d,t.externalEither=k,t.externalMaybe=D,t.externalTags=K,t.fold=S,t.fromNullable=gt,t.getOr=q,t.getTags=y,t.getWith=z,t.isN=ct,t.isY=st,t.map=H,t.mapAll=j,t.mapN=R,t.mapY=Q,t.maybe=D,t.ns=lt,t.otherwise=n,t.pipe=(...t)=>{if(0==t.length)throw new Error("You must provide a non-empty spread of functions.");return n=>e(n,...t)},t.run=e,t.tagBy=tt,t.tagName=c,t.tags=K,t.toBoolean=nt,t.toJSON=t=>i((function t({value:e,isPojo:n,isArray:r,valueStag:a,emptyStag:o}){return a?i(t,e.value):o?null:n?g(Object.entries(e).map((e=>i(t,e)))):r?e.map((e=>i(t,e))):e}),t),t.toString=a,t.typeName=s,t.valueInstance=l,t.ys=ut,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=sum-type.umd.min.js.map