UNPKG

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) 8.95 kB
var q=e=>e!=null?Array.isArray(e)?e:[e]:[];var E=F;function F(e){this.value=e}F.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 E(e(this.value)):new E},filter(e){return this.isPresent()?e(this.value)?this:new E:new E},toArray(){return q(this.value)}};var $=function(e){return new E(e)};var N=Object.prototype,te=N.hasOwnProperty;function O(e,n){if(e===n)return!0;if(e&&n&&typeof e=="object"&&typeof n=="object"){if(e.constructor!==n.constructor)return!1;if(Array.isArray(e)){if(e.length!==n.length)return!1;for(let T=e.length-1;T>=0;T--)if(!O(e[T],n[T]))return!1;return!0}if(e.constructor===RegExp)return e.source===n.source&&e.flags===n.flags;if(e.valueOf!==N.valueOf)return e.valueOf()===n.valueOf();if(e.toString!==N.toString)return e.toString()===n.toString();let u=Object.keys(e);if(u.length!==Object.keys(n).length)return!1;for(let T=u.length-1;T>=0;T--)if(!te.call(n,u[T]))return!1;for(let T=u.length-1;T>=0;T--){let y=u[T];if(!O(e[y],n[y]))return!1}return!0}return e!==e&&n!==n}var B;(d=>{function e(c){return c!=null}d.present=e;function n(c){return c!=null&&(Array.isArray(c)?c.length!==0:!0)&&(typeof c=="string"?c!=="":!0)}d.notEmpty=n;function u(c){return function(b){return O(c,b)}}d.equal=u;function T(c){return function(b){return!O(c,b)}}d.notEqual=T;function y(){let c=new Set;return function(b){let K=b;return typeof b=="object"&&(K=JSON.stringify(b)),c.has(K)?!1:(c.add(K),!0)}}d.unique=y;function o(c){let f=new Set;if(typeof c=="string")return function(K){let A=K[c];return f.has(A)?!1:(f.add(A),!0)};if(typeof c=="function")return function(K){let A=c(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 c}`)}d.uniqueBy=o;function m(c){let f=!1;return function(K,A){return(A===0||f)&&(f=c(K)),f}}d.takeWhile=m})(B||(B={}));var S;(T=>{function e(y){let o=!1;return function(d,c){return(c===0||o)&&(o=y(d)),o}}T.takeWhile=e;function n(y){let o=!1;return function(d,c){return(c===0||!o)&&(o=!y(d)),o}}T.skipWhile=n;function u(y,o){return function(d,c){try{return y(d)}catch(f){return o&&o(f,d,c),!1}}}T.skipOnError=u})(S||(S={}));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 j="_immutable",k=e=>(Object.getOwnPropertyDescriptor(e,j)&&Object.seal(e.storage),e),w=e=>Object.getOwnPropertyDescriptor(e,j)?Object.seal(e):e,M=(e,n)=>e.length-1===n;function L(e,n,u){throw new Error(`Key: "${u}" has duplicates`)}var U=e=>(n,u,T,y)=>{let o=R(e(u));if(Object.prototype.hasOwnProperty.call(n,o))throw new Error(`Key: "${o}" has duplicates`);return n[o]=u,M(y,T)?w(n):n},W=(e,n,u)=>(T,y,o,m)=>{let d=R(e(y));return Object.prototype.hasOwnProperty.call(T,d)?T[d]=u(T[d],n(y),d):T[d]=n(y),M(m,o)?w(T):T};var H=e=>(n,u,T,y)=>{let o=R(e(u));if(n.containsKey(o))throw new Error(`Key: "${o}" has duplicates`);return n.put(o,u),M(y,T)?k(n):n},Q=(e,n)=>(u,T,y,o)=>{let m=R(e(T));if(u.containsKey(m))throw new Error(`Key: "${m}" has duplicates`);return u.put(m,n(T)),M(o,y)?k(u):u};function X(e={}){this.storage=e}var v=X;X.prototype={put(e,n){this.storage[e]=n},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(n=>n===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(t){return new v(t)}G.Map=e;function n(){let t=new v;return Object.defineProperty(t,j,{value:!0,enumerable:!1})}G.ImmutableMap=n;function u(){return Object.defineProperty({},j,{value:!0,enumerable:!1})}G.ImmutableObject=u;function T(t,p=a=>a){let a=r=>{let i=r;return typeof i.get=="function"&&typeof i.put=="function"};switch(typeof t){case"string":{let r=t;return function(i,l,s,x){let h=l[r];if(typeof h=="string")if(a(i)){let g=i.get(h);return g?g.push(p(l)):i.put(h,[p(l)]),M(x,s)?k(i):i}else{let g=i,D=g[h];return D!==void 0?D.push(p(l)):g[h]=[p(l)],M(x,s)?w(g):i}throw new Error('Value of "'+r+'" in groupBy must be string, instead get: '+typeof l[r])}}case"function":return function(r,i,l,s){let x=R(t(i));if(a(r)){let h=r.get(x);return h!==void 0?h.push(p(i)):r.put(x,[p(i)]),M(s,l)?k(r):r}else{let h=r,g=h[x];return g!==void 0?g.push(p(i)):h[x]=[p(i)],M(s,l)?w(h):r}};default:throw new Error(`Reducer.groupBy function accepts as a paramter string or callback, instead got ${typeof t}`)}}G.groupBy=T;function y(t,p=a=>a){switch(typeof t){case"string":{let a=t;return function(r,i,l,s){let x=i[a];if(typeof x=="string"){let h=r[x];return h!==void 0?h.push(p(i)):r[x]=[p(i)],M(s,l)?w(r):r}throw new Error('Value of "'+a+'" in groupBy must be string, instead get: '+typeof i[a])}}case"function":return function(a,r,i,l){let s=R(t(r)),x=a[s];return x!==void 0?x.push(p(r)):a[s]=[p(r)],M(l,i)?w(a):a};default:throw new Error(`Reducer.groupBy function accepts as a paramter string or callback, instead got ${typeof t}`)}}G.groupByObject=y,G.flat=(t,p)=>{if(Array.isArray(p))for(let a=0;a<p.length;a++){let r=p[a];t[t.length]=r}else t[t.length]=p;return t};function m(t,p){let a=p===void 0?H(t):Q(t,p);return function(i,l,s,x){return a(i,l,s,x)}}G.toMap=m;function d(t,p,a){let i=p===void 0?U(t):W(t,p,a||L);return function(s,x,h,g){return i(s,x,h,g)}}G.toObject=d;function c(t,p,a,r){return M(r,a)?Math.min(...r):0}G.min=c;function f(t,p,a,r){return M(r,a)?Math.max(...r):0}G.max=f,G.sum=(t,p)=>t+p,G.MergeStrategy={OVERRIDE:()=>!0,UNIQUE:t=>t==null,CHECKED:(t,p)=>t==null||O(t,p)};function A(t=G.MergeStrategy.OVERRIDE){return function(a,r){for(let i of Object.keys(r)){let l=a[i],s=r[i];if(!t(l,s,i))throw new Error(`Failed to merge objects. Check the merging predicate ("strategy") and objects in an array with key: ${i}`);a[i]=s}return a}}G.toMergedObject=A;function re(t,p){let a=p||((l,s)=>[l,s]),r=!1,i=t.length;return function(s,x,h){if(r)return s;let g=t[h];return i==h?(r=!0,s):(s.push(a(x,g)),s)}}G.zip=re;function ne(...t){let p=!1;return function(r,i,l){if(p)return r;if(t.some(x=>l>=x.length))return p=!0,r;let s=[i];for(let x=0;x<t.length;x++)s.push(t[x][l]);return r.push(s),r}}G.zipAll=ne;function oe(){let t;return function(a,r,i,l){t==null&&(t=l.map(s=>s.length).reduce(c));for(let s=0;s<t;s++){let x=a[s];x?x.push(r[s]):a[s]=[r[s]]}return a}}G.unzip=oe,G.Partition=()=>[[],[]];function ae(t){let p=`Predicate for 'partitionBy' can be key of object, predicate function or partial object to match, instead got '${t}'`,a;if(typeof t=="string")a=r=>!!r[t];else if(typeof t=="function")a=r=>t(r);else if(typeof t=="object"){if(t===null)throw new Error(p);a=r=>Object.keys(t).every(i=>r[i]===t[i])}else throw new Error(p);return function(i,l){return a(l)?i[0].push(l):i[1].push(l),i}}G.partitionBy=ae;function pe(){return function(p,a,r,i){return i.length-1!=r&&p.push([i[r],i[r+1]]),p}}G.pairwise=pe;function Te(t,p){let a=p;return function(i,l){let s=t(a,l);return a=t(a,l),i.push(s),i}}G.scan=Te})(P||(P={}));var C;(n=>n.toObjValues=u=>Object.keys(u).map(T=>u[T]))(C||(C={}));function I(e,n){let u=e.indexOf(n);return u===-1?e.length:u}function _(...e){return function(n,u){for(let T=0;T<e.length;T++){let{order:y,toValue:o}=e[T],m=I(y,o(n)),d=I(y,o(u));if(m!==d)return m>d?1:-1}return 0}}function Y(e,n){return function(u,T){let y=I(n,u[e]),o=I(n,T[e]);return y!==o?y>o?1:-1:0}}function J(e){return function(...n){return function(u,T){for(let y=0;y<n.length;y++){let o=n[y],m=o(u),d=o(T);if(m!==d)return m>d?e.true:e.false}return 0}}}function z(e){return function(...n){return function(u,T){for(let y=0;y<n.length;y++){let o=n[y],m=u[o],d=T[o];if(m!==d)return m>d?e.true:e.false}return 0}}}var V;(y=>{function e(...o){let m={true:1,false:-1},d=typeof o[0]=="string"?z(m)(...o):J(m)(...o);return function(f,b){return d(f,b)}}y.ascendingBy=e;function n(...o){let m={true:-1,false:1},d=typeof o[0]=="string"?z(m)(...o):J(m)(...o);return function(f,b){return d(f,b)}}y.descendingBy=n;function u(...o){let m=typeof o[0]=="string"?Y(o[0],o[1]):_(...o);return function(c,f){return m(c,f)}}y.by=u;function T(o){return function(d,c){return _({toValue:b=>b,order:o})(d,c)}}y.orderedBy=T})(V||(V={}));var Z=e=>new v(e);var ee;(f=>(f.Optional=E,f.optional=$,f.toBe=B,f.Filter=S,f.Reducer=P,f.Mapper=C,f.Sort=V,f.JMap=v,f.Map=Z))(ee||(ee={}));export{S as Filter,v as JMap,Z as Map,C as Mapper,E as Optional,P as Reducer,V as Sort,ee as d,$ as optional,B as toBe};