@cuppachino/ts-pattern
Version:
The exhaustive Pattern Matching library for TypeScript.
3 lines (2 loc) • 5.54 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){var t=Symbol("@ts-pattern/matcher"),e="@ts-pattern/anonymous-select-key",r=function(n){return Boolean(n&&"object"==typeof n)},u=function(n){return n&&!!n[t]},i=function n(e,i,o){if(r(e)){if(u(e)){var c=e[t]().match(i),a=c.matched,f=c.selections;return a&&f&&Object.keys(f).forEach(function(n){return o(n,f[n])}),a}if(!r(i))return!1;if(Array.isArray(e))return!!Array.isArray(i)&&e.length===i.length&&e.every(function(t,e){return n(t,i[e],o)});if(e instanceof Map)return i instanceof Map&&Array.from(e.keys()).every(function(t){return n(e.get(t),i.get(t),o)});if(e instanceof Set){if(!(i instanceof Set))return!1;if(0===e.size)return 0===i.size;if(1===e.size){var s=Array.from(e.values())[0];return u(s)?Array.from(i.values()).every(function(t){return n(s,t,o)}):i.has(s)}return Array.from(e.values()).every(function(n){return i.has(n)})}return Object.keys(e).every(function(r){var c,a=e[r];return(r in i||u(c=a)&&"optional"===c[t]().matcherType)&&n(a,i[r],o)})}return Object.is(i,e)},o=function n(e){var i,o,a;return r(e)?u(e)?null!=(i=null==(o=(a=e[t]()).getSelectionKeys)?void 0:o.call(a))?i:[]:Array.isArray(e)?c(e,n):c(Object.values(e),n):[]},c=function(n,t){return n.reduce(function(n,e){return n.concat(t(e))},[])};function a(n){var e;return(e={})[t]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(o(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:i(n,t,r),selections:e}},getSelectionKeys:function(){return o(n)},matcherType:"optional"}},e}function f(n){var e;return(e={})[t]=function(){return{match:function(t){if(!Array.isArray(t))return{matched:!1};var e={};if(0===t.length)return o(n).forEach(function(n){e[n]=[]}),{matched:!0,selections:e};var r=function(n,t){e[n]=(e[n]||[]).concat([t])};return{matched:t.every(function(t){return i(n,t,r)}),selections:e}},getSelectionKeys:function(){return o(n)}}},e}function s(){var n,e=[].slice.call(arguments);return(n={})[t]=function(){return{match:function(n){var t={},r=function(n,e){t[n]=e};return{matched:e.every(function(t){return i(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,o)},matcherType:"and"}},n}function l(){var n,e=[].slice.call(arguments);return(n={})[t]=function(){return{match:function(n){var t={},r=function(n,e){t[n]=e};return c(e,o).forEach(function(n){return r(n,void 0)}),{matched:e.some(function(t){return i(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,o)},matcherType:"or"}},n}function h(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:!i(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},e}function v(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},e}function y(){var n,r=[].slice.call(arguments),u="string"==typeof r[0]?r[0]:void 0,c=2===r.length?r[1]:"string"==typeof r[0]?void 0:r[0];return(n={})[t]=function(){return{match:function(n){var t,r=((t={})[null!=u?u:e]=n,t);return{matched:void 0===c||i(c,n,function(n,t){r[n]=t}),selections:r}},getSelectionKeys:function(){return[null!=u?u:e].concat(void 0===c?[]:o(c))}}},n}var m=v(function(n){return!0}),d=m,p=v(function(n){return"string"==typeof n}),g=v(function(n){return"number"==typeof n}),b=v(function(n){return"boolean"==typeof n}),w=v(function(n){return"bigint"==typeof n}),A=v(function(n){return"symbol"==typeof n}),S=v(function(n){return null==n}),j={__proto__:null,optional:a,array:f,intersection:s,union:l,not:h,when:v,select:y,any:m,_:d,string:p,number:g,boolean:b,bigint:w,symbol:A,nullish:S,instanceOf:function(n){return v(function(n){return function(t){return t instanceof n}}(n))},typed:function(){return{array:f,optional:a,intersection:s,union:l,not:h,select:y,when:v}}},K=/*#__PURE__*/function(){function n(n,t){this.value=void 0,this.cases=void 0,this.value=n,this.cases=t}var t=n.prototype;return t.with=function(){var t=[].slice.call(arguments),r=t[t.length-1],u=[t[0]],o=[];return 3===t.length&&"function"==typeof t[1]?(u.push(t[0]),o.push(t[1])):t.length>2&&u.push.apply(u,t.slice(1,t.length-1)),new n(this.value,this.cases.concat([{match:function(n){var t={},r=Boolean(u.some(function(e){return i(e,n,function(n,e){t[n]=e})})&&o.every(function(t){return t(n)}));return{matched:r,value:r&&Object.keys(t).length?e in t?t[e]:t:n}},handler:r}]))},t.when=function(t,e){return new n(this.value,this.cases.concat([{match:function(n){return{matched:Boolean(t(n)),value:n}},handler:e}]))},t.otherwise=function(t){return new n(this.value,this.cases.concat([{match:function(n){return{matched:!0,value:n}},handler:t}])).run()},t.exhaustive=function(){return this.run()},t.run=function(){for(var n=this.value,t=void 0,e=0;e<this.cases.length;e++){var r=this.cases[e],u=r.match(this.value);if(u.matched){n=u.value,t=r.handler;break}}if(!t){var i;try{i=JSON.stringify(this.value)}catch(n){i=this.value}throw new Error("Pattern matching error: no pattern matches value "+i)}return t(n,this.value)},n}();n.P=j,n.Pattern=j,n.isMatching=function(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return i(t,n,function(){})}}if(2===n.length){var e=n,r=e[0],u=e[1];return i(r,u,function(){})}throw new Error("isMatching wasn't given the right number of arguments: expected 1 or 2, received "+n.length+".")},n.match=function(n){return new K(n,[])}});
//# sourceMappingURL=index.umd.js.map