ranges-set
Advanced tools
Comparing version 1.0.2 to 1.1.0
export declare function difference(textA: string, textB: string): string; | ||
export declare function equal(textA: string, textB: string): boolean; | ||
export declare function expand(text: string): string[]; | ||
@@ -3,0 +4,0 @@ export declare function intersection(textA: string, textB: string): string; |
@@ -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 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 r(n,t){if(n.kind!==t.kind)return n.kind-t.kind;switch(n.kind){case 0:return n.text<t.text?-1:1;case 1:return n.min-t.min||n.max-t.max}}function e(n){return n.split(",").filter(Boolean).map(a)}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}function c(n){return n.sort(r).reduce(x,[])}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}(c(e(n)),c(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}(c(e(n)))},exports.intersection=function(n,t){return m(function(n,t){for(var r=[],e=0;e<n.length;++e)for(var a=n[e],m=0;m<t.length;++m){var u=i(a,t[m]);null!==u&&r.push(u)}return r}(c(e(n)),c(e(t))))},exports.normalize=function(n){return m(c(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}(c(e(n)),c(e(t)))},exports.union=function(n,t){var i=c(e(n)),r=c(e(t));return m(c(i.concat(r)))}; | ||
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))}; | ||
//# 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){return s(function(n,t){const i=[];n:for(let e=0;e<n.length;++e){const m=n[e];switch(m.kind){case 0:for(let n=0;n<t.length;++n){const i=t[n];if(0===i.kind&&i.text===m.text)continue n}i.push(m);break;case 1:for(let i=0;i<t.length;++i){const r=t[i];if(1===r.kind){if(m.min>=r.min&&m.max<=r.max)continue n;if(m.min<=r.min&&m.max>=r.max){m.max>r.max&&n.splice(e+1,0,{kind:1,min:r.max+1,max:m.max}),m.min<r.min&&n.splice(e+1,0,{kind:1,min:m.min,max:r.min-1});continue n}m.min>=r.min&&m.min<=r.max?m.min=r.max+1:m.max>=r.min&&m.max<=r.max&&(m.max=r.min-1)}}i.push(m)}}return i}(h(a(n)),h(a(t))))}function e(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}(h(a(n)))}function m(n,t){return s(function(n,t){const i=[];for(let e=0;e<n.length;++e){const m=n[e];for(let n=0;n<t.length;++n){const e=r(m,t[n]);null!==e&&i.push(e)}}return i}(h(a(n)),h(a(t))))}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:{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 c(n){return s(h(a(n)))}function u(n,t){if(n.kind!==t.kind)return n.kind-t.kind;switch(n.kind){case 0:return n.text<t.text?-1:1;case 1:return n.min-t.min||n.max-t.max}}function a(n){return n.split(",").filter(Boolean).map(o)}function o(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 s(n){return n.map(x).join()}function x(n){switch(n.kind){case 0:return n.text;case 1:return n.min===n.max?`${n.min}`:`${n.min}-${n.max}`}}function f(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}(h(a(n)),h(a(t)))}function l(n,t){const i=h(a(n)),e=h(a(t));return s(h(i.concat(e)))}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}function h(n){return n.sort(u).reduce(d,[])}export{i as difference,e as expand,m as intersection,c as normalize,f as subset,l as union}; | ||
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}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var n=/^\s*(?:0|[1-9]\d*)\s*$/,i=/^\s*(0|[1-9]\d*)\s*-\s*(0|[1-9]\d*)\s*$/;function t(n,i){return f(function(n,i){var t=[];n:for(var r=0;r<n.length;++r){var a=n[r];switch(a.kind){case 0:for(var e=0;e<i.length;++e){var m=i[e];if(0===m.kind&&m.text===a.text)continue n}t.push(a);break;case 1:for(var u=0;u<i.length;++u){var x=i[u];if(1===x.kind){if(a.min>=x.min&&a.max<=x.max)continue n;if(a.min<=x.min&&a.max>=x.max){a.max>x.max&&n.splice(r+1,0,{kind:1,min:x.max+1,max:a.max}),a.min<x.min&&n.splice(r+1,0,{kind:1,min:a.min,max:x.min-1});continue n}a.min>=x.min&&a.min<=x.max?a.min=x.max+1:a.max>=x.min&&a.max<=x.max&&(a.max=x.min-1)}}t.push(a)}}return t}(k(x(n)),k(x(i))))}function r(n){return function(n){for(var i=[],t=0;t<n.length;++t){var r=n[t];switch(r.kind){case 0:i.push(r.text);break;case 1:for(var a=r.min;a<=r.max;++a)i.push(""+a)}}return i}(k(x(n)))}function a(n,i){return f(function(n,i){for(var t=[],r=0;r<n.length;++r)for(var a=n[r],m=0;m<i.length;++m){var u=e(a,i[m]);null!==u&&t.push(u)}return t}(k(x(n)),k(x(i))))}function e(n,i){if(n.kind!==i.kind)return null;switch(n.kind){case 0:return n.text===i.text?n:null;case 1:var t=Math.max(n.min,i.min),r=Math.min(n.max,i.max);return t>r?null:{kind:1,min:t,max:r}}}function m(n){return f(k(x(n)))}function u(n,i){if(n.kind!==i.kind)return n.kind-i.kind;switch(n.kind){case 0:return n.text<i.text?-1:1;case 1:return n.min-i.min||n.max-i.max}}function x(n){return n.split(",").filter(Boolean).map(c)}function c(t){if(n.test(t))return{kind:1,min:+t,max:+t};var r=i.exec(t);return r?{kind:1,min:+r[1],max:+r[2]}:{kind:0,text:t}}function f(n){return n.map(o).join()}function o(n){switch(n.kind){case 0:return n.text;case 1:return n.min===n.max?""+n.min:n.min+"-"+n.max}}function s(n,i){return function(n,i){n:for(var t=0;t<i.length;++t){var r=i[t];switch(r.kind){case 0:for(var a=0;a<n.length;++a){var e=n[a];if(0===e.kind&&e.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}(k(x(n)),k(x(i)))}function d(n,i){var t=k(x(n)),r=k(x(i));return f(k(t.concat(r)))}function h(n,i){return 0!==n.length&&function(n,i){if(n.kind!==i.kind)return!1;switch(n.kind){case 0:return n.text===i.text;case 1:var t=n.min<=i.max+1&&n.max>=i.min||i.min<=n.max+1&&i.max>=n.min;return t&&(n.min=Math.min(n.min,i.min),n.max=Math.max(n.max,i.max)),t}}(n[n.length-1],i)||n.push(i),n}function k(n){return n.sort(u).reduce(h,[])}export{t as difference,r as expand,a as intersection,m as normalize,s as subset,d as union}; | ||
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}; | ||
//# sourceMappingURL=index.module.js.map |
80
index.ts
@@ -11,5 +11,25 @@ const KindLiteral = 0; | ||
function compare(reprA: Repr, reprB: Repr): 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 | ||
? 1 | ||
: 0 | ||
: -1; | ||
case KindRange: | ||
return ( | ||
reprA.min - (reprB as ReprRange).min || | ||
reprA.max - (reprB as ReprRange).max | ||
); | ||
} | ||
} | ||
export function difference(textA: string, textB: string): string { | ||
const reprsA = unionReprs(parse(textA)); | ||
const reprsB = unionReprs(parse(textB)); | ||
const reprsA = parse(textA); | ||
const reprsB = parse(textB); | ||
const reprs = differenceReprs(reprsA, reprsB); | ||
@@ -79,4 +99,24 @@ return serialize(reprs); | ||
export function equal(textA: string, textB: string): boolean { | ||
const reprsA = parse(textA); | ||
const reprsB = parse(textB); | ||
return equalReprs(reprsA, reprsB); | ||
} | ||
function equalReprs(reprsA: Repr[], reprsB: Repr[]): boolean { | ||
if (reprsA.length !== reprsB.length) { | ||
return false; | ||
} | ||
for (let index = 0; index < reprsA.length; ++index) { | ||
if (compare(reprsA[index], reprsB[index]) !== 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
export function expand(text: string): string[] { | ||
const reprs = unionReprs(parse(text)); | ||
const reprs = parse(text); | ||
return expandReprs(reprs); | ||
@@ -106,4 +146,4 @@ } | ||
export function intersection(textA: string, textB: string): string { | ||
const reprsA = unionReprs(parse(textA)); | ||
const reprsB = unionReprs(parse(textB)); | ||
const reprsA = parse(textA); | ||
const reprsB = parse(textB); | ||
const reprs = intersectionReprs(reprsA, reprsB); | ||
@@ -146,24 +186,8 @@ return serialize(reprs); | ||
export function normalize(text: string): string { | ||
const reprs = unionReprs(parse(text)); | ||
const reprs = parse(text); | ||
return serialize(reprs); | ||
} | ||
function order(reprA: Repr, reprB: Repr): number { | ||
if (reprA.kind !== reprB.kind) { | ||
return reprA.kind - reprB.kind; | ||
} | ||
switch (reprA.kind) { | ||
case KindLiteral: | ||
return reprA.text < (reprB as ReprLiteral).text ? -1 : 1; | ||
case KindRange: | ||
return ( | ||
reprA.min - (reprB as ReprRange).min || | ||
reprA.max - (reprB as ReprRange).max | ||
); | ||
} | ||
} | ||
function parse(text: string): Repr[] { | ||
return text.split(',').filter(Boolean).map(parseOne); | ||
return unionReprs(text.split(',').filter(Boolean).map(parseOne)); | ||
} | ||
@@ -198,4 +222,4 @@ | ||
export function subset(textA: string, textB: string): boolean { | ||
const reprsA = unionReprs(parse(textA)); | ||
const reprsB = unionReprs(parse(textB)); | ||
const reprsA = parse(textA); | ||
const reprsB = parse(textB); | ||
return subsetReprs(reprsA, reprsB); | ||
@@ -237,5 +261,3 @@ } | ||
export function union(textA: string, textB: string): string { | ||
const reprsA = unionReprs(parse(textA)); | ||
const reprsB = unionReprs(parse(textB)); | ||
const reprs = unionReprs(reprsA.concat(reprsB)); | ||
const reprs = parse(textA + ',' + textB); | ||
return serialize(reprs); | ||
@@ -280,3 +302,3 @@ } | ||
function unionReprs(reprs: Repr[]): Repr[] { | ||
return reprs.sort(order).reduce(unionReprReducer, []); | ||
return reprs.sort(compare).reduce(unionReprReducer, []); | ||
} |
{ | ||
"name": "ranges-set", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Set operations on human-friendly ranges.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -20,5 +20,6 @@ # ranges-set | ||
```ts | ||
import { difference, expand, intersection, normalize, subset, union } from 'ranges-set'; | ||
import { difference, equal, expand, intersection, normalize, subset, union } from 'ranges-set'; | ||
difference('1-4', '2-3'); // '1,4' | ||
equal('1-3', '1-2'); // false | ||
expand('1-3,5-7'); // ['1', '2', '3', '5', '6', '7'] | ||
@@ -35,2 +36,3 @@ intersection('1-10', '5-10'); // '5-10' | ||
function difference(textA: string, textB: string): string; | ||
function equal(textA: string, textB: string): boolean; | ||
function expand(text: string): string[]; | ||
@@ -37,0 +39,0 @@ function intersection(textA: string, textB: string): string; |
43
test.ts
import test from 'ava'; | ||
import { difference, expand, intersection, normalize, subset, union } from '.'; | ||
import { | ||
difference, | ||
equal, | ||
expand, | ||
intersection, | ||
normalize, | ||
subset, | ||
union, | ||
} from '.'; | ||
@@ -53,2 +61,35 @@ function test1<A, B>(fn: (a: A) => B, tests: [A, B][]) { | ||
test2(equal, [ | ||
['1', '1', true], | ||
['1', '1-2', false], | ||
['1', '2', false], | ||
['1', '2-100', false], | ||
['1', '3', false], | ||
['1,5', '1-5', false], | ||
['1-2', '1', false], | ||
['1-2', '1-2', true], | ||
['1-2', '2', false], | ||
['1-2,3,4-5', '1-5', true], | ||
['1-2,4-5', '1-5', false], | ||
['1-3', '2-4', false], | ||
['1-5', '1,5', false], | ||
['1-5', '1-2,4-5', false], | ||
['1-6', '1,3,6', false], | ||
['1-6', '1,4', false], | ||
['1-6', '1,6', false], | ||
['1-6', '2,4', false], | ||
['1-6', '2-5', false], | ||
['2', '1', false], | ||
['2-100', '1', false], | ||
['2-4', '1-3', false], | ||
['3', '1', false], | ||
['a', 'a', true], | ||
['a', 'b', false], | ||
['a,1', 'b,1', false], | ||
['a,1', 'b,2', false], | ||
['b', 'a', false], | ||
['b,1', 'a,1', false], | ||
['b,2', 'a,1', false], | ||
]); | ||
test1(expand, [ | ||
@@ -55,0 +96,0 @@ [',', []], |
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
59635
538
42