declarative-js
Version:
_declarative-js_ is modern JavaScript library, that helps to: - tackle array transformation with built in JavaScript array api (e.g. `array.filter(toBe.unique())`), - provide a type-level solution for representing optional values instead of null referen
2 lines (1 loc) • 9.43 kB
JavaScript
;var N=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var ie=(e,t)=>{for(var u in t)N(e,u,{get:t[u],enumerable:!0})},ae=(e,t,u,p)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of ne(t))!oe.call(e,s)&&s!==u&&N(e,s,{get:()=>t[s],enumerable:!(p=re(t,s))||p.enumerable});return e};var pe=e=>ae(N({},"__esModule",{value:!0}),e);var ue={};ie(ue,{Filter:()=>B,JMap:()=>v,Map:()=>F,Mapper:()=>C,Optional:()=>O,Reducer:()=>P,Sort:()=>I,d:()=>$,optional:()=>_,toBe:()=>j});module.exports=pe(ue);var U=e=>e!=null?Array.isArray(e)?e:[e]:[];var O=W;function W(e){this.value=e}W.prototype={orElse(e){return this.isPresent()?this.value:e},isPresent(){return this.value!=null},isAbsent(){return this.value==null},orElseGet(e){return this.isPresent()?this.value:e()},orElseThrow(e){if(!this.isPresent())throw new Error(e);return this.value},ifPresent(e){this.isPresent()&&e()},ifAbsent(e){this.isAbsent()&&e()},get(){if(this.value==null)throw new Error("Value is not defined");return this.value},map(e){return this.isPresent()?new O(e(this.value)):new O},filter(e){return this.isPresent()?e(this.value)?this:new O:new O},toArray(){return U(this.value)}};var _=function(e){return new O(e)};var J=Object.prototype,Te=J.hasOwnProperty;function w(e,t){if(e===t)return!0;if(e&&t&&typeof e=="object"&&typeof t=="object"){if(e.constructor!==t.constructor)return!1;if(Array.isArray(e)){if(e.length!==t.length)return!1;for(let p=e.length-1;p>=0;p--)if(!w(e[p],t[p]))return!1;return!0}if(e.constructor===RegExp)return e.source===t.source&&e.flags===t.flags;if(e.valueOf!==J.valueOf)return e.valueOf()===t.valueOf();if(e.toString!==J.toString)return e.toString()===t.toString();let u=Object.keys(e);if(u.length!==Object.keys(t).length)return!1;for(let p=u.length-1;p>=0;p--)if(!Te.call(t,u[p]))return!1;for(let p=u.length-1;p>=0;p--){let s=u[p];if(!w(e[s],t[s]))return!1}return!0}return e!==e&&t!==t}var j;(d=>{function e(y){return y!=null}d.present=e;function t(y){return y!=null&&(Array.isArray(y)?y.length!==0:!0)&&(typeof y=="string"?y!=="":!0)}d.notEmpty=t;function u(y){return function(b){return w(y,b)}}d.equal=u;function p(y){return function(b){return!w(y,b)}}d.notEqual=p;function s(){let y=new Set;return function(b){let K=b;return typeof b=="object"&&(K=JSON.stringify(b)),y.has(K)?!1:(y.add(K),!0)}}d.unique=s;function o(y){let f=new Set;if(typeof y=="string")return function(K){let A=K[y];return f.has(A)?!1:(f.add(A),!0)};if(typeof y=="function")return function(K){let A=y(K);return f.has(A)?!1:(f.add(A),!0)};throw new Error(`toBe.uniqueBy expected to have as a parameter string or function, instead got ${typeof y}`)}d.uniqueBy=o;function m(y){let f=!1;return function(K,A){return(A===0||f)&&(f=y(K)),f}}d.takeWhile=m})(j||(j={}));var B;(p=>{function e(s){let o=!1;return function(d,y){return(y===0||o)&&(o=s(d)),o}}p.takeWhile=e;function t(s){let o=!1;return function(d,y){return(y===0||!o)&&(o=!s(d)),o}}p.skipWhile=t;function u(s,o){return function(d,y){try{return s(d)}catch(f){return o&&o(f,d,y),!1}}}p.skipOnError=u})(B||(B={}));function R(e){if(typeof e!="string")throw new Error(`Resolved key must be a string, actual: value - ${e} type - ${typeof e}`);return e}var S="_immutable",k=e=>(Object.getOwnPropertyDescriptor(e,S)&&Object.seal(e.storage),e),E=e=>Object.getOwnPropertyDescriptor(e,S)?Object.seal(e):e,M=(e,t)=>e.length-1===t;function H(e,t,u){throw new Error(`Key: "${u}" has duplicates`)}var Q=e=>(t,u,p,s)=>{let o=R(e(u));if(Object.prototype.hasOwnProperty.call(t,o))throw new Error(`Key: "${o}" has duplicates`);return t[o]=u,M(s,p)?E(t):t},X=(e,t,u)=>(p,s,o,m)=>{let d=R(e(s));return Object.prototype.hasOwnProperty.call(p,d)?p[d]=u(p[d],t(s),d):p[d]=t(s),M(m,o)?E(p):p};var Y=e=>(t,u,p,s)=>{let o=R(e(u));if(t.containsKey(o))throw new Error(`Key: "${o}" has duplicates`);return t.put(o,u),M(s,p)?k(t):t},Z=(e,t)=>(u,p,s,o)=>{let m=R(e(p));if(u.containsKey(m))throw new Error(`Key: "${m}" has duplicates`);return u.put(m,t(p)),M(o,s)?k(u):u};function ee(e={}){this.storage=e}var v=ee;ee.prototype={put(e,t){this.storage[e]=t},get(e){return this.storage[e]},keys(){return Object.keys(this.storage)},values(){return Object.keys(this.storage).map(e=>this.storage[e])},containsKey(e){return Object.prototype.hasOwnProperty.call(this.storage,e)},containsValue(e){return this.values().some(t=>t===e)},entries(){return this.keys().map(e=>({key:e,value:this.storage[e]}))},size(){return this.keys().length},toObject(){return{...this.storage}}};var P;(G=>{function e(r){return new v(r)}G.Map=e;function t(){let r=new v;return Object.defineProperty(r,S,{value:!0,enumerable:!1})}G.ImmutableMap=t;function u(){return Object.defineProperty({},S,{value:!0,enumerable:!1})}G.ImmutableObject=u;function p(r,T=a=>a){let a=n=>{let i=n;return typeof i.get=="function"&&typeof i.put=="function"};switch(typeof r){case"string":{let n=r;return function(i,l,c,x){let h=l[n];if(typeof h=="string")if(a(i)){let g=i.get(h);return g?g.push(T(l)):i.put(h,[T(l)]),M(x,c)?k(i):i}else{let g=i,L=g[h];return L!==void 0?L.push(T(l)):g[h]=[T(l)],M(x,c)?E(g):i}throw new Error('Value of "'+n+'" in groupBy must be string, instead get: '+typeof l[n])}}case"function":return function(n,i,l,c){let x=R(r(i));if(a(n)){let h=n.get(x);return h!==void 0?h.push(T(i)):n.put(x,[T(i)]),M(c,l)?k(n):n}else{let h=n,g=h[x];return g!==void 0?g.push(T(i)):h[x]=[T(i)],M(c,l)?E(h):n}};default:throw new Error(`Reducer.groupBy function accepts as a paramter string or callback, instead got ${typeof r}`)}}G.groupBy=p;function s(r,T=a=>a){switch(typeof r){case"string":{let a=r;return function(n,i,l,c){let x=i[a];if(typeof x=="string"){let h=n[x];return h!==void 0?h.push(T(i)):n[x]=[T(i)],M(c,l)?E(n):n}throw new Error('Value of "'+a+'" in groupBy must be string, instead get: '+typeof i[a])}}case"function":return function(a,n,i,l){let c=R(r(n)),x=a[c];return x!==void 0?x.push(T(n)):a[c]=[T(n)],M(l,i)?E(a):a};default:throw new Error(`Reducer.groupBy function accepts as a paramter string or callback, instead got ${typeof r}`)}}G.groupByObject=s,G.flat=(r,T)=>{if(Array.isArray(T))for(let a=0;a<T.length;a++){let n=T[a];r[r.length]=n}else r[r.length]=T;return r};function m(r,T){let a=T===void 0?Y(r):Z(r,T);return function(i,l,c,x){return a(i,l,c,x)}}G.toMap=m;function d(r,T,a){let i=T===void 0?Q(r):X(r,T,a||H);return function(c,x,h,g){return i(c,x,h,g)}}G.toObject=d;function y(r,T,a,n){return M(n,a)?Math.min(...n):0}G.min=y;function f(r,T,a,n){return M(n,a)?Math.max(...n):0}G.max=f,G.sum=(r,T)=>r+T,G.MergeStrategy={OVERRIDE:()=>!0,UNIQUE:r=>r==null,CHECKED:(r,T)=>r==null||w(r,T)};function A(r=G.MergeStrategy.OVERRIDE){return function(a,n){for(let i of Object.keys(n)){let l=a[i],c=n[i];if(!r(l,c,i))throw new Error(`Failed to merge objects. Check the merging predicate ("strategy") and objects in an array with key: ${i}`);a[i]=c}return a}}G.toMergedObject=A;function se(r,T){let a=T||((l,c)=>[l,c]),n=!1,i=r.length;return function(c,x,h){if(n)return c;let g=r[h];return i==h?(n=!0,c):(c.push(a(x,g)),c)}}G.zip=se;function ce(...r){let T=!1;return function(n,i,l){if(T)return n;if(r.some(x=>l>=x.length))return T=!0,n;let c=[i];for(let x=0;x<r.length;x++)c.push(r[x][l]);return n.push(c),n}}G.zipAll=ce;function ye(){let r;return function(a,n,i,l){r==null&&(r=l.map(c=>c.length).reduce(y));for(let c=0;c<r;c++){let x=a[c];x?x.push(n[c]):a[c]=[n[c]]}return a}}G.unzip=ye,G.Partition=()=>[[],[]];function fe(r){let T=`Predicate for 'partitionBy' can be key of object, predicate function or partial object to match, instead got '${r}'`,a;if(typeof r=="string")a=n=>!!n[r];else if(typeof r=="function")a=n=>r(n);else if(typeof r=="object"){if(r===null)throw new Error(T);a=n=>Object.keys(r).every(i=>n[i]===r[i])}else throw new Error(T);return function(i,l){return a(l)?i[0].push(l):i[1].push(l),i}}G.partitionBy=fe;function de(){return function(T,a,n,i){return i.length-1!=n&&T.push([i[n],i[n+1]]),T}}G.pairwise=de;function me(r,T){let a=T;return function(i,l){let c=r(a,l);return a=r(a,l),i.push(c),i}}G.scan=me})(P||(P={}));var C;(t=>t.toObjValues=u=>Object.keys(u).map(p=>u[p]))(C||(C={}));function V(e,t){let u=e.indexOf(t);return u===-1?e.length:u}function z(...e){return function(t,u){for(let p=0;p<e.length;p++){let{order:s,toValue:o}=e[p],m=V(s,o(t)),d=V(s,o(u));if(m!==d)return m>d?1:-1}return 0}}function te(e,t){return function(u,p){let s=V(t,u[e]),o=V(t,p[e]);return s!==o?s>o?1:-1:0}}function D(e){return function(...t){return function(u,p){for(let s=0;s<t.length;s++){let o=t[s],m=o(u),d=o(p);if(m!==d)return m>d?e.true:e.false}return 0}}}function q(e){return function(...t){return function(u,p){for(let s=0;s<t.length;s++){let o=t[s],m=u[o],d=p[o];if(m!==d)return m>d?e.true:e.false}return 0}}}var I;(s=>{function e(...o){let m={true:1,false:-1},d=typeof o[0]=="string"?q(m)(...o):D(m)(...o);return function(f,b){return d(f,b)}}s.ascendingBy=e;function t(...o){let m={true:-1,false:1},d=typeof o[0]=="string"?q(m)(...o):D(m)(...o);return function(f,b){return d(f,b)}}s.descendingBy=t;function u(...o){let m=typeof o[0]=="string"?te(o[0],o[1]):z(...o);return function(y,f){return m(y,f)}}s.by=u;function p(o){return function(d,y){return z({toValue:b=>b,order:o})(d,y)}}s.orderedBy=p})(I||(I={}));var F=e=>new v(e);var $;(f=>(f.Optional=O,f.optional=_,f.toBe=j,f.Filter=B,f.Reducer=P,f.Mapper=C,f.Sort=I,f.JMap=v,f.Map=F))($||($={}));