ts-pattern
Advanced tools
Comparing version 4.0.1-rc.15 to 4.0.1-rc.16
@@ -5,9 +5,1 @@ import * as Pattern from './patterns'; | ||
export { Pattern, Pattern as P }; | ||
/** | ||
* ### Catch All wildcard | ||
* `__` is a wildcard pattern, matching **any value**. | ||
* @example | ||
* match(value) | ||
* .with(__, () => 'will always match') | ||
*/ | ||
export declare const __: import("./types/Pattern").GuardP<unknown, unknown>; |
@@ -1,2 +0,2 @@ | ||
const e=Symbol("@ts-pattern/matcher"),t="@ts-pattern/anonymous-select-key",n=e=>Boolean(e&&"object"==typeof e),r=t=>t&&!!t[e],o=(t,c,i)=>{if(n(t)){if(r(t)){const n=t[e](),{matched:r,selections:o={}}=n.match(c);return r&&Object.keys(o).forEach(e=>i(e,o[e])),r}if(!n(c))return!1;if(Array.isArray(t))return!!Array.isArray(c)&&t.length===c.length&&t.every((e,t)=>o(e,c[t],i));if(t instanceof Map)return c instanceof Map&&Array.from(t.keys()).every(e=>o(t.get(e),c.get(e),i));if(t instanceof Set){if(!(c instanceof Set))return!1;if(0===t.size)return 0===c.size;if(1===t.size){const[e]=Array.from(t.values());return r(e)?Array.from(c.values()).every(t=>o(e,t,i)):c.has(e)}return Array.from(t.values()).every(e=>c.has(e))}return Object.keys(t).every(n=>{const s=t[n];return(n in c||r(a=s)&&"optional"===a[e]().matcherType)&&o(s,c[n],i);var a})}return Object.is(c,t)},c=t=>{var o,s,a;return n(t)?r(t)?null!=(o=null==(s=(a=t[e]()).getSelectionKeys)?void 0:s.call(a))?o:[]:Array.isArray(t)?i(t,c):i(Object.values(t),c):[]},i=(e,t)=>e.reduce((e,n)=>e.concat(t(n)),[]);function s(t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return void 0===e?(c(t).forEach(e=>r(e,void 0)),{matched:!0,selections:n}):{matched:o(t,e,r),selections:n}},getSelectionKeys:()=>c(t),matcherType:"optional"})}}function a(t){return{[e]:()=>({match:e=>{if(!Array.isArray(e))return{matched:!1};let n={};const r=(e,t)=>{n[e]=(n[e]||[]).concat([t])};return{matched:e.every(e=>o(t,e,r)),selections:n}},getSelectionKeys:()=>c(t)})}}function u(...t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return{matched:t.every(t=>o(t,e,r)),selections:n}},getSelectionKeys:()=>i(t,c),matcherType:"and"})}}function l(...t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return i(t,c).forEach(e=>r(e,void 0)),{matched:t.some(t=>o(t,e,r)),selections:n}},getSelectionKeys:()=>i(t,c),matcherType:"or"})}}function h(t){return{[e]:()=>({match:e=>({matched:!o(t,e,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})}}function f(t){return{[e]:()=>({match:e=>({matched:Boolean(t(e))})})}}function y(...n){const r="string"==typeof n[0]?n[0]:void 0,i=2===n.length?n[1]:"string"==typeof n[0]?void 0:n[0];return{[e]:()=>({match:e=>{let n={[null!=r?r:t]:e};return{matched:void 0===i||o(i,e,(e,t)=>{n[e]=t}),selections:n}},getSelectionKeys:()=>[null!=r?r:t].concat(void 0===i?[]:c(i))})}}const m=f(function(e){return!0}),g=f(function(e){return"string"==typeof e}),p=f(function(e){return"number"==typeof e}),d=f(function(e){return"boolean"==typeof e}),v=f(function(e){return"bigint"==typeof e}),b=f(function(e){return"symbol"==typeof e}),A=f(function(e){return null==e});var S={__proto__:null,optional:s,array:a,intersection:u,union:l,not:h,when:f,select:y,any:m,string:g,number:p,boolean:d,bigint:v,symbol:b,nullish:A,instanceOf:function(e){return f(function(e){return t=>t instanceof e}(e))},typed:function(){return{array:a,optional:s,intersection:u,union:l,not:h,select:y,when:f}}};const w=e=>K(e,[]),K=(e,n)=>{const r=()=>{const t=n.find(({test:t})=>t(e));if(!t){let t;try{t=JSON.stringify(e)}catch(n){t=e}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}return t.handler(t.select(e),e)};return{with(...r){const c=r[r.length-1],i=[],s=[];3===r.length&&"function"==typeof r[1]?(i.push(r[0]),s.push(r[1])):i.push(...r.slice(0,r.length-1));let a={};return K(e,n.concat([{test:e=>Boolean(i.some(t=>o(t,e,(e,t)=>{a[e]=t}))&&s.every(t=>t(e))),handler:c,select:e=>Object.keys(a).length?t in a?a[t]:a:e}]))},when:(t,r)=>K(e,n.concat([{test:t,handler:r,select:e=>e}])),otherwise:t=>K(e,n.concat([{test:()=>!0,handler:t,select:e=>e}])).run(),exhaustive:()=>r(),run:r}};function O(...e){if(1===e.length){const[t]=e;return e=>o(t,e,()=>{})}if(2===e.length){const[t,n]=e;return o(t,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${e.length}.`)}const j=m;export{S as P,S as Pattern,j as __,O as isMatching,w as match}; | ||
const e=Symbol("@ts-pattern/matcher"),t="@ts-pattern/anonymous-select-key",n=e=>Boolean(e&&"object"==typeof e),r=t=>t&&!!t[e],o=(t,c,i)=>{if(n(t)){if(r(t)){const n=t[e](),{matched:r,selections:o={}}=n.match(c);return r&&Object.keys(o).forEach(e=>i(e,o[e])),r}if(!n(c))return!1;if(Array.isArray(t))return!!Array.isArray(c)&&t.length===c.length&&t.every((e,t)=>o(e,c[t],i));if(t instanceof Map)return c instanceof Map&&Array.from(t.keys()).every(e=>o(t.get(e),c.get(e),i));if(t instanceof Set){if(!(c instanceof Set))return!1;if(0===t.size)return 0===c.size;if(1===t.size){const[e]=Array.from(t.values());return r(e)?Array.from(c.values()).every(t=>o(e,t,i)):c.has(e)}return Array.from(t.values()).every(e=>c.has(e))}return Object.keys(t).every(n=>{const s=t[n];return(n in c||r(a=s)&&"optional"===a[e]().matcherType)&&o(s,c[n],i);var a})}return Object.is(c,t)},c=t=>{var o,s,a;return n(t)?r(t)?null!=(o=null==(s=(a=t[e]()).getSelectionKeys)?void 0:s.call(a))?o:[]:Array.isArray(t)?i(t,c):i(Object.values(t),c):[]},i=(e,t)=>e.reduce((e,n)=>e.concat(t(n)),[]);function s(t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return void 0===e?(c(t).forEach(e=>r(e,void 0)),{matched:!0,selections:n}):{matched:o(t,e,r),selections:n}},getSelectionKeys:()=>c(t),matcherType:"optional"})}}function a(t){return{[e]:()=>({match:e=>{if(!Array.isArray(e))return{matched:!1};let n={};const r=(e,t)=>{n[e]=(n[e]||[]).concat([t])};return{matched:e.every(e=>o(t,e,r)),selections:n}},getSelectionKeys:()=>c(t)})}}function u(...t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return{matched:t.every(t=>o(t,e,r)),selections:n}},getSelectionKeys:()=>i(t,c),matcherType:"and"})}}function l(...t){return{[e]:()=>({match:e=>{let n={};const r=(e,t)=>{n[e]=t};return i(t,c).forEach(e=>r(e,void 0)),{matched:t.some(t=>o(t,e,r)),selections:n}},getSelectionKeys:()=>i(t,c),matcherType:"or"})}}function h(t){return{[e]:()=>({match:e=>({matched:!o(t,e,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})}}function f(t){return{[e]:()=>({match:e=>({matched:Boolean(t(e))})})}}function y(...n){const r="string"==typeof n[0]?n[0]:void 0,i=2===n.length?n[1]:"string"==typeof n[0]?void 0:n[0];return{[e]:()=>({match:e=>{let n={[null!=r?r:t]:e};return{matched:void 0===i||o(i,e,(e,t)=>{n[e]=t}),selections:n}},getSelectionKeys:()=>[null!=r?r:t].concat(void 0===i?[]:c(i))})}}const m=f(function(e){return!0}),g=m,p=f(function(e){return"string"==typeof e}),d=f(function(e){return"number"==typeof e}),v=f(function(e){return"boolean"==typeof e}),b=f(function(e){return"bigint"==typeof e}),A=f(function(e){return"symbol"==typeof e}),S=f(function(e){return null==e});var w={__proto__:null,optional:s,array:a,intersection:u,union:l,not:h,when:f,select:y,any:m,_:g,string:p,number:d,boolean:v,bigint:b,symbol:A,nullish:S,instanceOf:function(e){return f(function(e){return t=>t instanceof e}(e))},typed:function(){return{array:a,optional:s,intersection:u,union:l,not:h,select:y,when:f}}};const K=e=>O(e,[]),O=(e,n)=>{const r=()=>{const t=n.find(({test:t})=>t(e));if(!t){let t;try{t=JSON.stringify(e)}catch(n){t=e}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}return t.handler(t.select(e),e)};return{with(...r){const c=r[r.length-1],i=[],s=[];3===r.length&&"function"==typeof r[1]?(i.push(r[0]),s.push(r[1])):i.push(...r.slice(0,r.length-1));let a={};return O(e,n.concat([{test:e=>Boolean(i.some(t=>o(t,e,(e,t)=>{a[e]=t}))&&s.every(t=>t(e))),handler:c,select:e=>Object.keys(a).length?t in a?a[t]:a:e}]))},when:(t,r)=>O(e,n.concat([{test:t,handler:r,select:e=>e}])),otherwise:t=>O(e,n.concat([{test:()=>!0,handler:t,select:e=>e}])).run(),exhaustive:()=>r(),run:r}};function j(...e){if(1===e.length){const[t]=e;return e=>o(t,e,()=>{})}if(2===e.length){const[t,n]=e;return o(t,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${e.length}.`)}export{w as P,w as Pattern,j as isMatching,K as match}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
var n=Symbol("@ts-pattern/matcher"),t="@ts-pattern/anonymous-select-key",r=function(n){return Boolean(n&&"object"==typeof n)},e=function(t){return t&&!!t[n]},u=function t(u,o,c){if(r(u)){if(e(u)){var i=u[n]().match(o),a=i.matched,f=i.selections,s=void 0===f?{}:f;return a&&Object.keys(s).forEach(function(n){return c(n,s[n])}),a}if(!r(o))return!1;if(Array.isArray(u))return!!Array.isArray(o)&&u.length===o.length&&u.every(function(n,r){return t(n,o[r],c)});if(u instanceof Map)return o instanceof Map&&Array.from(u.keys()).every(function(n){return t(u.get(n),o.get(n),c)});if(u instanceof Set){if(!(o instanceof Set))return!1;if(0===u.size)return 0===o.size;if(1===u.size){var l=Array.from(u.values())[0];return e(l)?Array.from(o.values()).every(function(n){return t(l,n,c)}):o.has(l)}return Array.from(u.values()).every(function(n){return o.has(n)})}return Object.keys(u).every(function(r){var i,a=u[r];return(r in o||e(i=a)&&"optional"===i[n]().matcherType)&&t(a,o[r],c)})}return Object.is(o,u)},o=function t(u){var o,i,a;return r(u)?e(u)?null!=(o=null==(i=(a=u[n]()).getSelectionKeys)?void 0:i.call(a))?o:[]:Array.isArray(u)?c(u,t):c(Object.values(u),t):[]},c=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function i(t){var r;return(r={})[n]=function(){return{match:function(n){var r={},e=function(n,t){r[n]=t};return void 0===n?(o(t).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:u(t,n,e),selections:r}},getSelectionKeys:function(){return o(t)},matcherType:"optional"}},r}function a(t){var r;return(r={})[n]=function(){return{match:function(n){if(!Array.isArray(n))return{matched:!1};var r={},e=function(n,t){r[n]=(r[n]||[]).concat([t])};return{matched:n.every(function(n){return u(t,n,e)}),selections:r}},getSelectionKeys:function(){return o(t)}}},r}function f(){var t,r=[].slice.call(arguments);return(t={})[n]=function(){return{match:function(n){var t={},e=function(n,r){t[n]=r};return{matched:r.every(function(t){return u(t,n,e)}),selections:t}},getSelectionKeys:function(){return c(r,o)},matcherType:"and"}},t}function s(){var t,r=[].slice.call(arguments);return(t={})[n]=function(){return{match:function(n){var t={},e=function(n,r){t[n]=r};return c(r,o).forEach(function(n){return e(n,void 0)}),{matched:r.some(function(t){return u(t,n,e)}),selections:t}},getSelectionKeys:function(){return c(r,o)},matcherType:"or"}},t}function l(t){var r;return(r={})[n]=function(){return{match:function(n){return{matched:!u(t,n,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},r}function h(t){var r;return(r={})[n]=function(){return{match:function(n){return{matched:Boolean(t(n))}}}},r}function y(){var r,e=[].slice.call(arguments),c="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return(r={})[n]=function(){return{match:function(n){var r,e=((r={})[null!=c?c:t]=n,r);return{matched:void 0===i||u(i,n,function(n,t){e[n]=t}),selections:e}},getSelectionKeys:function(){return[null!=c?c:t].concat(void 0===i?[]:o(i))}}},r}var v=h(function(n){return!0}),m=h(function(n){return"string"==typeof n}),p=h(function(n){return"number"==typeof n}),g=h(function(n){return"boolean"==typeof n}),d=h(function(n){return"bigint"==typeof n}),b=h(function(n){return"symbol"==typeof n}),A=h(function(n){return null==n}),S={__proto__:null,optional:i,array:a,intersection:f,union:s,not:l,when:h,select:y,any:v,string:m,number:p,boolean:g,bigint:d,symbol:b,nullish:A,instanceOf:function(n){return h(function(n){return function(t){return t instanceof n}}(n))},typed:function(){return{array:a,optional:i,intersection:f,union:s,not:l,select:y,when:h}}},w=function(n){return K(n,[])},K=function n(r,e){var o=function(){var n=e.find(function(n){return(0,n.test)(r)});if(!n){var t;try{t=JSON.stringify(r)}catch(n){t=r}throw new Error("Pattern matching error: no pattern matches value "+t)}return n.handler(n.select(r),r)};return{with:function(){var o=[].slice.call(arguments),c=o[o.length-1],i=[],a=[];3===o.length&&"function"==typeof o[1]?(i.push(o[0]),a.push(o[1])):i.push.apply(i,o.slice(0,o.length-1));var f={},s=function(n){return Boolean(i.some(function(t){return u(t,n,function(n,t){f[n]=t})})&&a.every(function(t){return t(n)}))};return n(r,e.concat([{test:s,handler:c,select:function(n){return Object.keys(f).length?t in f?f[t]:f:n}}]))},when:function(t,u){return n(r,e.concat([{test:t,handler:u,select:function(n){return n}}]))},otherwise:function(t){return n(r,e.concat([{test:function(){return!0},handler:t,select:function(n){return n}}])).run()},exhaustive:function(){return o()},run:o}};function O(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return u(t,n,function(){})}}if(2===n.length){var r=n,e=r[0],o=r[1];return u(e,o,function(){})}throw new Error("isMatching wasn't given the right number of arguments: expected 1 or 2, received "+n.length+".")}var j=v;export{S as P,S as Pattern,j as __,O as isMatching,w as match}; | ||
var n=Symbol("@ts-pattern/matcher"),t="@ts-pattern/anonymous-select-key",r=function(n){return Boolean(n&&"object"==typeof n)},e=function(t){return t&&!!t[n]},u=function t(u,o,c){if(r(u)){if(e(u)){var i=u[n]().match(o),a=i.matched,f=i.selections,s=void 0===f?{}:f;return a&&Object.keys(s).forEach(function(n){return c(n,s[n])}),a}if(!r(o))return!1;if(Array.isArray(u))return!!Array.isArray(o)&&u.length===o.length&&u.every(function(n,r){return t(n,o[r],c)});if(u instanceof Map)return o instanceof Map&&Array.from(u.keys()).every(function(n){return t(u.get(n),o.get(n),c)});if(u instanceof Set){if(!(o instanceof Set))return!1;if(0===u.size)return 0===o.size;if(1===u.size){var l=Array.from(u.values())[0];return e(l)?Array.from(o.values()).every(function(n){return t(l,n,c)}):o.has(l)}return Array.from(u.values()).every(function(n){return o.has(n)})}return Object.keys(u).every(function(r){var i,a=u[r];return(r in o||e(i=a)&&"optional"===i[n]().matcherType)&&t(a,o[r],c)})}return Object.is(o,u)},o=function t(u){var o,i,a;return r(u)?e(u)?null!=(o=null==(i=(a=u[n]()).getSelectionKeys)?void 0:i.call(a))?o:[]:Array.isArray(u)?c(u,t):c(Object.values(u),t):[]},c=function(n,t){return n.reduce(function(n,r){return n.concat(t(r))},[])};function i(t){var r;return(r={})[n]=function(){return{match:function(n){var r={},e=function(n,t){r[n]=t};return void 0===n?(o(t).forEach(function(n){return e(n,void 0)}),{matched:!0,selections:r}):{matched:u(t,n,e),selections:r}},getSelectionKeys:function(){return o(t)},matcherType:"optional"}},r}function a(t){var r;return(r={})[n]=function(){return{match:function(n){if(!Array.isArray(n))return{matched:!1};var r={},e=function(n,t){r[n]=(r[n]||[]).concat([t])};return{matched:n.every(function(n){return u(t,n,e)}),selections:r}},getSelectionKeys:function(){return o(t)}}},r}function f(){var t,r=[].slice.call(arguments);return(t={})[n]=function(){return{match:function(n){var t={},e=function(n,r){t[n]=r};return{matched:r.every(function(t){return u(t,n,e)}),selections:t}},getSelectionKeys:function(){return c(r,o)},matcherType:"and"}},t}function s(){var t,r=[].slice.call(arguments);return(t={})[n]=function(){return{match:function(n){var t={},e=function(n,r){t[n]=r};return c(r,o).forEach(function(n){return e(n,void 0)}),{matched:r.some(function(t){return u(t,n,e)}),selections:t}},getSelectionKeys:function(){return c(r,o)},matcherType:"or"}},t}function l(t){var r;return(r={})[n]=function(){return{match:function(n){return{matched:!u(t,n,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},r}function h(t){var r;return(r={})[n]=function(){return{match:function(n){return{matched:Boolean(t(n))}}}},r}function y(){var r,e=[].slice.call(arguments),c="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return(r={})[n]=function(){return{match:function(n){var r,e=((r={})[null!=c?c:t]=n,r);return{matched:void 0===i||u(i,n,function(n,t){e[n]=t}),selections:e}},getSelectionKeys:function(){return[null!=c?c:t].concat(void 0===i?[]:o(i))}}},r}var v=h(function(n){return!0}),m=v,p=h(function(n){return"string"==typeof n}),g=h(function(n){return"number"==typeof n}),d=h(function(n){return"boolean"==typeof n}),b=h(function(n){return"bigint"==typeof n}),A=h(function(n){return"symbol"==typeof n}),S=h(function(n){return null==n}),w={__proto__:null,optional:i,array:a,intersection:f,union:s,not:l,when:h,select:y,any:v,_:m,string:p,number:g,boolean:d,bigint:b,symbol:A,nullish:S,instanceOf:function(n){return h(function(n){return function(t){return t instanceof n}}(n))},typed:function(){return{array:a,optional:i,intersection:f,union:s,not:l,select:y,when:h}}},K=function(n){return O(n,[])},O=function n(r,e){var o=function(){var n=e.find(function(n){return(0,n.test)(r)});if(!n){var t;try{t=JSON.stringify(r)}catch(n){t=r}throw new Error("Pattern matching error: no pattern matches value "+t)}return n.handler(n.select(r),r)};return{with:function(){var o=[].slice.call(arguments),c=o[o.length-1],i=[],a=[];3===o.length&&"function"==typeof o[1]?(i.push(o[0]),a.push(o[1])):i.push.apply(i,o.slice(0,o.length-1));var f={},s=function(n){return Boolean(i.some(function(t){return u(t,n,function(n,t){f[n]=t})})&&a.every(function(t){return t(n)}))};return n(r,e.concat([{test:s,handler:c,select:function(n){return Object.keys(f).length?t in f?f[t]:f:n}}]))},when:function(t,u){return n(r,e.concat([{test:t,handler:u,select:function(n){return n}}]))},otherwise:function(t){return n(r,e.concat([{test:function(){return!0},handler:t,select:function(n){return n}}])).run()},exhaustive:function(){return o()},run:o}};function j(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return u(t,n,function(){})}}if(2===n.length){var r=n,e=r[0],o=r[1];return u(e,o,function(){})}throw new Error("isMatching wasn't given the right number of arguments: expected 1 or 2, received "+n.length+".")}export{w as P,w as Pattern,j as isMatching,K as match}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!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]},o=function n(e,o,i){if(r(e)){if(u(e)){var c=e[t]().match(o),f=c.matched,a=c.selections,s=void 0===a?{}:a;return f&&Object.keys(s).forEach(function(n){return i(n,s[n])}),f}if(!r(o))return!1;if(Array.isArray(e))return!!Array.isArray(o)&&e.length===o.length&&e.every(function(t,e){return n(t,o[e],i)});if(e instanceof Map)return o instanceof Map&&Array.from(e.keys()).every(function(t){return n(e.get(t),o.get(t),i)});if(e instanceof Set){if(!(o instanceof Set))return!1;if(0===e.size)return 0===o.size;if(1===e.size){var l=Array.from(e.values())[0];return u(l)?Array.from(o.values()).every(function(t){return n(l,t,i)}):o.has(l)}return Array.from(e.values()).every(function(n){return o.has(n)})}return Object.keys(e).every(function(r){var c,f=e[r];return(r in o||u(c=f)&&"optional"===c[t]().matcherType)&&n(f,o[r],i)})}return Object.is(o,e)},i=function n(e){var o,i,f;return r(e)?u(e)?null!=(o=null==(i=(f=e[t]()).getSelectionKeys)?void 0:i.call(f))?o:[]: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 f(n){var e;return(e={})[t]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(i(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:o(n,t,r),selections:e}},getSelectionKeys:function(){return i(n)},matcherType:"optional"}},e}function a(n){var e;return(e={})[t]=function(){return{match:function(t){if(!Array.isArray(t))return{matched:!1};var e={},r=function(n,t){e[n]=(e[n]||[]).concat([t])};return{matched:t.every(function(t){return o(n,t,r)}),selections:e}},getSelectionKeys:function(){return i(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 o(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,i)},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,i).forEach(function(n){return r(n,void 0)}),{matched:e.some(function(t){return o(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,i)},matcherType:"or"}},n}function h(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:!o(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},e}function y(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},e}function v(){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||o(c,n,function(n,t){r[n]=t}),selections:r}},getSelectionKeys:function(){return[null!=u?u:e].concat(void 0===c?[]:i(c))}}},n}var m=y(function(n){return!0}),d=y(function(n){return"string"==typeof n}),p=y(function(n){return"number"==typeof n}),g=y(function(n){return"boolean"==typeof n}),b=y(function(n){return"bigint"==typeof n}),A=y(function(n){return"symbol"==typeof n}),S=y(function(n){return null==n}),w={__proto__:null,optional:f,array:a,intersection:s,union:l,not:h,when:y,select:v,any:m,string:d,number:p,boolean:g,bigint:b,symbol:A,nullish:S,instanceOf:function(n){return y(function(n){return function(t){return t instanceof n}}(n))},typed:function(){return{array:a,optional:f,intersection:s,union:l,not:h,select:v,when:y}}},j=function n(t,r){var u=function(){var n=r.find(function(n){return(0,n.test)(t)});if(!n){var e;try{e=JSON.stringify(t)}catch(n){e=t}throw new Error("Pattern matching error: no pattern matches value "+e)}return n.handler(n.select(t),t)};return{with:function(){var u=[].slice.call(arguments),i=u[u.length-1],c=[],f=[];3===u.length&&"function"==typeof u[1]?(c.push(u[0]),f.push(u[1])):c.push.apply(c,u.slice(0,u.length-1));var a={},s=function(n){return Boolean(c.some(function(t){return o(t,n,function(n,t){a[n]=t})})&&f.every(function(t){return t(n)}))};return n(t,r.concat([{test:s,handler:i,select:function(n){return Object.keys(a).length?e in a?a[e]:a:n}}]))},when:function(e,u){return n(t,r.concat([{test:e,handler:u,select:function(n){return n}}]))},otherwise:function(e){return n(t,r.concat([{test:function(){return!0},handler:e,select:function(n){return n}}])).run()},exhaustive:function(){return u()},run:u}},K=m;n.P=w,n.Pattern=w,n.__=K,n.isMatching=function(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return o(t,n,function(){})}}if(2===n.length){var e=n,r=e[0],u=e[1];return o(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 j(n,[])}}); | ||
!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]},o=function n(e,o,i){if(r(e)){if(u(e)){var c=e[t]().match(o),f=c.matched,a=c.selections,s=void 0===a?{}:a;return f&&Object.keys(s).forEach(function(n){return i(n,s[n])}),f}if(!r(o))return!1;if(Array.isArray(e))return!!Array.isArray(o)&&e.length===o.length&&e.every(function(t,e){return n(t,o[e],i)});if(e instanceof Map)return o instanceof Map&&Array.from(e.keys()).every(function(t){return n(e.get(t),o.get(t),i)});if(e instanceof Set){if(!(o instanceof Set))return!1;if(0===e.size)return 0===o.size;if(1===e.size){var l=Array.from(e.values())[0];return u(l)?Array.from(o.values()).every(function(t){return n(l,t,i)}):o.has(l)}return Array.from(e.values()).every(function(n){return o.has(n)})}return Object.keys(e).every(function(r){var c,f=e[r];return(r in o||u(c=f)&&"optional"===c[t]().matcherType)&&n(f,o[r],i)})}return Object.is(o,e)},i=function n(e){var o,i,f;return r(e)?u(e)?null!=(o=null==(i=(f=e[t]()).getSelectionKeys)?void 0:i.call(f))?o:[]: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 f(n){var e;return(e={})[t]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(i(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:o(n,t,r),selections:e}},getSelectionKeys:function(){return i(n)},matcherType:"optional"}},e}function a(n){var e;return(e={})[t]=function(){return{match:function(t){if(!Array.isArray(t))return{matched:!1};var e={},r=function(n,t){e[n]=(e[n]||[]).concat([t])};return{matched:t.every(function(t){return o(n,t,r)}),selections:e}},getSelectionKeys:function(){return i(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 o(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,i)},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,i).forEach(function(n){return r(n,void 0)}),{matched:e.some(function(t){return o(t,n,r)}),selections:t}},getSelectionKeys:function(){return c(e,i)},matcherType:"or"}},n}function h(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:!o(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},e}function y(n){var e;return(e={})[t]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},e}function v(){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||o(c,n,function(n,t){r[n]=t}),selections:r}},getSelectionKeys:function(){return[null!=u?u:e].concat(void 0===c?[]:i(c))}}},n}var m=y(function(n){return!0}),d=m,p=y(function(n){return"string"==typeof n}),g=y(function(n){return"number"==typeof n}),b=y(function(n){return"boolean"==typeof n}),A=y(function(n){return"bigint"==typeof n}),S=y(function(n){return"symbol"==typeof n}),w=y(function(n){return null==n}),j={__proto__:null,optional:f,array:a,intersection:s,union:l,not:h,when:y,select:v,any:m,_:d,string:p,number:g,boolean:b,bigint:A,symbol:S,nullish:w,instanceOf:function(n){return y(function(n){return function(t){return t instanceof n}}(n))},typed:function(){return{array:a,optional:f,intersection:s,union:l,not:h,select:v,when:y}}},K=function n(t,r){var u=function(){var n=r.find(function(n){return(0,n.test)(t)});if(!n){var e;try{e=JSON.stringify(t)}catch(n){e=t}throw new Error("Pattern matching error: no pattern matches value "+e)}return n.handler(n.select(t),t)};return{with:function(){var u=[].slice.call(arguments),i=u[u.length-1],c=[],f=[];3===u.length&&"function"==typeof u[1]?(c.push(u[0]),f.push(u[1])):c.push.apply(c,u.slice(0,u.length-1));var a={},s=function(n){return Boolean(c.some(function(t){return o(t,n,function(n,t){a[n]=t})})&&f.every(function(t){return t(n)}))};return n(t,r.concat([{test:s,handler:i,select:function(n){return Object.keys(a).length?e in a?a[e]:a:n}}]))},when:function(e,u){return n(t,r.concat([{test:e,handler:u,select:function(n){return n}}]))},otherwise:function(e){return n(t,r.concat([{test:function(){return!0},handler:e,select:function(n){return n}}])).run()},exhaustive:function(){return u()},run:u}};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 o(t,n,function(){})}}if(2===n.length){var e=n,r=e[0],u=e[1];return o(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 K(n,[])}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -109,2 +109,11 @@ import * as symbols from './internals/symbols'; | ||
/** | ||
* ### Catch All wildcard | ||
* `P._` is a wildcard pattern, matching **any value**. | ||
* It's an alias to `P.any`. | ||
* @example | ||
* match(value) | ||
* .with(P._, () => 'will always match') | ||
*/ | ||
export declare const _: GuardP<unknown, unknown>; | ||
/** | ||
* ### String wildcard | ||
@@ -111,0 +120,0 @@ * `P.string` is a wildcard pattern matching any **string**. |
@@ -66,3 +66,3 @@ import type * as symbols from '../internals/symbols'; | ||
* | ||
* Equivalent to `.with(__, () => x).run()` | ||
* Equivalent to `.with(P._, () => x).exhaustive()` | ||
**/ | ||
@@ -69,0 +69,0 @@ otherwise<c>(handler: (value: i) => PickReturnValue<o, c>): PickReturnValue<o, Union<inferredOutput, c>>; |
@@ -36,3 +36,3 @@ import type * as symbols from '../internals/symbols'; | ||
* Patterns can be any (nested) javascript value. | ||
* They can also be a "wildcards", like `__`. | ||
* They can also be a "wildcards", like `_`. | ||
*/ | ||
@@ -39,0 +39,0 @@ export declare type Pattern<a> = Matchable<a, unknown, any, any> | (a extends Primitives ? a : unknown extends a ? UnknownPattern : a extends readonly (infer i)[] ? a extends readonly [infer a1, infer a2, infer a3, infer a4, infer a5] ? readonly [ |
{ | ||
"name": "ts-pattern", | ||
"version": "4.0.1-rc.15", | ||
"version": "4.0.1-rc.16", | ||
"description": " The exhaustive Pattern Matching library for TypeScript.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -49,3 +49,3 @@ <h1 align="center">ts-pattern</h1> | ||
- **Exhaustiveness checking** support, enforcing that you are matching every possible case with [`.exhaustive()`](#exhaustive). | ||
- **Expressive API**, with catch-all and type specific **wildcards**: [`__`](#__-wildcard), [`P.string`](#pstring-wildcard), [`P.number`](#pnumber-wildcard), etc. | ||
- **Expressive API**, with catch-all and type specific **wildcards**: [`P._`](#P_-wildcard), [`P.string`](#pstring-wildcard), [`P.number`](#pnumber-wildcard), etc. | ||
- Supports [**predicates**](#Pwhen-patterns), [**unions**](#punion-patterns), [**intersections**](#pintersection-patterns) and [**exclusion**](#pnot-patterns) patterns for non-trivial cases. | ||
@@ -87,8 +87,6 @@ - Supports properties selection, via the [`P.select(name?)`](#pselect-patterns) function. | ||
β Full support | ||
- β Full support | ||
- β οΈ Partial support, All features except passing multiple patterns to `.with()`. | ||
- β Not supported | ||
β οΈ Partial support, everything works except passing more than 2 patterns to `.with()` | ||
β Not supported | ||
# Documentation | ||
@@ -177,3 +175,3 @@ | ||
```ts | ||
import { match, P, __ } from 'ts-pattern'; | ||
import { match, P } from 'ts-pattern'; | ||
@@ -210,3 +208,3 @@ const reducer = (state: State, event: Event): State => | ||
.with(__, () => state) | ||
.with(P._, () => state) | ||
@@ -350,19 +348,19 @@ .exhaustive(); | ||
### the `__` wildcard | ||
### the `P._` wildcard | ||
`__` will match any value. | ||
`P._` will match any value. | ||
You can use it at the top level, or inside your pattern. | ||
```ts | ||
.with(__, () => state) | ||
.with(P._, () => state) | ||
// You could also use it inside your pattern: | ||
.with([__, __], () => state) | ||
.with([P._, P._], () => state) | ||
// at any level: | ||
.with([__, { type: __ }], () => state) | ||
.with([P._, { type: P._ }], () => state) | ||
``` | ||
You can also use `P.any`, which is an alias to `__`. | ||
You can also use `P.any`, which is an alias to `P._`. | ||
@@ -379,3 +377,3 @@ ### .exhaustive(), .otherwise() and .run() | ||
Alternatively you can use `.otherwise()`, which takes an handler function returning a default value. `.otherwise(handler)` is equivalent to `.with(__, handler).exhaustive()`. | ||
Alternatively you can use `.otherwise()`, which takes an handler function returning a default value. `.otherwise(handler)` is equivalent to `.with(P._, handler).exhaustive()`. | ||
@@ -674,8 +672,8 @@ ```ts | ||
#### `__` wildcard | ||
#### `P._` wildcard | ||
The `__` pattern will match any value. | ||
The `P._` pattern will match any value. | ||
```ts | ||
import { match, __ } from 'ts-pattern'; | ||
import { match, P } from 'ts-pattern'; | ||
@@ -685,3 +683,3 @@ const input = 'hello'; | ||
const output = match(input) | ||
.with(__, () => 'It will always match') | ||
.with(P._, () => 'It will always match') | ||
.otherwise(() => 'This string will never be used'); | ||
@@ -693,3 +691,3 @@ | ||
You can also use `P.any` which is an alias to `__`. | ||
You can also use `P.any` which is an alias to `P._`. | ||
@@ -846,3 +844,3 @@ #### `P.string` wildcard | ||
```ts | ||
import { match, __ } from 'ts-pattern'; | ||
import { match, P } from 'ts-pattern'; | ||
@@ -858,6 +856,6 @@ type Input = | ||
const output = match(input) | ||
.with([__, '+', __], ([x, , y]) => x + y) | ||
.with([__, '-', __], ([x, , y]) => x - y) | ||
.with([__, '*', __], ([x, , y]) => x * y) | ||
.with(['-', __], ([, x]) => -x) | ||
.with([P._, '+', P._], ([x, , y]) => x + y) | ||
.with([P._, '-', P._], ([x, , y]) => x - y) | ||
.with([P._, '*', P._], ([x, , y]) => x * y) | ||
.with(['-', P._], ([, x]) => -x) | ||
.otherwise(() => NaN); | ||
@@ -1204,3 +1202,3 @@ | ||
match<Input, 'ok'>({ type: 'hello' }) | ||
.with(__, (value) => 'ok') // value: Input | ||
.with(P._, (value) => 'ok') // value: Input | ||
.with(P.string, (value) => 'ok') // value: string | ||
@@ -1219,3 +1217,3 @@ .with( | ||
.with(P.not(P.when(() => true)), (value) => 'ok') // value: Input | ||
.with({ type: __ }, (value) => 'ok') // value: { type: string } | ||
.with({ type: P._ }, (value) => 'ok') // value: { type: string } | ||
.with({ type: P.string }, (value) => 'ok') // value: { type: string } | ||
@@ -1222,0 +1220,0 @@ .with({ type: P.when(() => true) }, (value) => 'ok') // value: { type: string } |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
883
219151
1220