UNPKG

ts-pattern

Version:

The exhaustive Pattern Matching library for TypeScript.

3 lines (2 loc) 11.8 kB
function n(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=Array(t);r<t;r++)e[r]=n[r];return e}function t(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return n(t,r);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var u=0;return function(){return u>=t.length?{done:!0}:{done:!1,value:t[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 e(){try{var n=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(n){}return(e=function(){return!!n})()}function u(n,t){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(n,t){return n.__proto__=t,n},u(n,t)}function i(n){var t="function"==typeof Map?new Map:void 0;return i=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,i)}function i(){return function(n,t,r){if(e())return Reflect.construct.apply(null,arguments);var i=[null];i.push.apply(i,t);var o=new(n.bind.apply(n,i));return r&&u(o,r.prototype),o}(n,arguments,r(this).constructor)}return i.prototype=Object.create(n.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),u(i,n)},i(n)}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(r,e,u){if(l(r)){var i=r[o]().match(e),f=i.matched,s=i.selections;return f&&s&&Object.keys(s).forEach(function(n){return u(n,s[n])}),f}if(a(r)){if(!a(e))return!1;if(Array.isArray(r)){if(!Array.isArray(e))return!1;for(var h,v=[],p=[],g=[],y=t(r.keys());!(h=y()).done;){var m=r[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(e.length<v.length+p.length)return!1;var d=e.slice(0,v.length),b=0===p.length?[]:e.slice(-p.length),w=e.slice(v.length,0===p.length?Infinity:-p.length);return v.every(function(t,r){return n(t,d[r],u)})&&p.every(function(t,r){return n(t,b[r],u)})&&(0===g.length||n(g[0],w,u))}return r.length===e.length&&r.every(function(t,r){return n(t,e[r],u)})}return Reflect.ownKeys(r).every(function(t){var i,c=r[t];return(t in e||l(i=c)&&"optional"===i[o]().matcherType)&&n(c,e[t],u)})}return Object.is(e,r)},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,r){for(var e,u=t(n);!(e=u()).done;)if(!r(e.value))return!1;return!0},b=function(n,r){for(var e,u=t(n.entries());!(e=u()).done;){var i=e.value;if(!r(i[1],i[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})),I=g(S(function(n){return"symbol"==typeof n})),N=g(S(function(n){return null==n})),k=g(S(function(n){return null!=n})),W={__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:I,nullish:N,nonNullable:k,instanceOf:function(n){return g(S(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return g(S(p(n)))}},R=/*#__PURE__*/function(n){var t,r;function e(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 r=n,(t=e).prototype=Object.create(r.prototype),t.prototype.constructor=t,u(t,r),e}(/*#__PURE__*/i(Error)),z={matched:!1,value:void 0},F=/*#__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))?z:{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)}:z)},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},t.narrow=function(){return this},n}();function L(n){throw new R(n)}exports.NonExhaustiveError=R,exports.P=W,exports.Pattern=W,exports.isMatching=p,exports.match=function(n){return new F(n,z)}; //# sourceMappingURL=index.cjs.map