UNPKG

range-analyzer

Version:

A library to help you analyze and manage ranges

3 lines (2 loc) 2.5 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var o=require("tslib"),r=function(o){return/^-?\d+$/.test(o)},t=function(o,r,f,m){var n=f;if(null===r||0===o.length)return null===r||f.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<=m.to&&o.to>=m.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,m)}));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 a=r.to,u={from:r.from,to:o[0].from-1};f.push(u),a>o[0].to?r.from=o[0].to+1:r=null}return o.shift(),t(o,r,f,m)};exports.getAvailableRanges=function(f,m,n){void 0===n&&(n={from:void 0,to:void 0});var i=f.map((function(t){if(t.from=r(t.from)?t.from:void 0,t.to=r(t.to)?t.to:void 0,r(t.from)&&r(t.to)){var f=o.__assign({},t);t.from=Math.min(f.from,f.to),t.to=Math.max(f.from,f.to)}return t})),a={from:r(null==m?void 0:m.from)?null==m?void 0:m.from:void 0,to:r(null==m?void 0:m.to)?null==m?void 0:m.to:void 0};if(r(a.from)&&r(a.to)){var u=o.__assign({},a);a.from=Math.min(u.from,u.to),a.to=Math.max(u.from,u.to)}var e={from:r(null==n?void 0:n.from)?null==n?void 0:n.from:void 0,to:r(null==n?void 0:n.to)?null==n?void 0:n.to:void 0};if(r(e.from)&&r(e.to)){var l=o.__assign({},e);e.from=Math.min(l.from,l.to),e.to=Math.max(l.from,l.to)}var v=i.map((function(o){return o.from})),d=o.__spreadArray(o.__spreadArray([],v,!0),[null==a?void 0:a.from,null==e?void 0:e.from],!1).filter((function(o){return void 0!==o})),s=i.map((function(o){return o.to})),p=o.__spreadArray(o.__spreadArray([],s,!0),[null==a?void 0:a.to,null==e?void 0:e.to],!1).filter((function(o){return void 0!==o})),_=o.__spreadArray(o.__spreadArray([],d,!0),p,!0),c=0,h=0;_.length>0&&(c=Math.max.apply(Math,_),h=Math.min.apply(Math,_));var M=Math.abs(c),y=Math.abs(h),g=Math.max(M,y)+1,x=-g;return i=i.map((function(o){return o.from=r(o.from)?o.from:x,o.to=r(o.to)?o.to:g,o})),a={from:r(a.from)?a.from:x,to:r(a.to)?a.to:g},e={from:r(e.from)?e.from:x,to:r(e.to)?e.to:g},i=i.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})),t(i,a,[],e).map((function(o){return{from:o.from===x?void 0:o.from,to:o.to===g?void 0:o.to}}))}; //# sourceMappingURL=index.js.map