UNPKG

extra-map.web

Version:

A group of functions for working with Maps {web}.

1 lines 9.89 kB
function IDENTITY(n){return n}function COMPARE(n,r){return n<r?-1:n>r?1:0}function fromRange(n,r,e=1){for(var t=(r-n)/e,u=[],o=0;o<t;++o,n+=e)u.push(n);return u}function index(n,r){var e=n.length;return r>=0?Math.min(r,e):Math.max(e+r,0)}function get$1(n,r){return n[index(n,r)]}function getAll$1(n,r){return r.map((r=>get$1(n,r)))}function last(n,r){return n.length>0?n[n.length-1]:r}function subsequences(n,r=-1){return[...isubsequences(n,r)]}function*isubsequences(n,r=-1){var e=n.length;if(!(r>e))if(r!==e)if(0!==r&&0!==e){var t=n.slice(0,-1);for(var u of(yield*isubsequences(t,r),isubsequences(t,r-1)))u.push(n[e-1]),yield u}else yield[];else yield n}function randomValue(n,r=Math.random){return n[Math.floor(r()*n.length)]}function randomSubsequence(n,r=-1,e=Math.random){return r>n.length?null:r>=0?randomSubsequenceFixed(n,r,e):randomSubsequenceAll(n,e)}function randomSubsequenceFixed(n,r,e){var t=fromRange(0,n.length);return randomPermutation$(t,r,e).sort(),getAll$1(n,t)}function randomSubsequenceAll(n,r){var e=[];for(var t of n)r()<.5&&e.push(t);return e}function randomPermutation$(n,r=-1,e=Math.random){var t=n.length;if(r>t)return n;r=r>=0?r:Math.floor((t+1)*e());for(var u=0;u<r;++u){var o=u+Math.floor((t-u)*e()),a=n[u];n[u]=n[o],n[o]=a}return n.length=r,n}function some$1(n,r=null){return r?n.some(r):someBoolean(n)}function someBoolean(n){for(var r=0,e=n.length;r<e;++r)if(n[r])return!0;return!1}function is(n){return n instanceof Map}function keys(n){return n.keys()}function values(n){return n.values()}function entries(n){return n.entries()}function from(n){return new Map(n)}function from$(n){return n instanceof Map?n:new Map(n)}function fromLists(n){var[r,e]=n,t=e[Symbol.iterator](),u=new Map;for(var o of r)u.set(o,t.next().value);return u}function fromKeys(n,r=null){r=r||IDENTITY;var e=new Map,t=-1;for(var u of n)e.set(u,r(u,++t,n));return e}function fromValues(n,r=null){r=r||IDENTITY;var e=new Map,t=-1;for(var u of n)e.set(r(u,++t,n),u);return e}function compare(n,r,e=null,t=null){e=e||COMPARE,t=t||IDENTITY;var u=unionKeys(n,r);for(var o of u){if(!n.has(o))return-1;if(!r.has(o))return 1;var a=e(t(n.get(o),o,n),t(r.get(o),o,r));if(0!==a)return a}return 0}function isEqual(n,r,e=null,t=null){return n.size===r.size&&0===compare(n,r,e,t)}function size(n){return n.size}function isEmpty(n){return 0===n.size}function get(n,r){return n.get(r)}function getAll(n,r){return r.map((r=>n.get(r)))}function getPath(n,r){for(var e of r)n=is(n)?n.get(e):void 0;return n}function hasPath(n,r){for(var e of r){if(!is(n))return!1;n=n.get(e)}return!0}function set(n,r,e){return new Map(n).set(r,e)}function set$(n,r,e){return n.set(r,e)}function setPath$(n,r,e){var t=getPath(n,r.slice(0,-1));return is(t)&&t.set(last(r),e),n}function swap(n,r,e){return swap$(new Map(n),r,e)}function swap$(n,r,e){var t=n.get(r);return n.set(r,n.get(e)),n.set(e,t),n}function remove(n,r){return remove$(new Map(n),r)}function remove$(n,r){return n.delete(r),n}function removePath$(n,r){var e=getPath(n,r.slice(0,-1));return is(e)&&e.delete(last(r)),n}function count(n,r){var e=0;for(var[t,u]of n)r(u,t,n)&&++e;return e}function countAs(n,r=null){r=r||IDENTITY;var e=new Map;for(var[t,u]of n){var o=r(u,t,n),a=e.get(o)||0;e.set(o,a+1)}return e}function min(n,r=null,e=null){return rangeEntries(n,r,e)[0][1]}function minEntry(n,r=null,e=null){return rangeEntries(n,r,e)[0]}function max(n,r=null,e=null){return rangeEntries(n,r,e)[1][1]}function maxEntry(n,r=null,e=null){return rangeEntries(n,r,e)[1]}function range(n,r=null,e=null){var[t,u]=rangeEntries(n,r,e);return[t[1],u[1]]}function rangeEntries(n,r=null,e=null){r=r||COMPARE,e=e||IDENTITY;var t,u,o,a,f,i,s=0;for(var[l,c]of n){var v=e(c,l,n);(0===s||r(v,o)<0)&&(t=l,u=c,o=v),(0===s||r(v,i)>0)&&(a=l,f=c,i=v),++s}return[[t,u],[a,f]]}function head(n,r=[]){for(var e of n)return e;return r}function tail(n){return drop(n,1)}function take(n,r=1){var e=new Map,t=-1;for(var[u,o]of n){if(++t>=r)break;e.set(u,o)}return e}function take$(n,r=1){var e=-1;for(var t of n.keys())++e>=r&&n.delete(t);return n}function drop(n,r=1){var e=new Map,t=-1;for(var[u,o]of n)++t>=r&&e.set(u,o);return e}function drop$(n,r=1){var e=-1;for(var t of n.keys()){if(++e>=r)break;n.delete(t)}return n}function*subsets(n,r=-1){for(var e of subsequences([...n.keys()],r))yield filterAt(n,e)}function randomKey(n,r=Math.random){return randomValue([...n.keys()],r)}function randomEntry(n,r=Math.random){return randomValue([...n],r)}function randomSubset(n,r=-1,e=Math.random){var t=randomSubsequence([...n.keys()],r,e);return filterAt(n,t)}function has(n,r){return n.has(r)}function hasValue(n,r,e=null,t=null){return void 0!==searchValue(n,r,e,t)}function hasEntry(n,r,e=null,t=null){e=e||COMPARE,t=t||IDENTITY;var[u,o]=r;return n.has(u)&&0===e(t(n.get(u),u,n),o)}function hasSubset(n,r,e=null,t=null){e=e||COMPARE,t=t||IDENTITY;for(var[u,o]of r){if(!n.has(u))return!1;if(0!==e(t(n.get(u),u,n),t(o,u,r)))return!1}return!0}function find(n,r){for(var[e,t]of n)if(r(t,e,n))return t}function findAll(n,r){var e=[];for(var[t,u]of n)r(u,t,n)&&e.push(u);return e}function search(n,r){for(var[e,t]of n)if(r(t,e,n))return e}function searchAll(n,r){var e=[];for(var[t,u]of n)r(u,t,n)&&e.push(t);return e}function searchValue(n,r,e=null,t=null){e=e||COMPARE;var u=(t=t||IDENTITY)(r,null,null);for(var[o,a]of n){if(0===e(t(a,o,n),u))return o}}function searchValueAll(n,r,e=null,t=null){e=e||COMPARE;var u=(t=t||IDENTITY)(r,null,null),o=[];for(var[a,f]of n){0===e(t(f,a,n),u)&&o.push(a)}return o}function forEach(n,r){for(var[e,t]of n)r(t,e,n)}function some(n,r){for(var[e,t]of n)if(r(t,e,n))return!0;return!1}function every(n,r){for(var[e,t]of n)if(!r(t,e,n))return!1;return!0}function map(n,r){var e=new Map;for(var[t,u]of n)e.set(t,r(u,t,n));return e}function map$(n,r){for(var[e,t]of n)n.set(e,r(t,e,n));return n}function reduce(n,r,e){var t=arguments.length<=2;for(var[u,o]of n)t?(e=o,t=!1):e=r(e,o,u,n);return e}function filter(n,r){var e=new Map;for(var[t,u]of n)r(u,t,n)&&e.set(t,u);return e}function filter$(n,r){for(var[e,t]of n)r(t,e,n)||n.delete(e);return n}function filterAt(n,r){var e=new Map;for(var t of r)e.set(t,n.get(t));return e}function filterAt$(n,r){for(var e of n.keys())r.includes(e)||n.delete(e);return n}function reject(n,r){var e=new Map;for(var[t,u]of n)r(u,t,n)||e.set(t,u);return e}function reject$(n,r){for(var[e,t]of n)r(t,e,n)&&n.delete(e);return n}function rejectAt(n,r){return rejectAt$(new Map(n),r)}function rejectAt$(n,r){for(var e of r)n.delete(e);return n}function flat(n,r=-1,e=null,t=null){e=e||IDENTITY,t=t||is;return flatTo$(new Map,n,r,e,t)}function flatTo$(n,r,e,t,u){for(var[o,a]of r){var f=t(a,o,r);0!==e&&u(f,o,r)?flatTo$(n,f,e-1,t,u):n.set(o,f)}return n}function flatMap(n,r=null,e=null){r=r||IDENTITY,e=e||is;var t=new Map;for(var[u,o]of n){var a=r(o,u,n);e(a,u,n)?concat$(t,a):t.set(u,a)}return t}function zip(n,r=null,e=null,t){r=r||IDENTITY,e=e||some$1;var u=unionKeys(...n),o=new Map;for(var a of u){if(e(n.map((n=>!n.has(a)))))break;var f=n.map((n=>n.has(a)?n.get(a):t));o.set(a,r(f,a,null))}return o}function partition(n,r){var e=new Map,t=new Map;for(var[u,o]of n)r(o,u,n)?e.set(u,o):t.set(u,o);return[e,t]}function partitionAs(n,r){r=r||IDENTITY;var e=new Map;for(var[t,u]of n){var o=r(u,t,n);e.has(o)||e.set(o,new Map),e.get(o).set(t,u)}return e}function chunk(n,r=1,e=r){for(var t=[...n.keys()],u=[],o=0,a=t.length;o<a;o+=e)u.push(filterAt(n,t.slice(o,o+r)));return u}function concat(...n){return concat$(new Map,...n)}function concat$(n,...r){for(var e of r)for(var[t,u]of e)n.set(t,u);return n}function join(n,r=",",e="="){var t="";for(var[u,o]of n)t+=u+e+o+r;return t.slice(0,-r.length)}function isDisjoint(n,r){for(var[e]of r)if(n.has(e))return!1;return!0}function unionKeys(...n){var r=new Set;for(var e of n)for(var[t]of e)r.add(t);return r}function union(n,r,e=null){return union$(new Map(n),r,e)}function union$(n,r,e=null){e=e||IDENTITY;for(var[t,u]of r)n.has(t)?n.set(t,e(n.get(t),u)):n.set(t,u);return n}function intersectionKeys(...n){var r=new Set;if(0===n.length)return r;var e=n[0],t=n.slice(1);n:for(var u of e.keys()){for(var o of t)if(!o.has(u))continue n;r.add(u)}return r}function intersection(n,r,e=null){e=e||IDENTITY;var t=new Map;for(var[u,o]of r)n.has(u)&&t.set(u,e(n.get(u),o));return t}function intersection$(n,r,e=null){e=e||IDENTITY;var t=[];for(var[u,o]of[...n])r.has(u)||t.push(u),n.set(u,e(o,r.get(u)));return rejectAt$(n,t)}function difference(n,r){return difference$(new Map(n),r)}function difference$(n,r){for(var[e]of r)n.delete(e);return n}function symmetricDifference(n,r){return symmetricDifference$(new Map(n),r)}function symmetricDifference$(n,r){for(var[e,t]of r)n.has(e)?n.delete(e):n.set(e,t);return n}function*cartesianProduct(n,r=null){r=r||IDENTITY;for(var e=n.length,t=n.map((n=>[...n.keys()])),u=t.map((n=>n.length)),o=t.map((n=>0)),a=0;;++a){for(var f=new Map,i=0;i<e;++i){var s=o[i],l=n[i],c=t[i][s];f.set(c,l.get(c))}yield r(f,a,null);for(var v=e-1;v>=0&&!(++o[v]<u[v]);--v)o[v]=0;if(v<0)break}}export{cartesianProduct,chunk,compare,concat,concat$,count,countAs,difference,difference$,drop,drop$,entries,randomEntry as entry,every,filter,filter$,filterAt,filterAt$,find,findAll,flat,flatMap,forEach,from,from$,from as fromEntries,from$ as fromEntries$,fromKeys,fromLists,fromValues,get,getAll,getPath,has,hasEntry,has as hasKey,hasPath,hasSubset,hasValue,head,intersection,intersection$,intersectionKeys,is,isDisjoint,isEmpty,isEqual,join,randomKey as key,keys,size as length,map,map$,max,maxEntry,min,minEntry,partition,partitionAs,randomEntry,randomKey,randomSubset,range,rangeEntries,reduce,reject,reject$,rejectAt,rejectAt$,remove,remove$,removePath$,search,searchAll,searchValue,searchValueAll,set,set$,setPath$,size,some,randomSubset as subset,subsets,swap,swap$,symmetricDifference,symmetricDifference$,tail,take,take$,union,union$,unionKeys,values,zip};