ranges-set
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,16 @@ | ||
declare const enum Kind { | ||
Literal = 0, | ||
Range = 1 | ||
} | ||
declare type MRepr = MReprLiteral | MReprRange; | ||
declare type MReprs = MRepr[]; | ||
declare type MReprLiteral = { | ||
_kind: Kind.Literal; | ||
_text: string; | ||
}; | ||
declare type MReprRange = { | ||
_kind: Kind.Range; | ||
_min: number; | ||
_max: number; | ||
}; | ||
export declare function difference(textA: string, textB: string): string; | ||
@@ -6,3 +21,5 @@ export declare function equal(textA: string, textB: string): boolean; | ||
export declare function normalize(text: string): string; | ||
export declare function parse(text: string): MReprs; | ||
export declare function subset(textA: string, textB: string): boolean; | ||
export declare function union(textA: string, textB: string): string; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
var n=/^\s*(?:0|[1-9]\d*)\s*$/,t=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function i(n,t){if(n.kind!==t.kind)return n.kind-t.kind;switch(n.kind){case 0:return n.text>=t.text?n.text>t.text?1:0:-1;case 1:return n.min-t.min||n.max-t.max}}function r(n,t){if(n.kind!==t.kind)return null;switch(n.kind){case 0:return n.text===t.text?n:null;case 1:var i=Math.max(n.min,t.min),r=Math.min(n.max,t.max);return i>r?null:{kind:1,min:i,max:r}}}function e(n){return n.split(",").filter(Boolean).map(a).sort(i).reduce(x,[])}function a(i){if(n.test(i))return{kind:1,min:+i,max:+i};var r=t.exec(i);return r?{kind:1,min:+r[1],max:+r[2]}:{kind:0,text:i}}function m(n){return n.map(u).join()}function u(n){switch(n.kind){case 0:return n.text;case 1:return n.min===n.max?""+n.min:n.min+"-"+n.max}}function x(n,t){return 0!==n.length&&function(n,t){if(n.kind!==t.kind)return!1;switch(n.kind){case 0:return n.text===t.text;case 1:var i=n.min<=t.max+1&&n.max>=t.min||t.min<=n.max+1&&t.max>=n.min;return i&&(n.min=Math.min(n.min,t.min),n.max=Math.max(n.max,t.max)),i}}(n[n.length-1],t)||n.push(t),n}exports.difference=function(n,t){return m(function(n,t){var i=[];n:for(var r=0;r<n.length;++r){var e=n[r];switch(e.kind){case 0:for(var a=0;a<t.length;++a){var m=t[a];if(0===m.kind&&m.text===e.text)continue n}i.push(e);break;case 1:for(var u=0;u<t.length;++u){var x=t[u];if(1===x.kind){if(e.min>=x.min&&e.max<=x.max)continue n;if(e.min<=x.min&&e.max>=x.max){e.max>x.max&&n.splice(r+1,0,{kind:1,min:x.max+1,max:e.max}),e.min<x.min&&n.splice(r+1,0,{kind:1,min:e.min,max:x.min-1});continue n}e.min>=x.min&&e.min<=x.max?e.min=x.max+1:e.max>=x.min&&e.max<=x.max&&(e.max=x.min-1)}}i.push(e)}}return i}(e(n),e(t)))},exports.equal=function(n,t){return function(n,t){if(n.length!==t.length)return!1;for(var r=0;r<n.length;++r)if(0!==i(n[r],t[r]))return!1;return!0}(e(n),e(t))},exports.expand=function(n){return function(n){for(var t=[],i=0;i<n.length;++i){var r=n[i];switch(r.kind){case 0:t.push(r.text);break;case 1:for(var e=r.min;e<=r.max;++e)t.push(""+e)}}return t}(e(n))},exports.intersection=function(n,t){return m(function(n,t){for(var i=[],e=0;e<n.length;++e)for(var a=n[e],m=0;m<t.length;++m){var u=r(a,t[m]);null!==u&&i.push(u)}return i}(e(n),e(t)))},exports.normalize=function(n){return m(e(n))},exports.subset=function(n,t){return function(n,t){n:for(var i=0;i<t.length;++i){var r=t[i];switch(r.kind){case 0:for(var e=0;e<n.length;++e){var a=n[e];if(0===a.kind&&a.text===r.text)continue n}return!1;case 1:for(var m=0;m<n.length;++m){var u=n[m];if(1===u.kind&&r.min>=u.min&&r.max<=u.max)continue n}return!1}}return!0}(e(n),e(t))},exports.union=function(n,t){return m(e(n+","+t))}; | ||
var r=/^\s*(?:0|[1-9]\d*)\s*$/,n=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function t(r,n){if(r.t!==n.t)return r.t-n.t;switch(r.t){case 0:return r.u>=n.u?r.u>n.u?1:0:-1;case 1:return r.i-n.i||r.o-n.o}}function e(r,n){var t,e,u,i,f,o,c=[];r:for(t=0;t<r.length;++t){switch((e=r[t]).t){case 0:for(u=0;u<n.length;++u)if(0===(i=n[u]).t&&i.u===e.u)continue r;break;case 1:for(f=0;f<n.length;++f)if(1===(o=n[f]).t){if(e.i>=o.i&&e.o<=o.o)continue r;if(e.i<=o.i&&e.o>=o.o){e.o>o.o&&r.splice(t+1,0,{t:1,i:o.o+1,o:e.o}),e.i<o.i&&r.splice(t+1,0,{t:1,i:e.i,o:o.i-1});continue r}e.i>=o.i&&e.i<=o.o?e.i=o.o+1:e.o>=o.i&&e.o<=o.o&&(e.o=o.i-1)}}c.push(e)}return c}function u(r,n){if(r.length!==n.length)return!1;for(var e=0;e<r.length;++e)if(t(r[e],n[e]))return!1;return!0}function i(r){var n,t,e,u=[];for(n=0;n<r.length;++n)switch((t=r[n]).t){case 0:u.push(t.u);break;case 1:for(e=t.i;e<=t.o;++e)u.push(""+e)}return u}function f(r,n){var t,e;if(r.t!==n.t)return null;switch(r.t){case 0:return r.u===n.u?r:null;case 1:return(t=Math.max(r.i,n.i))>(e=Math.min(r.o,n.o))?null:{t:1,i:t,o:e}}}function o(r,n){var t,e,u,i,o=[];for(t=0;t<r.length;++t)for(e=r[t],u=0;u<n.length;++u)null!==(i=f(e,n[u]))&&o.push(i);return o}function c(r){var n,t,e=[],u=r.split(",");for(n=0;n<u.length;++n)(t=u[n])&&v(e,a(t));return e}function a(t){if(r.test(t))return{t:1,i:+t,o:+t};var e=n.exec(t);return e?{t:1,i:+e[1],o:+e[2]}:{t:0,u:t}}function s(r){return r.map(_).join()}function _(r){switch(r.t){case 0:return r.u;case 1:return r.i===r.o?""+r.i:r.i+"-"+r.o}}function x(r,n){var t,e,u,i,f,o;r:for(t=0;t<n.length;++t)switch((e=n[t]).t){case 0:for(u=0;u<r.length;++u)if(0===(i=r[u]).t&&i.u===e.u)continue r;return!1;case 1:for(f=0;f<r.length;++f)if(1===(o=r[f]).t&&o.i<=e.i&&o.o>=e.o)continue r;return!1}return!0}function h(r,n){var t;if(r.t!==n.t)return!1;switch(r.t){case 0:return r.u===n.u;case 1:return(t=r.i<=n.o+1&&r.o>=n.i||n.i<=r.o+1&&n.o>=r.i)&&(r.i=Math.min(r.i,n.i),r.o=Math.max(r.o,n.o)),t}}function v(r,n){for(var e,u,i=0,f=r.length;i<f;){if(!(u=t(n,r[e=i+f>>>1])))return;u<0?f=e:i=e+1}m(r,n,i)||m(r,n,i+1)||r.splice(i,0,n)}function m(r,n,t){if(t&&t<=r.length&&h(r[t-1],n)){for(;t<r.length&&h(r[t-1],r[t]);)r.splice(t,1);return!0}return!1}exports.difference=function(r,n){return s(e(c(r),c(n)))},exports.equal=function(r,n){return u(c(r),c(n))},exports.expand=function(r){return i(c(r))},exports.intersection=function(r,n){return s(o(c(r),c(n)))},exports.normalize=function(r){return s(c(r))},exports.parse=c,exports.subset=function(r,n){return x(c(r),c(n))},exports.union=function(r,n){return s(c(r+","+n))}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
const n=/^\s*(?:0|[1-9]\d*)\s*$/,t=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function i(n,t){if(n.kind!==t.kind)return n.kind-t.kind;switch(n.kind){case 0:return n.text>=t.text?n.text>t.text?1:0:-1;case 1:return n.min-t.min||n.max-t.max}}function e(n,t){return x(function(n,t){const i=[];n:for(let e=0;e<n.length;++e){const r=n[e];switch(r.kind){case 0:for(let n=0;n<t.length;++n){const i=t[n];if(0===i.kind&&i.text===r.text)continue n}i.push(r);break;case 1:for(let i=0;i<t.length;++i){const m=t[i];if(1===m.kind){if(r.min>=m.min&&r.max<=m.max)continue n;if(r.min<=m.min&&r.max>=m.max){r.max>m.max&&n.splice(e+1,0,{kind:1,min:m.max+1,max:r.max}),r.min<m.min&&n.splice(e+1,0,{kind:1,min:r.min,max:m.min-1});continue n}r.min>=m.min&&r.min<=m.max?r.min=m.max+1:r.max>=m.min&&r.max<=m.max&&(r.max=m.min-1)}}i.push(r)}}return i}(o(n),o(t)))}function r(n,t){return function(n,t){if(n.length!==t.length)return!1;for(let e=0;e<n.length;++e)if(0!==i(n[e],t[e]))return!1;return!0}(o(n),o(t))}function m(n){return function(n){const t=[];for(let i=0;i<n.length;++i){const e=n[i];switch(e.kind){case 0:t.push(e.text);break;case 1:for(let n=e.min;n<=e.max;++n)t.push(`${n}`)}}return t}(o(n))}function u(n,t){return x(function(n,t){const i=[];for(let e=0;e<n.length;++e){const r=n[e];for(let n=0;n<t.length;++n){const e=c(r,t[n]);null!==e&&i.push(e)}}return i}(o(n),o(t)))}function c(n,t){if(n.kind!==t.kind)return null;switch(n.kind){case 0:return n.text===t.text?n:null;case 1:{const i=Math.max(n.min,t.min),e=Math.min(n.max,t.max);return i>e?null:{kind:1,min:i,max:e}}}}function a(n){return x(o(n))}function o(n){return n.split(",").filter(Boolean).map(s).sort(i).reduce(d,[])}function s(i){if(n.test(i))return{kind:1,min:+i,max:+i};const e=t.exec(i);return e?{kind:1,min:+e[1],max:+e[2]}:{kind:0,text:i}}function x(n){return n.map(f).join()}function f(n){switch(n.kind){case 0:return n.text;case 1:return n.min===n.max?`${n.min}`:`${n.min}-${n.max}`}}function l(n,t){return function(n,t){n:for(let i=0;i<t.length;++i){const e=t[i];switch(e.kind){case 0:for(let t=0;t<n.length;++t){const i=n[t];if(0===i.kind&&i.text===e.text)continue n}return!1;case 1:for(let t=0;t<n.length;++t){const i=n[t];if(1===i.kind&&e.min>=i.min&&e.max<=i.max)continue n}return!1}}return!0}(o(n),o(t))}function h(n,t){return x(o(n+","+t))}function d(n,t){return 0!==n.length&&function(n,t){if(n.kind!==t.kind)return!1;switch(n.kind){case 0:return n.text===t.text;case 1:{const i=n.min<=t.max+1&&n.max>=t.min||t.min<=n.max+1&&t.max>=n.min;return i&&(n.min=Math.min(n.min,t.min),n.max=Math.max(n.max,t.max)),i}}}(n[n.length-1],t)||n.push(t),n}export{e as difference,r as equal,m as expand,u as intersection,a as normalize,l as subset,h as union}; | ||
const n=/^\s*(?:0|[1-9]\d*)\s*$/,t=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function r(n,t){if(n.t!==t.t)return n.t-t.t;switch(n.t){case 0:return n.u>=t.u?n.u>t.u?1:0:-1;case 1:return n.o-t.o||n.i-t.i}}function e(n,t){return d(c(h(n),h(t)))}function c(n,t){const r=[];n:for(let e=0;e<n.length;++e){const c=n[e];switch(c.t){case 0:for(let n=0;n<t.length;++n){const r=t[n];if(0===r.t&&r.u===c.u)continue n}break;case 1:for(let r=0;r<t.length;++r){const u=t[r];if(1===u.t){if(c.o>=u.o&&c.i<=u.i)continue n;if(c.o<=u.o&&c.i>=u.i){c.i>u.i&&n.splice(e+1,0,{t:1,o:u.i+1,i:c.i}),c.o<u.o&&n.splice(e+1,0,{t:1,o:c.o,i:u.o-1});continue n}c.o>=u.o&&c.o<=u.i?c.o=u.i+1:c.i>=u.o&&c.i<=u.i&&(c.i=u.o-1)}}}r.push(c)}return r}function u(n,t){return o(h(n),h(t))}function o(n,t){if(n.length!==t.length)return!1;for(let e=0;e<n.length;++e)if(r(n[e],t[e]))return!1;return!0}function i(n){return f(h(n))}function f(n){const t=[];for(let r=0;r<n.length;++r){const e=n[r];switch(e.t){case 0:t.push(e.u);break;case 1:for(let n=e.o;n<=e.i;++n)t.push(`${n}`)}}return t}function s(n,t){return d(l(h(n),h(t)))}function a(n,t){if(n.t!==t.t)return null;switch(n.t){case 0:return n.u===t.u?n:null;case 1:{const r=Math.max(n.o,t.o),e=Math.min(n.i,t.i);return r>e?null:{t:1,o:r,i:e}}}}function l(n,t){const r=[];for(let e=0;e<n.length;++e){const c=n[e];for(let n=0;n<t.length;++n){const e=a(c,t[n]);null!==e&&r.push(e)}}return r}function _(n){return d(h(n))}function h(n){const t=[],r=n.split(",");for(let n=0;n<r.length;++n){const e=r[n];e&&b(t,m(e))}return t}function m(r){if(n.test(r))return{t:1,o:+r,i:+r};const e=t.exec(r);return e?{t:1,o:+e[1],i:+e[2]}:{t:0,u:r}}function d(n){return n.map(k).join()}function k(n){switch(n.t){case 0:return n.u;case 1:return n.o===n.i?`${n.o}`:`${n.o}-${n.i}`}}function $(n,t){return w(h(n),h(t))}function w(n,t){n:for(let r=0;r<t.length;++r){const e=t[r];switch(e.t){case 0:for(let t=0;t<n.length;++t){const r=n[t];if(0===r.t&&r.u===e.u)continue n}return!1;case 1:for(let t=0;t<n.length;++t){const r=n[t];if(1===r.t&&r.o<=e.o&&r.i>=e.i)continue n}return!1}}return!0}function x(n,t){return d(h(`${n},${t}`))}function M(n,t){if(n.t!==t.t)return!1;switch(n.t){case 0:return n.u===t.u;case 1:{const r=n.o<=t.i+1&&n.i>=t.o||t.o<=n.i+1&&t.i>=n.o;return r&&(n.o=Math.min(n.o,t.o),n.i=Math.max(n.i,t.i)),r}}}function b(n,t){let e=0,c=n.length;for(;e<c;){const u=e+c>>>1,o=r(t,n[u]);if(!o)return;o<0?c=u:e=u+1}p(n,t,e)||p(n,t,e+1)||n.splice(e,0,t)}function p(n,t,r){if(r&&r<=n.length&&M(n[r-1],t)){for(;r<n.length&&M(n[r-1],n[r]);)n.splice(r,1);return!0}return!1}export{e as difference,u as equal,i as expand,s as intersection,_ as normalize,h as parse,$ as subset,x as union}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var n=/^\s*(?:0|[1-9]\d*)\s*$/,t=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function i(n,t){if(n.kind!==t.kind)return n.kind-t.kind;switch(n.kind){case 0:return n.text>=t.text?n.text>t.text?1:0:-1;case 1:return n.min-t.min||n.max-t.max}}function r(n,t){return o(function(n,t){var i=[];n:for(var r=0;r<n.length;++r){var e=n[r];switch(e.kind){case 0:for(var a=0;a<t.length;++a){var m=t[a];if(0===m.kind&&m.text===e.text)continue n}i.push(e);break;case 1:for(var u=0;u<t.length;++u){var x=t[u];if(1===x.kind){if(e.min>=x.min&&e.max<=x.max)continue n;if(e.min<=x.min&&e.max>=x.max){e.max>x.max&&n.splice(r+1,0,{kind:1,min:x.max+1,max:e.max}),e.min<x.min&&n.splice(r+1,0,{kind:1,min:e.min,max:x.min-1});continue n}e.min>=x.min&&e.min<=x.max?e.min=x.max+1:e.max>=x.min&&e.max<=x.max&&(e.max=x.min-1)}}i.push(e)}}return i}(c(n),c(t)))}function e(n,t){return function(n,t){if(n.length!==t.length)return!1;for(var r=0;r<n.length;++r)if(0!==i(n[r],t[r]))return!1;return!0}(c(n),c(t))}function a(n){return function(n){for(var t=[],i=0;i<n.length;++i){var r=n[i];switch(r.kind){case 0:t.push(r.text);break;case 1:for(var e=r.min;e<=r.max;++e)t.push(""+e)}}return t}(c(n))}function m(n,t){return o(function(n,t){for(var i=[],r=0;r<n.length;++r)for(var e=n[r],a=0;a<t.length;++a){var m=u(e,t[a]);null!==m&&i.push(m)}return i}(c(n),c(t)))}function u(n,t){if(n.kind!==t.kind)return null;switch(n.kind){case 0:return n.text===t.text?n:null;case 1:var i=Math.max(n.min,t.min),r=Math.min(n.max,t.max);return i>r?null:{kind:1,min:i,max:r}}}function x(n){return o(c(n))}function c(n){return n.split(",").filter(Boolean).map(f).sort(i).reduce(k,[])}function f(i){if(n.test(i))return{kind:1,min:+i,max:+i};var r=t.exec(i);return r?{kind:1,min:+r[1],max:+r[2]}:{kind:0,text:i}}function o(n){return n.map(s).join()}function s(n){switch(n.kind){case 0:return n.text;case 1:return n.min===n.max?""+n.min:n.min+"-"+n.max}}function h(n,t){return function(n,t){n:for(var i=0;i<t.length;++i){var r=t[i];switch(r.kind){case 0:for(var e=0;e<n.length;++e){var a=n[e];if(0===a.kind&&a.text===r.text)continue n}return!1;case 1:for(var m=0;m<n.length;++m){var u=n[m];if(1===u.kind&&r.min>=u.min&&r.max<=u.max)continue n}return!1}}return!0}(c(n),c(t))}function d(n,t){return o(c(n+","+t))}function k(n,t){return 0!==n.length&&function(n,t){if(n.kind!==t.kind)return!1;switch(n.kind){case 0:return n.text===t.text;case 1:var i=n.min<=t.max+1&&n.max>=t.min||t.min<=n.max+1&&t.max>=n.min;return i&&(n.min=Math.min(n.min,t.min),n.max=Math.max(n.max,t.max)),i}}(n[n.length-1],t)||n.push(t),n}export{r as difference,e as equal,a as expand,m as intersection,x as normalize,h as subset,d as union}; | ||
var n=/^\s*(?:0|[1-9]\d*)\s*$/,r=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function t(n,r){if(n.t!==r.t)return n.t-r.t;switch(n.t){case 0:return n.u>=r.u?n.u>r.u?1:0:-1;case 1:return n.i-r.i||n.o-r.o}}function u(n,r){return d(i(v(n),v(r)))}function i(n,r){var t,u,i,e,f,c,o=[];n:for(t=0;t<n.length;++t){switch((u=n[t]).t){case 0:for(i=0;i<r.length;++i)if(0===(e=r[i]).t&&e.u===u.u)continue n;break;case 1:for(f=0;f<r.length;++f)if(1===(c=r[f]).t){if(u.i>=c.i&&u.o<=c.o)continue n;if(u.i<=c.i&&u.o>=c.o){u.o>c.o&&n.splice(t+1,0,{t:1,i:c.o+1,o:u.o}),u.i<c.i&&n.splice(t+1,0,{t:1,i:u.i,o:c.i-1});continue n}u.i>=c.i&&u.i<=c.o?u.i=c.o+1:u.o>=c.i&&u.o<=c.o&&(u.o=c.i-1)}}o.push(u)}return o}function e(n,r){return f(v(n),v(r))}function f(n,r){if(n.length!==r.length)return!1;for(var u=0;u<n.length;++u)if(t(n[u],r[u]))return!1;return!0}function c(n){return o(v(n))}function o(n){var r,t,u,i=[];for(r=0;r<n.length;++r)switch((t=n[r]).t){case 0:i.push(t.u);break;case 1:for(u=t.i;u<=t.o;++u)i.push(""+u)}return i}function a(n,r){return d(_(v(n),v(r)))}function s(n,r){var t,u;if(n.t!==r.t)return null;switch(n.t){case 0:return n.u===r.u?n:null;case 1:return(t=Math.max(n.i,r.i))>(u=Math.min(n.o,r.o))?null:{t:1,i:t,o:u}}}function _(n,r){var t,u,i,e,f=[];for(t=0;t<n.length;++t)for(u=n[t],i=0;i<r.length;++i)null!==(e=s(u,r[i]))&&f.push(e);return f}function h(n){return d(v(n))}function v(n){var r,t,u=[],i=n.split(",");for(r=0;r<i.length;++r)(t=i[r])&&b(u,m(t));return u}function m(t){if(n.test(t))return{t:1,i:+t,o:+t};var u=r.exec(t);return u?{t:1,i:+u[1],o:+u[2]}:{t:0,u:t}}function d(n){return n.map(k).join()}function k(n){switch(n.t){case 0:return n.u;case 1:return n.i===n.o?""+n.i:n.i+"-"+n.o}}function l(n,r){return w(v(n),v(r))}function w(n,r){var t,u,i,e,f,c;n:for(t=0;t<r.length;++t)switch((u=r[t]).t){case 0:for(i=0;i<n.length;++i)if(0===(e=n[i]).t&&e.u===u.u)continue n;return!1;case 1:for(f=0;f<n.length;++f)if(1===(c=n[f]).t&&c.i<=u.i&&c.o>=u.o)continue n;return!1}return!0}function x(n,r){return d(v(n+","+r))}function M(n,r){var t;if(n.t!==r.t)return!1;switch(n.t){case 0:return n.u===r.u;case 1:return(t=n.i<=r.o+1&&n.o>=r.i||r.i<=n.o+1&&r.o>=n.i)&&(n.i=Math.min(n.i,r.i),n.o=Math.max(n.o,r.o)),t}}function b(n,r){for(var u,i,e=0,f=n.length;e<f;){if(!(i=t(r,n[u=e+f>>>1])))return;i<0?f=u:e=u+1}$(n,r,e)||$(n,r,e+1)||n.splice(e,0,r)}function $(n,r,t){if(t&&t<=n.length&&M(n[t-1],r)){for(;t<n.length&&M(n[t-1],n[t]);)n.splice(t,1);return!0}return!1}export{u as difference,e as equal,c as expand,a as intersection,h as normalize,v as parse,l as subset,x as union}; | ||
//# sourceMappingURL=index.module.js.map |
237
index.ts
@@ -1,27 +0,35 @@ | ||
const KindLiteral = 0; | ||
const KindRange = 1; | ||
const enum Kind { | ||
Literal = 0, | ||
Range = 1, | ||
} | ||
type Repr = ReprLiteral | ReprRange; | ||
type ReprLiteral = { kind: typeof KindLiteral; text: string }; | ||
type ReprRange = { kind: typeof KindRange; min: number; max: number }; | ||
type IRepr = IReprLiteral | IReprRange; | ||
type IReprs = readonly IRepr[]; | ||
type IReprLiteral = Readonly<MReprLiteral>; | ||
type IReprRange = Readonly<MReprRange>; | ||
type MRepr = MReprLiteral | MReprRange; | ||
type MReprs = MRepr[]; | ||
type MReprLiteral = { _kind: Kind.Literal; _text: string }; | ||
type MReprRange = { _kind: Kind.Range; _min: number; _max: number }; | ||
const numberPattern = /^\s*(?:0|[1-9]\d*)\s*$/; | ||
const rangePattern = /^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/; | ||
function compare(reprA: Repr, reprB: Repr): number { | ||
if (reprA.kind !== reprB.kind) { | ||
return reprA.kind - reprB.kind; | ||
function compare(reprA: IRepr, reprB: IRepr): number { | ||
if (reprA._kind !== reprB._kind) { | ||
return reprA._kind - reprB._kind; | ||
} | ||
switch (reprA.kind) { | ||
case KindLiteral: | ||
return reprA.text >= (reprB as ReprLiteral).text | ||
? reprA.text > (reprB as ReprLiteral).text | ||
switch (reprA._kind) { | ||
case Kind.Literal: | ||
return reprA._text >= (reprB as IReprLiteral)._text | ||
? reprA._text > (reprB as IReprLiteral)._text | ||
? 1 | ||
: 0 | ||
: -1; | ||
case KindRange: | ||
case Kind.Range: | ||
return ( | ||
reprA.min - (reprB as ReprRange).min || | ||
reprA.max - (reprB as ReprRange).max | ||
reprA._min - (reprB as IReprRange)._min || | ||
reprA._max - (reprB as IReprRange)._max | ||
); | ||
@@ -39,11 +47,11 @@ } | ||
// eslint-disable-next-line complexity | ||
function differenceReprs(reprsA: Repr[], reprsB: Repr[]): Repr[] { | ||
const reprs: Repr[] = []; | ||
function differenceReprs(reprsA: MReprs, reprsB: IReprs): MReprs { | ||
const reprs: MReprs = []; | ||
loop: for (let indexA = 0; indexA < reprsA.length; ++indexA) { | ||
const reprA = reprsA[indexA]; | ||
switch (reprA.kind) { | ||
case KindLiteral: | ||
switch (reprA._kind) { | ||
case Kind.Literal: | ||
for (let indexB = 0; indexB < reprsB.length; ++indexB) { | ||
const reprB = reprsB[indexB]; | ||
if (reprB.kind === KindLiteral && reprB.text === reprA.text) { | ||
if (reprB._kind === Kind.Literal && reprB._text === reprA._text) { | ||
continue loop; | ||
@@ -53,26 +61,25 @@ } | ||
reprs.push(reprA); | ||
break; | ||
case KindRange: | ||
case Kind.Range: | ||
for (let indexB = 0; indexB < reprsB.length; ++indexB) { | ||
const reprB = reprsB[indexB]; | ||
if (reprB.kind === KindRange) { | ||
if (reprA.min >= reprB.min && reprA.max <= reprB.max) { | ||
if (reprB._kind === Kind.Range) { | ||
if (reprA._min >= reprB._min && reprA._max <= reprB._max) { | ||
continue loop; | ||
} | ||
if (reprA.min <= reprB.min && reprA.max >= reprB.max) { | ||
if (reprA.max > reprB.max) { | ||
if (reprA._min <= reprB._min && reprA._max >= reprB._max) { | ||
if (reprA._max > reprB._max) { | ||
reprsA.splice(indexA + 1, 0, { | ||
kind: KindRange, | ||
min: reprB.max + 1, | ||
max: reprA.max, | ||
_kind: Kind.Range, | ||
_min: reprB._max + 1, | ||
_max: reprA._max, | ||
}); | ||
} | ||
if (reprA.min < reprB.min) { | ||
if (reprA._min < reprB._min) { | ||
reprsA.splice(indexA + 1, 0, { | ||
kind: KindRange, | ||
min: reprA.min, | ||
max: reprB.min - 1, | ||
_kind: Kind.Range, | ||
_min: reprA._min, | ||
_max: reprB._min - 1, | ||
}); | ||
@@ -84,6 +91,6 @@ } | ||
if (reprA.min >= reprB.min && reprA.min <= reprB.max) { | ||
reprA.min = reprB.max + 1; | ||
} else if (reprA.max >= reprB.min && reprA.max <= reprB.max) { | ||
reprA.max = reprB.min - 1; | ||
if (reprA._min >= reprB._min && reprA._min <= reprB._max) { | ||
reprA._min = reprB._max + 1; | ||
} else if (reprA._max >= reprB._min && reprA._max <= reprB._max) { | ||
reprA._max = reprB._min - 1; | ||
} | ||
@@ -93,5 +100,6 @@ } | ||
reprs.push(reprA); | ||
break; | ||
} | ||
reprs.push(reprA); | ||
} | ||
@@ -108,3 +116,3 @@ | ||
function equalReprs(reprsA: Repr[], reprsB: Repr[]): boolean { | ||
function equalReprs(reprsA: IReprs, reprsB: IReprs): boolean { | ||
if (reprsA.length !== reprsB.length) { | ||
@@ -115,3 +123,3 @@ return false; | ||
for (let index = 0; index < reprsA.length; ++index) { | ||
if (compare(reprsA[index], reprsB[index]) !== 0) { | ||
if (compare(reprsA[index], reprsB[index])) { | ||
return false; | ||
@@ -129,12 +137,12 @@ } | ||
function expandReprs(reprs: Repr[]): string[] { | ||
function expandReprs(reprs: IReprs): string[] { | ||
const texts: string[] = []; | ||
for (let index = 0; index < reprs.length; ++index) { | ||
const repr = reprs[index]; | ||
switch (repr.kind) { | ||
case KindLiteral: | ||
texts.push(repr.text); | ||
switch (repr._kind) { | ||
case Kind.Literal: | ||
texts.push(repr._text); | ||
break; | ||
case KindRange: { | ||
for (let index = repr.min; index <= repr.max; ++index) { | ||
case Kind.Range: { | ||
for (let index = repr._min; index <= repr._max; ++index) { | ||
texts.push(`${index}`); | ||
@@ -157,14 +165,14 @@ } | ||
function intersectionRepr(reprA: Repr, reprB: Repr): Repr | null { | ||
if (reprA.kind !== reprB.kind) { | ||
function intersectionRepr(reprA: MRepr, reprB: IRepr): MRepr | null { | ||
if (reprA._kind !== reprB._kind) { | ||
return null; | ||
} | ||
switch (reprA.kind) { | ||
case KindLiteral: | ||
return reprA.text === (reprB as ReprLiteral).text ? reprA : null; | ||
case KindRange: { | ||
const min = Math.max(reprA.min, (reprB as ReprRange).min); | ||
const max = Math.min(reprA.max, (reprB as ReprRange).max); | ||
return min > max ? null : { kind: KindRange, min, max }; | ||
switch (reprA._kind) { | ||
case Kind.Literal: | ||
return reprA._text === (reprB as IReprLiteral)._text ? reprA : null; | ||
case Kind.Range: { | ||
const min = Math.max(reprA._min, (reprB as IReprRange)._min); | ||
const max = Math.min(reprA._max, (reprB as IReprRange)._max); | ||
return min > max ? null : { _kind: Kind.Range, _min: min, _max: max }; | ||
} | ||
@@ -174,4 +182,4 @@ } | ||
function intersectionReprs(reprsA: Repr[], reprsB: Repr[]): Repr[] { | ||
const reprs: Repr[] = []; | ||
function intersectionReprs(reprsA: MReprs, reprsB: IReprs): MReprs { | ||
const reprs: MReprs = []; | ||
for (let indexA = 0; indexA < reprsA.length; ++indexA) { | ||
@@ -196,9 +204,19 @@ const reprA = reprsA[indexA]; | ||
function parse(text: string): Repr[] { | ||
return unionReprs(text.split(',').filter(Boolean).map(parseOne)); | ||
export function parse(text: string): MReprs { | ||
const reprs: MReprs = []; | ||
const chunks = text.split(','); | ||
for (let index = 0; index < chunks.length; ++index) { | ||
const chunk = chunks[index]; | ||
if (chunk) { | ||
const repr = parseOne(chunk); | ||
unionReprs(reprs, repr); | ||
} | ||
} | ||
return reprs; | ||
} | ||
function parseOne(text: string): Repr { | ||
function parseOne(text: string): MRepr { | ||
if (numberPattern.test(text)) { | ||
return { kind: KindRange, min: +text, max: +text }; | ||
return { _kind: Kind.Range, _min: +text, _max: +text }; | ||
} | ||
@@ -208,18 +226,20 @@ | ||
if (rangeMatch) { | ||
return { kind: KindRange, min: +rangeMatch[1], max: +rangeMatch[2] }; | ||
return { _kind: Kind.Range, _min: +rangeMatch[1], _max: +rangeMatch[2] }; | ||
} | ||
return { kind: KindLiteral, text }; | ||
return { _kind: Kind.Literal, _text: text }; | ||
} | ||
function serialize(reprs: Repr[]): string { | ||
function serialize(reprs: IReprs): string { | ||
return reprs.map(serializeOne).join(); | ||
} | ||
function serializeOne(repr: Repr): string { | ||
switch (repr.kind) { | ||
case KindLiteral: | ||
return repr.text; | ||
case KindRange: | ||
return repr.min === repr.max ? `${repr.min}` : `${repr.min}-${repr.max}`; | ||
function serializeOne(repr: IRepr): string { | ||
switch (repr._kind) { | ||
case Kind.Literal: | ||
return repr._text; | ||
case Kind.Range: | ||
return repr._min === repr._max | ||
? `${repr._min}` | ||
: `${repr._min}-${repr._max}`; | ||
} | ||
@@ -234,10 +254,10 @@ } | ||
function subsetReprs(reprsA: Repr[], reprsB: Repr[]): boolean { | ||
function subsetReprs(reprsA: IReprs, reprsB: IReprs): boolean { | ||
loop: for (let indexB = 0; indexB < reprsB.length; ++indexB) { | ||
const reprB = reprsB[indexB]; | ||
switch (reprB.kind) { | ||
case KindLiteral: | ||
switch (reprB._kind) { | ||
case Kind.Literal: | ||
for (let indexA = 0; indexA < reprsA.length; ++indexA) { | ||
const reprA = reprsA[indexA]; | ||
if (reprA.kind === KindLiteral && reprA.text === reprB.text) { | ||
if (reprA._kind === Kind.Literal && reprA._text === reprB._text) { | ||
continue loop; | ||
@@ -248,9 +268,9 @@ } | ||
return false; | ||
case KindRange: | ||
case Kind.Range: | ||
for (let indexA = 0; indexA < reprsA.length; ++indexA) { | ||
const reprA = reprsA[indexA]; | ||
if ( | ||
reprA.kind === KindRange && | ||
reprB.min >= reprA.min && | ||
reprB.max <= reprA.max | ||
reprA._kind === Kind.Range && | ||
reprA._min <= reprB._min && | ||
reprA._max >= reprB._max | ||
) { | ||
@@ -269,26 +289,26 @@ continue loop; | ||
export function union(textA: string, textB: string): string { | ||
const reprs = parse(textA + ',' + textB); | ||
const reprs = parse(`${textA},${textB}`); | ||
return serialize(reprs); | ||
} | ||
function unionRepr(reprA: Repr, reprB: Repr): boolean { | ||
if (reprA.kind !== reprB.kind) { | ||
function unionRepr(reprA: MRepr, reprB: IRepr): boolean { | ||
if (reprA._kind !== reprB._kind) { | ||
return false; | ||
} | ||
switch (reprA.kind) { | ||
case KindLiteral: { | ||
const same = reprA.text === (reprB as ReprLiteral).text; | ||
switch (reprA._kind) { | ||
case Kind.Literal: { | ||
const same = reprA._text === (reprB as IReprLiteral)._text; | ||
return same; | ||
} | ||
case KindRange: { | ||
case Kind.Range: { | ||
const unionable = | ||
(reprA.min <= (reprB as ReprRange).max + 1 && | ||
reprA.max >= (reprB as ReprRange).min) || | ||
((reprB as ReprRange).min <= reprA.max + 1 && | ||
(reprB as ReprRange).max >= reprA.min); | ||
(reprA._min <= (reprB as IReprRange)._max + 1 && | ||
reprA._max >= (reprB as IReprRange)._min) || | ||
((reprB as IReprRange)._min <= reprA._max + 1 && | ||
(reprB as IReprRange)._max >= reprA._min); | ||
if (unionable) { | ||
reprA.min = Math.min(reprA.min, (reprB as ReprRange).min); | ||
reprA.max = Math.max(reprA.max, (reprB as ReprRange).max); | ||
reprA._min = Math.min(reprA._min, (reprB as IReprRange)._min); | ||
reprA._max = Math.max(reprA._max, (reprB as IReprRange)._max); | ||
} | ||
@@ -301,12 +321,35 @@ | ||
function unionReprReducer(reprs: Repr[], repr: Repr): Repr[] { | ||
if (reprs.length === 0 || !unionRepr(reprs[reprs.length - 1], repr)) { | ||
reprs.push(repr); | ||
function unionReprs(reprs: MReprs, repr: MRepr): void { | ||
let low = 0; | ||
let high = reprs.length; | ||
while (low < high) { | ||
// eslint-disable-next-line no-bitwise | ||
const middle = (low + high) >>> 1; | ||
const result = compare(repr, reprs[middle]); | ||
if (!result) { | ||
return; | ||
} | ||
if (result < 0) { | ||
high = middle; | ||
} else { | ||
low = middle + 1; | ||
} | ||
} | ||
return reprs; | ||
if (!unionReprsAt(reprs, repr, low) && !unionReprsAt(reprs, repr, low + 1)) { | ||
reprs.splice(low, 0, repr); | ||
} | ||
} | ||
function unionReprs(reprs: Repr[]): Repr[] { | ||
return reprs.sort(compare).reduce(unionReprReducer, []); | ||
function unionReprsAt(reprs: MReprs, repr: IRepr, index: number): boolean { | ||
if (index && index <= reprs.length && unionRepr(reprs[index - 1], repr)) { | ||
while (index < reprs.length && unionRepr(reprs[index - 1], reprs[index])) { | ||
reprs.splice(index, 1); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} |
{ | ||
"name": "ranges-set", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Set operations on human-friendly ranges.", | ||
@@ -8,6 +8,6 @@ "license": "MIT", | ||
"author": "Radosław Miernik <radekmie@gmail.com>", | ||
"main": "index.js", | ||
"exports": "index.modern.js", | ||
"module": "index.module.js", | ||
"source": "index.ts", | ||
"main": "./index.js", | ||
"exports": "./index.modern.js", | ||
"module": "./index.module.js", | ||
"source": "./index.ts", | ||
"sideEffects": false, | ||
@@ -35,5 +35,5 @@ "files": [ | ||
"eslint-config-vazco": "6.1.0", | ||
"microbundle": "0.13.0", | ||
"microbundle": "0.13.3", | ||
"nyc": "15.1.0", | ||
"ts-node": "9.1.1" | ||
"ts-node": "10.2.1" | ||
}, | ||
@@ -59,3 +59,14 @@ "ava": { | ||
} | ||
}, | ||
"minify": { | ||
"compress": { | ||
"hoist_vars": true, | ||
"reduce_funcs": false | ||
}, | ||
"mangle": { | ||
"properties": { | ||
"regex": "^_" | ||
} | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64845
591