UNPKG

ts-pattern

Version:

The exhaustive Pattern Matching library for TypeScript.

3 lines (2 loc) 11.8 kB
function n(t){return n=Object.setPrototypeOf?Object.getPrototypeOf:function(n){return n.__proto__||Object.getPrototypeOf(n)},n(t)}function t(n,r){return t=Object.setPrototypeOf||function(n,t){return n.__proto__=t,n},t(n,r)}function r(n,e,u){return r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(n){return!1}}()?Reflect.construct:function(n,r,e){var u=[null];u.push.apply(u,r);var i=new(Function.bind.apply(n,u));return e&&t(i,e.prototype),i},r.apply(null,arguments)}function e(u){var i="function"==typeof Map?new Map:void 0;return e=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(e))return i.get(e);i.set(e,u)}function u(){return r(e,arguments,n(this).constructor)}return u.prototype=Object.create(e.prototype,{constructor:{value:u,enumerable:!1,writable:!0,configurable:!0}}),t(u,e)},e(u)}function u(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function i(n,t){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,t){if(n){if("string"==typeof n)return u(n,t);var r=Object.prototype.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)?u(n,t):void 0}}(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var e=0;return function(){return e>=n.length?{done:!0}:{done:!1,value:n[e++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o=Symbol.for("@ts-pattern/matcher"),c=Symbol.for("@ts-pattern/isVariadic"),f="@ts-pattern/anonymous-select-key",a=function(n){return Boolean(n&&"object"==typeof n)},l=function(n){return n&&!!n[o]},s=function n(t,r,e){if(l(t)){var u=t[o]().match(r),f=u.matched,s=u.selections;return f&&s&&Object.keys(s).forEach(function(n){return e(n,s[n])}),f}if(a(t)){if(!a(r))return!1;if(Array.isArray(t)){if(!Array.isArray(r))return!1;for(var h,v=[],p=[],g=[],y=i(t.keys());!(h=y()).done;){var m=t[h.value];l(m)&&m[c]?g.push(m):g.length?p.push(m):v.push(m)}if(g.length){if(g.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 d=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,r){return n(t,d[r],e)})&&p.every(function(t,r){return n(t,b[r],e)})&&(0===g.length||n(g[0],w,e))}return t.length===r.length&&t.every(function(t,u){return n(t,r[u],e)})}return Reflect.ownKeys(t).every(function(u){var i,c=t[u];return(u in r||l(i=c)&&"optional"===i[o]().matcherType)&&n(c,r[u],e)})}return Object.is(r,t)},h=function n(t){var r,e,u;return a(t)?l(t)?null!=(r=null==(e=(u=t[o]()).getSelectionKeys)?void 0:e.call(u))?r:[]:Array.isArray(t)?v(t,n):v(Object.values(t),n):[]},v=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function p(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return s(t,n,function(){})}}if(2===n.length)return s(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 w(n,t)},or:function(t){return O(n,t)},select:function(t){return void 0===t?j(n):j(t,n)}})}function y(n){return Object.assign(function(n){var t;return Object.assign(n,((t={})[Symbol.iterator]=function(){var t,r=0,e=[{value:Object.assign(n,((t={})[c]=!0,t)),done:!1},{done:!0,value:void 0}];return{next:function(){var n;return null!=(n=e[r++])?n:e.at(-1)}}},t))}(n),{optional:function(){return y(m(n))},select:function(t){return y(void 0===t?j(n):j(t,n))}})}function m(n){var t;return g(((t={})[o]=function(){return{match:function(t){var r={},e=function(n,t){r[n]=t};return void 0===t?(h(n).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:s(n,t,e),selections:r}},getSelectionKeys:function(){return h(n)},matcherType:"optional"}},t))}var d=function(n,t){for(var r,e=i(n);!(r=e()).done;)if(!t(r.value))return!1;return!0},b=function(n,t){for(var r,e=i(n.entries());!(r=e()).done;){var u=r.value;if(!t(u[1],u[0]))return!1}return!0};function w(){var n,t=[].slice.call(arguments);return g(((n={})[o]=function(){return{match:function(n){var r={},e=function(n,t){r[n]=t};return{matched:t.every(function(t){return s(t,n,e)}),selections:r}},getSelectionKeys:function(){return v(t,h)},matcherType:"and"}},n))}function O(){var n,t=[].slice.call(arguments);return g(((n={})[o]=function(){return{match:function(n){var r={},e=function(n,t){r[n]=t};return v(t,h).forEach(function(n){return e(n,void 0)}),{matched:t.some(function(t){return s(t,n,e)}),selections:r}},getSelectionKeys:function(){return v(t,h)},matcherType:"or"}},n))}function S(n){var t;return(t={})[o]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},t}function j(){var n,t=[].slice.call(arguments),r="string"==typeof t[0]?t[0]:void 0,e=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return g(((n={})[o]=function(){return{match:function(n){var t,u=((t={})[null!=r?r:f]=n,t);return{matched:void 0===e||s(e,n,function(n,t){u[n]=t}),selections:u}},getSelectionKeys:function(){return[null!=r?r:f].concat(void 0===e?[]:h(e))}}},n))}function x(n){return"number"==typeof n}function A(n){return"string"==typeof n}function E(n){return"bigint"==typeof n}var _=g(S(function(n){return!0})),P=_,K=function n(t){return Object.assign(g(t),{startsWith:function(r){return n(w(t,(e=r,S(function(n){return A(n)&&n.startsWith(e)}))));var e},endsWith:function(r){return n(w(t,(e=r,S(function(n){return A(n)&&n.endsWith(e)}))));var e},minLength:function(r){return n(w(t,function(n){return S(function(t){return A(t)&&t.length>=n})}(r)))},length:function(r){return n(w(t,function(n){return S(function(t){return A(t)&&t.length===n})}(r)))},maxLength:function(r){return n(w(t,function(n){return S(function(t){return A(t)&&t.length<=n})}(r)))},includes:function(r){return n(w(t,(e=r,S(function(n){return A(n)&&n.includes(e)}))));var e},regex:function(r){return n(w(t,(e=r,S(function(n){return A(n)&&Boolean(n.match(e))}))));var e}})}(S(A)),T=function n(t){return Object.assign(g(t),{between:function(r,e){return n(w(t,function(n,t){return S(function(r){return x(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(w(t,function(n){return S(function(t){return x(t)&&t<n})}(r)))},gt:function(r){return n(w(t,function(n){return S(function(t){return x(t)&&t>n})}(r)))},lte:function(r){return n(w(t,function(n){return S(function(t){return x(t)&&t<=n})}(r)))},gte:function(r){return n(w(t,function(n){return S(function(t){return x(t)&&t>=n})}(r)))},int:function(){return n(w(t,S(function(n){return x(n)&&Number.isInteger(n)})))},finite:function(){return n(w(t,S(function(n){return x(n)&&Number.isFinite(n)})))},positive:function(){return n(w(t,S(function(n){return x(n)&&n>0})))},negative:function(){return n(w(t,S(function(n){return x(n)&&n<0})))}})}(S(x)),B=function n(t){return Object.assign(g(t),{between:function(r,e){return n(w(t,function(n,t){return S(function(r){return E(r)&&n<=r&&t>=r})}(r,e)))},lt:function(r){return n(w(t,function(n){return S(function(t){return E(t)&&t<n})}(r)))},gt:function(r){return n(w(t,function(n){return S(function(t){return E(t)&&t>n})}(r)))},lte:function(r){return n(w(t,function(n){return S(function(t){return E(t)&&t<=n})}(r)))},gte:function(r){return n(w(t,function(n){return S(function(t){return E(t)&&t>=n})}(r)))},positive:function(){return n(w(t,S(function(n){return E(n)&&n>0})))},negative:function(){return n(w(t,S(function(n){return E(n)&&n<0})))}})}(S(E)),M=g(S(function(n){return"boolean"==typeof n})),R=g(S(function(n){return"symbol"==typeof n})),I=g(S(function(n){return null==n})),N=g(S(function(n){return null!=n})),k={__proto__:null,matcher:o,optional:m,array:function(){var n,t=[].slice.call(arguments);return y(((n={})[o]=function(){return{match:function(n){if(!Array.isArray(n))return{matched:!1};if(0===t.length)return{matched:!0};var r=t[0],e={};if(0===n.length)return h(r).forEach(function(n){e[n]=[]}),{matched:!0,selections:e};var u=function(n,t){e[n]=(e[n]||[]).concat([t])};return{matched:n.every(function(n){return s(r,n,u)}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:h(t[0])}}},n))},set:function(){var n,t=[].slice.call(arguments);return g(((n={})[o]=function(){return{match:function(n){if(!(n instanceof Set))return{matched:!1};var r={};if(0===n.size)return{matched:!0,selections:r};if(0===t.length)return{matched:!0};var e=function(n,t){r[n]=(r[n]||[]).concat([t])},u=t[0];return{matched:d(n,function(n){return s(u,n,e)}),selections:r}},getSelectionKeys:function(){return 0===t.length?[]:h(t[0])}}},n))},map:function(){var n,t=[].slice.call(arguments);return g(((n={})[o]=function(){return{match:function(n){if(!(n instanceof Map))return{matched:!1};var r={};if(0===n.size)return{matched:!0,selections:r};var e,u=function(n,t){r[n]=(r[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==(e=t[0])?void 0:e.toString()));var i=t[0],o=t[1];return{matched:b(n,function(n,t){var r=s(i,t,u),e=s(o,n,u);return r&&e}),selections:r}},getSelectionKeys:function(){return 0===t.length?[]:[].concat(h(t[0]),h(t[1]))}}},n))},intersection:w,union:O,not:function(n){var t;return g(((t={})[o]=function(){return{match:function(t){return{matched:!s(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},t))},when:S,select:j,any:_,_:P,string:K,number:T,bigint:B,boolean:M,symbol:R,nullish:I,nonNullable:N,instanceOf:function(n){return g(S(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return g(S(p(n)))}},W=/*#__PURE__*/function(n){var r,e;function u(t){var r,e;try{e=JSON.stringify(t)}catch(n){e=t}return(r=n.call(this,"Pattern matching error: no pattern matches value "+e)||this).input=void 0,r.input=t,r}return e=n,(r=u).prototype=Object.create(e.prototype),r.prototype.constructor=r,t(r,e),u}(/*#__PURE__*/e(Error)),F={matched:!1,value:void 0},z=/*#__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,r=[].slice.call(arguments);if(this.state.matched)return this;var e=r[r.length-1],u=[r[0]],i=void 0;3===r.length&&"function"==typeof r[1]?i=r[1]:r.length>2&&u.push.apply(u,r.slice(1,r.length-1));var o=!1,c={},a=function(n,t){o=!0,c[n]=t},l=!u.some(function(n){return s(n,t.input,a)})||i&&!Boolean(i(this.input))?F:{matched:!0,value:e(o?f in c?c[f]:c:this.input,this.input)};return new n(this.input,l)},t.when=function(t,r){if(this.state.matched)return this;var e=Boolean(t(this.input));return new n(this.input,e?{matched:!0,value:r(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=L),this.state.matched?this.state.value:n(this.input)},t.run=function(){return this.exhaustive()},t.returnType=function(){return this},n}();function L(n){throw new W(n)}exports.NonExhaustiveError=W,exports.P=k,exports.Pattern=k,exports.isMatching=p,exports.match=function(n){return new z(n,F)}; //# sourceMappingURL=index.cjs.map