UNPKG

range-analyzer

Version:

A library to help you analyze and manage ranges

3 lines (2 loc) 2.36 kB
import{__assign as o,__spreadArray as r}from"tslib";var t=function(o){return/^-?\d+$/.test(o)},f=function(f,n,i){void 0===i&&(i={from:void 0,to:void 0});var u=f.map((function(r){if(r.from=t(r.from)?r.from:void 0,r.to=t(r.to)?r.to:void 0,t(r.from)&&t(r.to)){var f=o({},r);r.from=Math.min(f.from,f.to),r.to=Math.max(f.from,f.to)}return r})),l={from:t(null==n?void 0:n.from)?null==n?void 0:n.from:void 0,to:t(null==n?void 0:n.to)?null==n?void 0:n.to:void 0};if(t(l.from)&&t(l.to)){var a=o({},l);l.from=Math.min(a.from,a.to),l.to=Math.max(a.from,a.to)}var v={from:t(null==i?void 0:i.from)?null==i?void 0:i.from:void 0,to:t(null==i?void 0:i.to)?null==i?void 0:i.to:void 0};if(t(v.from)&&t(v.to)){var e=o({},v);v.from=Math.min(e.from,e.to),v.to=Math.max(e.from,e.to)}var d=u.map((function(o){return o.from})),h=r(r([],d,!0),[null==l?void 0:l.from,null==v?void 0:v.from],!1).filter((function(o){return void 0!==o})),c=u.map((function(o){return o.to})),p=r(r([],c,!0),[null==l?void 0:l.to,null==v?void 0:v.to],!1).filter((function(o){return void 0!==o})),M=r(r([],h,!0),p,!0),s=0,x=0;M.length>0&&(s=Math.max.apply(Math,M),x=Math.min.apply(Math,M));var b=Math.abs(s),g=Math.abs(x),y=Math.max(b,g)+1,$=-y;return u=u.map((function(o){return o.from=t(o.from)?o.from:$,o.to=t(o.to)?o.to:y,o})),l={from:t(l.from)?l.from:$,to:t(l.to)?l.to:y},v={from:t(v.from)?v.from:$,to:t(v.to)?v.to:y},u=u.slice().sort((function(o,r){var t=o.to-o.from,f=r.to-r.from;if(o.from>r.from)return 1;if(o.from===r.from){if(o.from+t>r.from+f)return-1;if(o.from+t===r.from+f)return 0;if(o.from+t<r.from+f)return 1}return-1})),m(u,l,[],v).map((function(o){return{from:o.from===$?void 0:o.from,to:o.to===y?void 0:o.to}}))},m=function(o,r,t,f){var n=t;if(null===r||0===o.length)return null===r||t.some((function(o){return o.from===(null==r?void 0:r.from)&&o.to===(null==r?void 0:r.to)}))||n.push(r),n=(n=n.filter((function(o){return o.from<=f.to&&o.to>=f.from}))).map((function(o){return function(o,r){return{from:o.from<r.from?r.from:o.from,to:o.to>r.to?r.to:o.to}}(o,f)}));var i=r.to-r.from;if(r.from>=o[0].from&&r.from<=o[0].to)r.from+i<=o[0].to?r=null:r.from=o[0].to+1;else if(r.from<o[0].from&&r.from+i>=o[0].from){var u=r.to,l={from:r.from,to:o[0].from-1};t.push(l),u>o[0].to?r.from=o[0].to+1:r=null}return o.shift(),m(o,r,t,f)};export{f as getAvailableRanges}; //# sourceMappingURL=index.es.js.map