ts-pattern
Version:
The exhaustive Pattern Matching library for TypeScript.
3 lines (2 loc) • 11.9 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n||self).tsPattern={})}(this,function(n){function t(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=Array(t);e<t;e++)r[e]=n[e];return r}function e(n,e){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(r)return(r=r.call(n)).next.bind(r);if(Array.isArray(n)||(r=function(n,e){if(n){if("string"==typeof n)return t(n,e);var r={}.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(n,e):void 0}}(n))||e&&n&&"number"==typeof n.length){r&&(n=r);var u=0;return function(){return u>=n.length?{done:!0}:{done:!1,value:n[u++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(n){return r=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(n){return n.__proto__||Object.getPrototypeOf(n)},r(n)}function u(){try{var n=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(n){}return(u=function(){return!!n})()}function i(n,t){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(n,t){return n.__proto__=t,n},i(n,t)}function o(n){var t="function"==typeof Map?new Map:void 0;return o=function(n){if(null===n||!function(n){try{return-1!==Function.toString.call(n).indexOf("[native code]")}catch(t){return"function"==typeof n}}(n))return n;if("function"!=typeof n)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(n))return t.get(n);t.set(n,e)}function e(){return function(n,t,e){if(u())return Reflect.construct.apply(null,arguments);var r=[null];r.push.apply(r,t);var o=new(n.bind.apply(n,r));return e&&i(o,e.prototype),o}(n,arguments,r(this).constructor)}return e.prototype=Object.create(n.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),i(e,n)},o(n)}var c=Symbol.for("@ts-pattern/matcher"),f=Symbol.for("@ts-pattern/isVariadic"),a="@ts-pattern/anonymous-select-key",l=function(n){return Boolean(n&&"object"==typeof n)},s=function(n){return n&&!!n[c]},h=function n(t,r,u){if(s(t)){var i=t[c]().match(r),o=i.matched,a=i.selections;return o&&a&&Object.keys(a).forEach(function(n){return u(n,a[n])}),o}if(l(t)){if(!l(r))return!1;if(Array.isArray(t)){if(!Array.isArray(r))return!1;for(var h,v=[],p=[],y=[],g=e(t.keys());!(h=g()).done;){var d=t[h.value];s(d)&&d[f]?y.push(d):y.length?p.push(d):v.push(d)}if(y.length){if(y.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(r.length<v.length+p.length)return!1;var m=r.slice(0,v.length),b=0===p.length?[]:r.slice(-p.length),w=r.slice(v.length,0===p.length?Infinity:-p.length);return v.every(function(t,e){return n(t,m[e],u)})&&p.every(function(t,e){return n(t,b[e],u)})&&(0===y.length||n(y[0],w,u))}return t.length===r.length&&t.every(function(t,e){return n(t,r[e],u)})}return Reflect.ownKeys(t).every(function(e){var i,o=t[e];return(e in r||s(i=o)&&"optional"===i[c]().matcherType)&&n(o,r[e],u)})}return Object.is(r,t)},v=function n(t){var e,r,u;return l(t)?s(t)?null!=(e=null==(r=(u=t[c]()).getSelectionKeys)?void 0:r.call(u))?e:[]:Array.isArray(t)?p(t,n):p(Object.values(t),n):[]},p=function(n,t){return n.reduce(function(n,e){return n.concat(t(e))},[])};function y(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return h(t,n,function(){})}}if(2===n.length)return h(n[0],n[1],function(){});throw new Error("isMatching wasn't given the right number of arguments: expected 1 or 2, received "+n.length+".")}function g(n){return Object.assign(n,{optional:function(){return m(n)},and:function(t){return O(n,t)},or:function(t){return S(n,t)},select:function(t){return void 0===t?x(n):x(t,n)}})}function d(n){return Object.assign(function(n){var t;return Object.assign(n,((t={})[Symbol.iterator]=function(){var t,e=0,r=[{value:Object.assign(n,((t={})[f]=!0,t)),done:!1},{done:!0,value:void 0}];return{next:function(){var n;return null!=(n=r[e++])?n:r.at(-1)}}},t))}(n),{optional:function(){return d(m(n))},select:function(t){return d(void 0===t?x(n):x(t,n))}})}function m(n){var t;return g(((t={})[c]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(v(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:h(n,t,r),selections:e}},getSelectionKeys:function(){return v(n)},matcherType:"optional"}},t))}var b=function(n,t){for(var r,u=e(n);!(r=u()).done;)if(!t(r.value))return!1;return!0},w=function(n,t){for(var r,u=e(n.entries());!(r=u()).done;){var i=r.value;if(!t(i[1],i[0]))return!1}return!0};function O(){var n,t=[].slice.call(arguments);return g(((n={})[c]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return{matched:t.every(function(t){return h(t,n,r)}),selections:e}},getSelectionKeys:function(){return p(t,v)},matcherType:"and"}},n))}function S(){var n,t=[].slice.call(arguments);return g(((n={})[c]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return p(t,v).forEach(function(n){return r(n,void 0)}),{matched:t.some(function(t){return h(t,n,r)}),selections:e}},getSelectionKeys:function(){return p(t,v)},matcherType:"or"}},n))}function j(n){var t;return(t={})[c]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},t}function x(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,r=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return g(((n={})[c]=function(){return{match:function(n){var t,u=((t={})[null!=e?e:a]=n,t);return{matched:void 0===r||h(r,n,function(n,t){u[n]=t}),selections:u}},getSelectionKeys:function(){return[null!=e?e:a].concat(void 0===r?[]:v(r))}}},n))}function A(n){return"number"==typeof n}function E(n){return"string"==typeof n}function _(n){return"bigint"==typeof n}var P=g(j(function(n){return!0})),K=P,T=function n(t){return Object.assign(g(t),{startsWith:function(e){return n(O(t,(r=e,j(function(n){return E(n)&&n.startsWith(r)}))));var r},endsWith:function(e){return n(O(t,(r=e,j(function(n){return E(n)&&n.endsWith(r)}))));var r},minLength:function(e){return n(O(t,function(n){return j(function(t){return E(t)&&t.length>=n})}(e)))},length:function(e){return n(O(t,function(n){return j(function(t){return E(t)&&t.length===n})}(e)))},maxLength:function(e){return n(O(t,function(n){return j(function(t){return E(t)&&t.length<=n})}(e)))},includes:function(e){return n(O(t,(r=e,j(function(n){return E(n)&&n.includes(r)}))));var r},regex:function(e){return n(O(t,(r=e,j(function(n){return E(n)&&Boolean(n.match(r))}))));var r}})}(j(E)),B=function n(t){return Object.assign(g(t),{between:function(e,r){return n(O(t,function(n,t){return j(function(e){return A(e)&&n<=e&&t>=e})}(e,r)))},lt:function(e){return n(O(t,function(n){return j(function(t){return A(t)&&t<n})}(e)))},gt:function(e){return n(O(t,function(n){return j(function(t){return A(t)&&t>n})}(e)))},lte:function(e){return n(O(t,function(n){return j(function(t){return A(t)&&t<=n})}(e)))},gte:function(e){return n(O(t,function(n){return j(function(t){return A(t)&&t>=n})}(e)))},int:function(){return n(O(t,j(function(n){return A(n)&&Number.isInteger(n)})))},finite:function(){return n(O(t,j(function(n){return A(n)&&Number.isFinite(n)})))},positive:function(){return n(O(t,j(function(n){return A(n)&&n>0})))},negative:function(){return n(O(t,j(function(n){return A(n)&&n<0})))}})}(j(A)),M=function n(t){return Object.assign(g(t),{between:function(e,r){return n(O(t,function(n,t){return j(function(e){return _(e)&&n<=e&&t>=e})}(e,r)))},lt:function(e){return n(O(t,function(n){return j(function(t){return _(t)&&t<n})}(e)))},gt:function(e){return n(O(t,function(n){return j(function(t){return _(t)&&t>n})}(e)))},lte:function(e){return n(O(t,function(n){return j(function(t){return _(t)&&t<=n})}(e)))},gte:function(e){return n(O(t,function(n){return j(function(t){return _(t)&&t>=n})}(e)))},positive:function(){return n(O(t,j(function(n){return _(n)&&n>0})))},negative:function(){return n(O(t,j(function(n){return _(n)&&n<0})))}})}(j(_)),I=g(j(function(n){return"boolean"==typeof n})),N=g(j(function(n){return"symbol"==typeof n})),k=g(j(function(n){return null==n})),W=g(j(function(n){return null!=n})),R={__proto__:null,matcher:c,optional:m,array:function(){var n,t=[].slice.call(arguments);return d(((n={})[c]=function(){return{match:function(n){if(!Array.isArray(n))return{matched:!1};if(0===t.length)return{matched:!0};var e=t[0],r={};if(0===n.length)return v(e).forEach(function(n){r[n]=[]}),{matched:!0,selections:r};var u=function(n,t){r[n]=(r[n]||[]).concat([t])};return{matched:n.every(function(n){return h(e,n,u)}),selections:r}},getSelectionKeys:function(){return 0===t.length?[]:v(t[0])}}},n))},set:function(){var n,t=[].slice.call(arguments);return g(((n={})[c]=function(){return{match:function(n){if(!(n instanceof Set))return{matched:!1};var e={};if(0===n.size)return{matched:!0,selections:e};if(0===t.length)return{matched:!0};var r=function(n,t){e[n]=(e[n]||[]).concat([t])},u=t[0];return{matched:b(n,function(n){return h(u,n,r)}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:v(t[0])}}},n))},map:function(){var n,t=[].slice.call(arguments);return g(((n={})[c]=function(){return{match:function(n){if(!(n instanceof Map))return{matched:!1};var e={};if(0===n.size)return{matched:!0,selections:e};var r,u=function(n,t){e[n]=(e[n]||[]).concat([t])};if(0===t.length)return{matched:!0};if(1===t.length)throw new Error("`P.map` wasn't given enough arguments. Expected (key, value), received "+(null==(r=t[0])?void 0:r.toString()));var i=t[0],o=t[1];return{matched:w(n,function(n,t){var e=h(i,t,u),r=h(o,n,u);return e&&r}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:[].concat(v(t[0]),v(t[1]))}}},n))},intersection:O,union:S,not:function(n){var t;return g(((t={})[c]=function(){return{match:function(t){return{matched:!h(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},t))},when:j,select:x,any:P,_:K,string:T,number:B,bigint:M,boolean:I,symbol:N,nullish:k,nonNullable:W,instanceOf:function(n){return g(j(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return g(j(y(n)))}},z=/*#__PURE__*/function(n){var t,e;function r(t){var e,r;try{r=JSON.stringify(t)}catch(n){r=t}return(e=n.call(this,"Pattern matching error: no pattern matches value "+r)||this).input=void 0,e.input=t,e}return e=n,(t=r).prototype=Object.create(e.prototype),t.prototype.constructor=t,i(t,e),r}(/*#__PURE__*/o(Error)),F={matched:!1,value:void 0},L=/*#__PURE__*/function(){function n(n,t){this.input=void 0,this.state=void 0,this.input=n,this.state=t}var t=n.prototype;return t.with=function(){var t=this,e=[].slice.call(arguments);if(this.state.matched)return this;var r=e[e.length-1],u=[e[0]],i=void 0;3===e.length&&"function"==typeof e[1]?i=e[1]:e.length>2&&u.push.apply(u,e.slice(1,e.length-1));var o=!1,c={},f=function(n,t){o=!0,c[n]=t},l=!u.some(function(n){return h(n,t.input,f)})||i&&!Boolean(i(this.input))?F:{matched:!0,value:r(o?a in c?c[a]:c:this.input,this.input)};return new n(this.input,l)},t.when=function(t,e){if(this.state.matched)return this;var r=Boolean(t(this.input));return new n(this.input,r?{matched:!0,value:e(this.input,this.input)}:F)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(n){return void 0===n&&(n=U),this.state.matched?this.state.value:n(this.input)},t.run=function(){return this.exhaustive()},t.returnType=function(){return this},t.narrow=function(){return this},n}();function U(n){throw new z(n)}n.NonExhaustiveError=z,n.P=R,n.Pattern=R,n.isMatching=y,n.match=function(n){return new L(n,F)}});
//# sourceMappingURL=index.umd.js.map