ts-pattern
Advanced tools
Comparing version 5.0.0-rc.7 to 5.0.0-rc.8
@@ -1,2 +0,2 @@ | ||
const t=Symbol("@ts-pattern/matcher"),e=Symbol("@ts-pattern/isVariadic"),n="@ts-pattern/anonymous-select-key",r=t=>Boolean(t&&"object"==typeof t),i=e=>e&&!!e[t],s=(n,o,c)=>{if(i(n)){const e=n[t](),{matched:r,selections:i}=e.match(o);return r&&i&&Object.keys(i).forEach(t=>c(t,i[t])),r}if(r(n)){if(!r(o))return!1;if(Array.isArray(n)){if(!Array.isArray(o))return!1;let t=[],r=[],a=[];for(const s of n.keys()){const o=n[s];i(o)&&o[e]?a.push(o):a.length?r.push(o):t.push(o)}if(a.length){if(a.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(o.length<t.length+r.length)return!1;const e=o.slice(0,t.length),n=0===r.length?[]:o.slice(-r.length),i=o.slice(t.length,0===r.length?Infinity:-r.length);return t.every((t,n)=>s(t,e[n],c))&&r.every((t,e)=>s(t,n[e],c))&&(0===a.length||s(a[0],i,c))}return n.length===o.length&&n.every((t,e)=>s(t,o[e],c))}return Object.keys(n).every(e=>{const r=n[e];return(e in o||i(a=r)&&"optional"===a[t]().matcherType)&&s(r,o[e],c);var a})}return Object.is(o,n)},o=e=>{var n,s,a;return r(e)?i(e)?null!=(n=null==(s=(a=e[t]()).getSelectionKeys)?void 0:s.call(a))?n:[]:Array.isArray(e)?c(e,o):c(Object.values(e),o):[]},c=(t,e)=>t.reduce((t,n)=>t.concat(e(n)),[]);function a(...t){if(1===t.length){const[e]=t;return t=>s(e,t,()=>{})}if(2===t.length){const[e,n]=t;return s(e,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}const u=t=>Object.assign(t,{optional:()=>h(t),and:e=>f(t,e),or:e=>m(t,e),select:e=>void 0===e?p(t):p(e,t)});function h(e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return void 0===t?(o(e).forEach(t=>r(t,void 0)),{matched:!0,selections:n}):{matched:s(e,t,r),selections:n}},getSelectionKeys:()=>o(e),matcherType:"optional"})})}const l=(t,e)=>{for(const n of t)if(!e(n))return!1;return!0},g=(t,e)=>{for(const[n,r]of t.entries())if(!e(r,n))return!1;return!0};function f(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return{matched:e.every(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"and"})})}function m(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return c(e,o).forEach(t=>r(t,void 0)),{matched:e.some(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"or"})})}function y(e){return u({[t]:()=>({match:t=>({matched:Boolean(e(t))})})})}function p(...e){const r="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return u({[t]:()=>({match:t=>{let e={[null!=r?r:n]:t};return{matched:void 0===i||s(i,t,(t,n)=>{e[t]=n}),selections:e}},getSelectionKeys:()=>[null!=r?r:n].concat(void 0===i?[]:o(i))})})}function d(t){return"number"==typeof t}function v(t){return"string"==typeof t}function b(t){return"bigint"==typeof t}const w=u(y(function(t){return!0})),S=w,B=t=>Object.assign(t,{startsWith:e=>{return B(f(t,(n=e,y(t=>v(t)&&t.startsWith(n)))));var n},endsWith:e=>{return B(f(t,(n=e,y(t=>v(t)&&t.endsWith(n)))));var n},minLength:e=>B(f(t,(t=>y(e=>v(e)&&e.length>=t))(e))),maxLength:e=>B(f(t,(t=>y(e=>v(e)&&e.length<=t))(e))),includes:e=>{return B(f(t,(n=e,y(t=>v(t)&&t.includes(n)))));var n},regex:e=>{return B(f(t,(n=e,y(t=>v(t)&&Boolean(t.match(n))))));var n}}),O=B(y(v)),j=(t,e)=>y(n=>d(n)&&t<=n&&e>=n),E=t=>y(e=>d(e)&&e<t),I=t=>y(e=>d(e)&&e>t),K=t=>y(e=>d(e)&&e<=t),A=t=>y(e=>d(e)&&e>=t),k=()=>y(t=>d(t)&&Number.isInteger(t)),x=()=>y(t=>d(t)&&Number.isFinite(t)),P=()=>y(t=>d(t)&&t>0),T=()=>y(t=>d(t)&&t<0),_=t=>Object.assign(t,{between:(e,n)=>_(f(t,j(e,n))),lt:e=>_(f(t,E(e))),gt:e=>_(f(t,I(e))),lte:e=>_(f(t,K(e))),gte:e=>_(f(t,A(e))),int:()=>_(f(t,k())),finite:()=>_(f(t,x())),positive:()=>_(f(t,P())),negative:()=>_(f(t,T()))}),W=_(y(d)),N=(t,e)=>y(n=>b(n)&&t<=n&&e>=n),$=t=>y(e=>b(e)&&e<t),z=t=>y(e=>b(e)&&e>t),L=t=>y(e=>b(e)&&e<=t),M=t=>y(e=>b(e)&&e>=t),F=()=>y(t=>b(t)&&t>0),J=()=>y(t=>b(t)&&t<0),U=t=>Object.assign(t,{between:(e,n)=>U(f(t,N(e,n))),lt:e=>U(f(t,$(e))),gt:e=>U(f(t,z(e))),lte:e=>U(f(t,L(e))),gte:e=>U(f(t,M(e))),positive:()=>U(f(t,F())),negative:()=>U(f(t,J()))}),V=U(y(b)),q=u(y(function(t){return"boolean"==typeof t})),C=u(y(function(t){return"symbol"==typeof t})),D=u(y(function(t){return null==t}));var G={__proto__:null,matcher:t,optional:h,array:function n(...r){return u({[t]:()=>({match:t=>{if(!Array.isArray(t))return{matched:!1};if(0===r.length)return{matched:!0};const e=r[0];let n={};if(0===t.length)return o(e).forEach(t=>{n[t]=[]}),{matched:!0,selections:n};const i=(t,e)=>{n[t]=(n[t]||[]).concat([e])};return{matched:t.every(t=>s(e,t,i)),selections:n}},getSelectionKeys:()=>0===r.length?[]:o(r[0])}),*[Symbol.iterator](){yield Object.assign(0===r.length?n():n(r[0]),{[e]:!0})}})},set:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Set))return{matched:!1};let n={};if(0===t.size)return{matched:!0,selections:n};if(0===e.length)return{matched:!0};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e])},i=e[0];return{matched:l(t,t=>s(i,t,r)),selections:n}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},map:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Map))return{matched:!1};let n={};if(0===t.size)return{matched:!0,selections:n};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e])};if(0===e.length)return{matched:!0};var i;if(1===e.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(i=e[0])?void 0:i.toString()}`);const[o,c]=e;return{matched:g(t,(t,e)=>{const n=s(o,e,r),i=s(c,t,r);return n&&i}),selections:n}},getSelectionKeys:()=>0===e.length?[]:[...o(e[0]),...o(e[1])]})})},intersection:f,union:m,not:function(e){return u({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:y,select:p,any:w,_:S,string:O,between:j,lt:E,gt:I,lte:K,gte:A,int:k,finite:x,positive:P,negative:T,number:W,betweenBigInt:N,ltBigInt:$,gtBigInt:z,lteBigInt:L,gteBigInt:M,positiveBigInt:F,negativeBigInt:J,bigint:V,boolean:q,symbol:C,nullish:D,instanceOf:function(t){return u(y(function(t){return e=>e instanceof t}(t)))},shape:function(t){return u(y(a(t)))}};function H(t){return new R(t)}const Q={matched:!1,value:void 0};class R{constructor(t,e=Q){this.input=void 0,this.state=void 0,this.input=t,this.state=e}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]],i=[];3===t.length&&"function"==typeof t[1]?(r.push(t[0]),i.push(t[1])):t.length>2&&r.push(...t.slice(1,t.length-1));let o={};const c=Boolean(r.some(t=>s(t,this.input,(t,e)=>{o[t]=e}))&&i.every(t=>t(this.input)))?{matched:!0,value:e(Object.keys(o).length?n in o?o[n]:o:this.input,this.input)}:Q;return new R(this.input,c)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new R(this.input,n?{matched:!0,value:e(this.input,this.input)}:Q)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(){return this.run()}run(){if(this.state.matched)return this.state.value;let t;try{t=JSON.stringify(this.input)}catch(e){t=this.input}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}returnType(){return this}}export{G as P,G as Pattern,a as isMatching,H as match}; | ||
const t=Symbol("@ts-pattern/matcher"),e=Symbol("@ts-pattern/isVariadic"),n="@ts-pattern/anonymous-select-key",r=t=>Boolean(t&&"object"==typeof t),i=e=>e&&!!e[t],s=(n,o,c)=>{if(i(n)){const e=n[t](),{matched:r,selections:i}=e.match(o);return r&&i&&Object.keys(i).forEach(t=>c(t,i[t])),r}if(r(n)){if(!r(o))return!1;if(Array.isArray(n)){if(!Array.isArray(o))return!1;let t=[],r=[],a=[];for(const s of n.keys()){const o=n[s];i(o)&&o[e]?a.push(o):a.length?r.push(o):t.push(o)}if(a.length){if(a.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(o.length<t.length+r.length)return!1;const e=o.slice(0,t.length),n=0===r.length?[]:o.slice(-r.length),i=o.slice(t.length,0===r.length?Infinity:-r.length);return t.every((t,n)=>s(t,e[n],c))&&r.every((t,e)=>s(t,n[e],c))&&(0===a.length||s(a[0],i,c))}return n.length===o.length&&n.every((t,e)=>s(t,o[e],c))}return Object.keys(n).every(e=>{const r=n[e];return(e in o||i(a=r)&&"optional"===a[t]().matcherType)&&s(r,o[e],c);var a})}return Object.is(o,n)},o=e=>{var n,s,a;return r(e)?i(e)?null!=(n=null==(s=(a=e[t]()).getSelectionKeys)?void 0:s.call(a))?n:[]:Array.isArray(e)?c(e,o):c(Object.values(e),o):[]},c=(t,e)=>t.reduce((t,n)=>t.concat(e(n)),[]);function a(...t){if(1===t.length){const[e]=t;return t=>s(e,t,()=>{})}if(2===t.length){const[e,n]=t;return s(e,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function u(t){return Object.assign(t,{optional:()=>l(t),and:e=>m(t,e),or:e=>y(t,e),select:e=>void 0===e?d(t):d(e,t)})}function h(t){return Object.assign((t=>Object.assign(t,{*[Symbol.iterator](){yield Object.assign(t,{[e]:!0})}}))(t),{optional:()=>h(l(t)),select:e=>h(void 0===e?d(t):d(e,t))})}function l(e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return void 0===t?(o(e).forEach(t=>r(t,void 0)),{matched:!0,selections:n}):{matched:s(e,t,r),selections:n}},getSelectionKeys:()=>o(e),matcherType:"optional"})})}const g=(t,e)=>{for(const n of t)if(!e(n))return!1;return!0},f=(t,e)=>{for(const[n,r]of t.entries())if(!e(r,n))return!1;return!0};function m(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return{matched:e.every(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"and"})})}function y(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e};return c(e,o).forEach(t=>r(t,void 0)),{matched:e.some(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"or"})})}function p(e){return{[t]:()=>({match:t=>({matched:Boolean(e(t))})})}}function d(...e){const r="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return u({[t]:()=>({match:t=>{let e={[null!=r?r:n]:t};return{matched:void 0===i||s(i,t,(t,n)=>{e[t]=n}),selections:e}},getSelectionKeys:()=>[null!=r?r:n].concat(void 0===i?[]:o(i))})})}function v(t){return"number"==typeof t}function b(t){return"string"==typeof t}function w(t){return"bigint"==typeof t}const S=u(p(function(t){return!0})),O=S,j=t=>Object.assign(u(t),{startsWith:e=>{return j(m(t,(n=e,p(t=>b(t)&&t.startsWith(n)))));var n},endsWith:e=>{return j(m(t,(n=e,p(t=>b(t)&&t.endsWith(n)))));var n},minLength:e=>j(m(t,(t=>p(e=>b(e)&&e.length>=t))(e))),maxLength:e=>j(m(t,(t=>p(e=>b(e)&&e.length<=t))(e))),includes:e=>{return j(m(t,(n=e,p(t=>b(t)&&t.includes(n)))));var n},regex:e=>{return j(m(t,(n=e,p(t=>b(t)&&Boolean(t.match(n))))));var n}}),B=j(p(b)),E=(t,e)=>p(n=>v(n)&&t<=n&&e>=n),I=t=>p(e=>v(e)&&e<t),K=t=>p(e=>v(e)&&e>t),A=t=>p(e=>v(e)&&e<=t),k=t=>p(e=>v(e)&&e>=t),x=()=>p(t=>v(t)&&Number.isInteger(t)),P=()=>p(t=>v(t)&&Number.isFinite(t)),T=()=>p(t=>v(t)&&t>0),_=()=>p(t=>v(t)&&t<0),W=t=>Object.assign(u(t),{between:(e,n)=>W(m(t,E(e,n))),lt:e=>W(m(t,I(e))),gt:e=>W(m(t,K(e))),lte:e=>W(m(t,A(e))),gte:e=>W(m(t,k(e))),int:()=>W(m(t,x())),finite:()=>W(m(t,P())),positive:()=>W(m(t,T())),negative:()=>W(m(t,_()))}),N=W(p(v)),$=(t,e)=>p(n=>w(n)&&t<=n&&e>=n),z=t=>p(e=>w(e)&&e<t),L=t=>p(e=>w(e)&&e>t),M=t=>p(e=>w(e)&&e<=t),F=t=>p(e=>w(e)&&e>=t),J=()=>p(t=>w(t)&&t>0),U=()=>p(t=>w(t)&&t<0),V=t=>Object.assign(u(t),{between:(e,n)=>V(m(t,$(e,n))),lt:e=>V(m(t,z(e))),gt:e=>V(m(t,L(e))),lte:e=>V(m(t,M(e))),gte:e=>V(m(t,F(e))),positive:()=>V(m(t,J())),negative:()=>V(m(t,U()))}),q=V(p(w)),C=u(p(function(t){return"boolean"==typeof t})),D=u(p(function(t){return"symbol"==typeof t})),G=u(p(function(t){return null==t}));var H={__proto__:null,matcher:t,optional:l,array:function(...e){return h({[t]:()=>({match:t=>{if(!Array.isArray(t))return{matched:!1};if(0===e.length)return{matched:!0};const n=e[0];let r={};if(0===t.length)return o(n).forEach(t=>{r[t]=[]}),{matched:!0,selections:r};const i=(t,e)=>{r[t]=(r[t]||[]).concat([e])};return{matched:t.every(t=>s(n,t,i)),selections:r}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},set:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Set))return{matched:!1};let n={};if(0===t.size)return{matched:!0,selections:n};if(0===e.length)return{matched:!0};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e])},i=e[0];return{matched:g(t,t=>s(i,t,r)),selections:n}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},map:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Map))return{matched:!1};let n={};if(0===t.size)return{matched:!0,selections:n};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e])};if(0===e.length)return{matched:!0};var i;if(1===e.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(i=e[0])?void 0:i.toString()}`);const[o,c]=e;return{matched:f(t,(t,e)=>{const n=s(o,e,r),i=s(c,t,r);return n&&i}),selections:n}},getSelectionKeys:()=>0===e.length?[]:[...o(e[0]),...o(e[1])]})})},intersection:m,union:y,not:function(e){return u({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:p,select:d,any:S,_:O,string:B,between:E,lt:I,gt:K,lte:A,gte:k,int:x,finite:P,positive:T,negative:_,number:N,betweenBigInt:$,ltBigInt:z,gtBigInt:L,lteBigInt:M,gteBigInt:F,positiveBigInt:J,negativeBigInt:U,bigint:q,boolean:C,symbol:D,nullish:G,instanceOf:function(t){return u(p(function(t){return e=>e instanceof t}(t)))},shape:function(t){return u(p(a(t)))}};function Q(t){return new X(t)}const R={matched:!1,value:void 0};class X{constructor(t,e=R){this.input=void 0,this.state=void 0,this.input=t,this.state=e}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]],i=[];3===t.length&&"function"==typeof t[1]?(r.push(t[0]),i.push(t[1])):t.length>2&&r.push(...t.slice(1,t.length-1));let o={};const c=Boolean(r.some(t=>s(t,this.input,(t,e)=>{o[t]=e}))&&i.every(t=>t(this.input)))?{matched:!0,value:e(Object.keys(o).length?n in o?o[n]:o:this.input,this.input)}:R;return new X(this.input,c)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new X(this.input,n?{matched:!0,value:e(this.input,this.input)}:R)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(){return this.run()}run(){if(this.state.matched)return this.state.value;let t;try{t=JSON.stringify(this.input)}catch(e){t=this.input}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}returnType(){return this}}export{H as P,H as Pattern,a as isMatching,Q as match}; | ||
//# sourceMappingURL=index.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){function t(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new 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=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)?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.")}var r=Symbol("@ts-pattern/matcher"),u=Symbol("@ts-pattern/isVariadic"),i="@ts-pattern/anonymous-select-key",o=function(n){return Boolean(n&&"object"==typeof n)},c=function(n){return n&&!!n[r]},f=function n(t,i,f){if(c(t)){var a=t[r]().match(i),s=a.matched,l=a.selections;return s&&l&&Object.keys(l).forEach(function(n){return f(n,l[n])}),s}if(o(t)){if(!o(i))return!1;if(Array.isArray(t)){if(!Array.isArray(i))return!1;for(var h,g=[],v=[],m=[],p=e(t.keys());!(h=p()).done;){var d=t[h.value];c(d)&&d[u]?m.push(d):m.length?v.push(d):g.push(d)}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(i.length<g.length+v.length)return!1;var y=i.slice(0,g.length),b=0===v.length?[]:i.slice(-v.length),w=i.slice(g.length,0===v.length?Infinity:-v.length);return g.every(function(t,e){return n(t,y[e],f)})&&v.every(function(t,e){return n(t,b[e],f)})&&(0===m.length||n(m[0],w,f))}return t.length===i.length&&t.every(function(t,e){return n(t,i[e],f)})}return Object.keys(t).every(function(e){var u,o=t[e];return(e in i||c(u=o)&&"optional"===u[r]().matcherType)&&n(o,i[e],f)})}return Object.is(i,t)},a=function n(t){var e,u,i;return o(t)?c(t)?null!=(e=null==(u=(i=t[r]()).getSelectionKeys)?void 0:u.call(i))?e:[]:Array.isArray(t)?s(t,n):s(Object.values(t),n):[]},s=function(n,t){return n.reduce(function(n,e){return n.concat(t(e))},[])};function l(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return f(t,n,function(){})}}if(2===n.length)return f(n[0],n[1],function(){});throw new Error("isMatching wasn't given the right number of arguments: expected 1 or 2, received "+n.length+".")}var h=function(n){return Object.assign(n,{optional:function(){return g(n)},and:function(t){return p(n,t)},or:function(t){return d(n,t)},select:function(t){return void 0===t?b(n):b(t,n)}})};function g(n){var t;return h(((t={})[r]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(a(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:f(n,t,r),selections:e}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}var v=function(n,t){for(var r,u=e(n);!(r=u()).done;)if(!t(r.value))return!1;return!0},m=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 p(){var n,t=[].slice.call(arguments);return h(((n={})[r]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return{matched:t.every(function(t){return f(t,n,r)}),selections:e}},getSelectionKeys:function(){return s(t,a)},matcherType:"and"}},n))}function d(){var n,t=[].slice.call(arguments);return h(((n={})[r]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return s(t,a).forEach(function(n){return r(n,void 0)}),{matched:t.some(function(t){return f(t,n,r)}),selections:e}},getSelectionKeys:function(){return s(t,a)},matcherType:"or"}},n))}function y(n){var t;return h(((t={})[r]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},t))}function b(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,u=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return h(((n={})[r]=function(){return{match:function(n){var t,r=((t={})[null!=e?e:i]=n,t);return{matched:void 0===u||f(u,n,function(n,t){r[n]=t}),selections:r}},getSelectionKeys:function(){return[null!=e?e:i].concat(void 0===u?[]:a(u))}}},n))}function w(n){return"number"==typeof n}function S(n){return"string"==typeof n}function j(n){return"bigint"==typeof n}var A=h(y(function(n){return!0})),O=A,B=function n(t){return Object.assign(t,{startsWith:function(e){return n(p(t,(r=e,y(function(n){return S(n)&&n.startsWith(r)}))));var r},endsWith:function(e){return n(p(t,(r=e,y(function(n){return S(n)&&n.endsWith(r)}))));var r},minLength:function(e){return n(p(t,function(n){return y(function(t){return S(t)&&t.length>=n})}(e)))},maxLength:function(e){return n(p(t,function(n){return y(function(t){return S(t)&&t.length<=n})}(e)))},includes:function(e){return n(p(t,(r=e,y(function(n){return S(n)&&n.includes(r)}))));var r},regex:function(e){return n(p(t,(r=e,y(function(n){return S(n)&&Boolean(n.match(r))}))));var r}})}(y(S)),I=function(n,t){return y(function(e){return w(e)&&n<=e&&t>=e})},x=function(n){return y(function(t){return w(t)&&t<n})},E=function(n){return y(function(t){return w(t)&&t>n})},K=function(n){return y(function(t){return w(t)&&t<=n})},T=function(n){return y(function(t){return w(t)&&t>=n})},k=function(){return y(function(n){return w(n)&&Number.isInteger(n)})},P=function(){return y(function(n){return w(n)&&Number.isFinite(n)})},_=function(){return y(function(n){return w(n)&&n>0})},M=function(){return y(function(n){return w(n)&&n<0})},W=function n(t){return Object.assign(t,{between:function(e,r){return n(p(t,I(e,r)))},lt:function(e){return n(p(t,x(e)))},gt:function(e){return n(p(t,E(e)))},lte:function(e){return n(p(t,K(e)))},gte:function(e){return n(p(t,T(e)))},int:function(){return n(p(t,k()))},finite:function(){return n(p(t,P()))},positive:function(){return n(p(t,_()))},negative:function(){return n(p(t,M()))}})}(y(w)),N=function(n,t){return y(function(e){return j(e)&&n<=e&&t>=e})},z=function(n){return y(function(t){return j(t)&&t<n})},L=function(n){return y(function(t){return j(t)&&t>n})},R=function(n){return y(function(t){return j(t)&&t<=n})},U=function(n){return y(function(t){return j(t)&&t>=n})},C=function(){return y(function(n){return j(n)&&n>0})},F=function(){return y(function(n){return j(n)&&n<0})},J=function n(t){return Object.assign(t,{between:function(e,r){return n(p(t,N(e,r)))},lt:function(e){return n(p(t,z(e)))},gt:function(e){return n(p(t,L(e)))},lte:function(e){return n(p(t,R(e)))},gte:function(e){return n(p(t,U(e)))},positive:function(){return n(p(t,C()))},negative:function(){return n(p(t,F()))}})}(y(j)),V=h(y(function(n){return"boolean"==typeof n})),$=h(y(function(n){return"symbol"==typeof n})),q=h(y(function(n){return null==n})),D={__proto__:null,matcher:r,optional:g,array:function n(){var t,e=[].slice.call(arguments);return h(((t={})[r]=function(){return{match:function(n){if(!Array.isArray(n))return{matched:!1};if(0===e.length)return{matched:!0};var t=e[0],r={};if(0===n.length)return a(t).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 f(t,n,u)}),selections:r}},getSelectionKeys:function(){return 0===e.length?[]:a(e[0])}}},t[Symbol.iterator]=function(){/*#__PURE__*/return regeneratorRuntime.mark(function t(){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Object.assign(0===e.length?n():n(e[0]),((r={})[u]=!0,r));case 2:case"end":return t.stop()}},t)})()},t))},set:function(){var n,t=[].slice.call(arguments);return h(((n={})[r]=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:v(n,function(n){return f(u,n,r)}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:a(t[0])}}},n))},map:function(){var n,t=[].slice.call(arguments);return h(((n={})[r]=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:m(n,function(n,t){var e=f(i,t,u),r=f(o,n,u);return e&&r}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:[].concat(a(t[0]),a(t[1]))}}},n))},intersection:p,union:d,not:function(n){var t;return h(((t={})[r]=function(){return{match:function(t){return{matched:!f(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},t))},when:y,select:b,any:A,_:O,string:B,between:I,lt:x,gt:E,lte:K,gte:T,int:k,finite:P,positive:_,negative:M,number:W,betweenBigInt:N,ltBigInt:z,gtBigInt:L,lteBigInt:R,gteBigInt:U,positiveBigInt:C,negativeBigInt:F,bigint:J,boolean:V,symbol:$,nullish:q,instanceOf:function(n){return h(y(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return h(y(l(n)))}},G={matched:!1,value:void 0},H=/*#__PURE__*/function(){function n(n,t){void 0===t&&(t=G),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]],o=[];3===e.length&&"function"==typeof e[1]?(u.push(e[0]),o.push(e[1])):e.length>2&&u.push.apply(u,e.slice(1,e.length-1));var c={},a=Boolean(u.some(function(n){return f(n,t.input,function(n,t){c[n]=t})})&&o.every(function(n){return n(t.input)}))?{matched:!0,value:r(Object.keys(c).length?i in c?c[i]:c:this.input,this.input)}:G;return new n(this.input,a)},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)}:G)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(){return this.run()},t.run=function(){if(this.state.matched)return this.state.value;var n;try{n=JSON.stringify(this.input)}catch(t){n=this.input}throw new Error("Pattern matching error: no pattern matches value "+n)},t.returnType=function(){return this},n}();n.P=D,n.Pattern=D,n.isMatching=l,n.match=function(n){return new H(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){function t(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new 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=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)?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.")}var r=Symbol("@ts-pattern/matcher"),u=Symbol("@ts-pattern/isVariadic"),i="@ts-pattern/anonymous-select-key",o=function(n){return Boolean(n&&"object"==typeof n)},c=function(n){return n&&!!n[r]},f=function n(t,i,f){if(c(t)){var a=t[r]().match(i),s=a.matched,l=a.selections;return s&&l&&Object.keys(l).forEach(function(n){return f(n,l[n])}),s}if(o(t)){if(!o(i))return!1;if(Array.isArray(t)){if(!Array.isArray(i))return!1;for(var h,g=[],v=[],m=[],p=e(t.keys());!(h=p()).done;){var d=t[h.value];c(d)&&d[u]?m.push(d):m.length?v.push(d):g.push(d)}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(i.length<g.length+v.length)return!1;var y=i.slice(0,g.length),b=0===v.length?[]:i.slice(-v.length),w=i.slice(g.length,0===v.length?Infinity:-v.length);return g.every(function(t,e){return n(t,y[e],f)})&&v.every(function(t,e){return n(t,b[e],f)})&&(0===m.length||n(m[0],w,f))}return t.length===i.length&&t.every(function(t,e){return n(t,i[e],f)})}return Object.keys(t).every(function(e){var u,o=t[e];return(e in i||c(u=o)&&"optional"===u[r]().matcherType)&&n(o,i[e],f)})}return Object.is(i,t)},a=function n(t){var e,u,i;return o(t)?c(t)?null!=(e=null==(u=(i=t[r]()).getSelectionKeys)?void 0:u.call(i))?e:[]:Array.isArray(t)?s(t,n):s(Object.values(t),n):[]},s=function(n,t){return n.reduce(function(n,e){return n.concat(t(e))},[])};function l(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return f(t,n,function(){})}}if(2===n.length)return f(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 h(n){return Object.assign(n,{optional:function(){return v(n)},and:function(t){return d(n,t)},or:function(t){return y(n,t)},select:function(t){return void 0===t?w(n):w(t,n)}})}function g(n){return Object.assign(function(n){var t;return Object.assign(n,((t={})[Symbol.iterator]=function(){/*#__PURE__*/return regeneratorRuntime.mark(function t(){var e;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Object.assign(n,((e={})[u]=!0,e));case 2:case"end":return t.stop()}},t)})()},t))}(n),{optional:function(){return g(v(n))},select:function(t){return g(void 0===t?w(n):w(t,n))}})}function v(n){var t;return h(((t={})[r]=function(){return{match:function(t){var e={},r=function(n,t){e[n]=t};return void 0===t?(a(n).forEach(function(n){return r(n,void 0)}),{matched:!0,selections:e}):{matched:f(n,t,r),selections:e}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}var m=function(n,t){for(var r,u=e(n);!(r=u()).done;)if(!t(r.value))return!1;return!0},p=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 d(){var n,t=[].slice.call(arguments);return h(((n={})[r]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return{matched:t.every(function(t){return f(t,n,r)}),selections:e}},getSelectionKeys:function(){return s(t,a)},matcherType:"and"}},n))}function y(){var n,t=[].slice.call(arguments);return h(((n={})[r]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return s(t,a).forEach(function(n){return r(n,void 0)}),{matched:t.some(function(t){return f(t,n,r)}),selections:e}},getSelectionKeys:function(){return s(t,a)},matcherType:"or"}},n))}function b(n){var t;return(t={})[r]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},t}function w(){var n,t=[].slice.call(arguments),e="string"==typeof t[0]?t[0]:void 0,u=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return h(((n={})[r]=function(){return{match:function(n){var t,r=((t={})[null!=e?e:i]=n,t);return{matched:void 0===u||f(u,n,function(n,t){r[n]=t}),selections:r}},getSelectionKeys:function(){return[null!=e?e:i].concat(void 0===u?[]:a(u))}}},n))}function S(n){return"number"==typeof n}function j(n){return"string"==typeof n}function O(n){return"bigint"==typeof n}var A=h(b(function(n){return!0})),B=A,I=function n(t){return Object.assign(h(t),{startsWith:function(e){return n(d(t,(r=e,b(function(n){return j(n)&&n.startsWith(r)}))));var r},endsWith:function(e){return n(d(t,(r=e,b(function(n){return j(n)&&n.endsWith(r)}))));var r},minLength:function(e){return n(d(t,function(n){return b(function(t){return j(t)&&t.length>=n})}(e)))},maxLength:function(e){return n(d(t,function(n){return b(function(t){return j(t)&&t.length<=n})}(e)))},includes:function(e){return n(d(t,(r=e,b(function(n){return j(n)&&n.includes(r)}))));var r},regex:function(e){return n(d(t,(r=e,b(function(n){return j(n)&&Boolean(n.match(r))}))));var r}})}(b(j)),x=function(n,t){return b(function(e){return S(e)&&n<=e&&t>=e})},E=function(n){return b(function(t){return S(t)&&t<n})},K=function(n){return b(function(t){return S(t)&&t>n})},T=function(n){return b(function(t){return S(t)&&t<=n})},k=function(n){return b(function(t){return S(t)&&t>=n})},P=function(){return b(function(n){return S(n)&&Number.isInteger(n)})},_=function(){return b(function(n){return S(n)&&Number.isFinite(n)})},M=function(){return b(function(n){return S(n)&&n>0})},W=function(){return b(function(n){return S(n)&&n<0})},N=function n(t){return Object.assign(h(t),{between:function(e,r){return n(d(t,x(e,r)))},lt:function(e){return n(d(t,E(e)))},gt:function(e){return n(d(t,K(e)))},lte:function(e){return n(d(t,T(e)))},gte:function(e){return n(d(t,k(e)))},int:function(){return n(d(t,P()))},finite:function(){return n(d(t,_()))},positive:function(){return n(d(t,M()))},negative:function(){return n(d(t,W()))}})}(b(S)),z=function(n,t){return b(function(e){return O(e)&&n<=e&&t>=e})},L=function(n){return b(function(t){return O(t)&&t<n})},R=function(n){return b(function(t){return O(t)&&t>n})},U=function(n){return b(function(t){return O(t)&&t<=n})},C=function(n){return b(function(t){return O(t)&&t>=n})},F=function(){return b(function(n){return O(n)&&n>0})},J=function(){return b(function(n){return O(n)&&n<0})},V=function n(t){return Object.assign(h(t),{between:function(e,r){return n(d(t,z(e,r)))},lt:function(e){return n(d(t,L(e)))},gt:function(e){return n(d(t,R(e)))},lte:function(e){return n(d(t,U(e)))},gte:function(e){return n(d(t,C(e)))},positive:function(){return n(d(t,F()))},negative:function(){return n(d(t,J()))}})}(b(O)),$=h(b(function(n){return"boolean"==typeof n})),q=h(b(function(n){return"symbol"==typeof n})),D=h(b(function(n){return null==n})),G={__proto__:null,matcher:r,optional:v,array:function(){var n,t=[].slice.call(arguments);return g(((n={})[r]=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 a(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 f(e,n,u)}),selections:r}},getSelectionKeys:function(){return 0===t.length?[]:a(t[0])}}},n))},set:function(){var n,t=[].slice.call(arguments);return h(((n={})[r]=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:m(n,function(n){return f(u,n,r)}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:a(t[0])}}},n))},map:function(){var n,t=[].slice.call(arguments);return h(((n={})[r]=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:p(n,function(n,t){var e=f(i,t,u),r=f(o,n,u);return e&&r}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:[].concat(a(t[0]),a(t[1]))}}},n))},intersection:d,union:y,not:function(n){var t;return h(((t={})[r]=function(){return{match:function(t){return{matched:!f(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},t))},when:b,select:w,any:A,_:B,string:I,between:x,lt:E,gt:K,lte:T,gte:k,int:P,finite:_,positive:M,negative:W,number:N,betweenBigInt:z,ltBigInt:L,gtBigInt:R,lteBigInt:U,gteBigInt:C,positiveBigInt:F,negativeBigInt:J,bigint:V,boolean:$,symbol:q,nullish:D,instanceOf:function(n){return h(b(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return h(b(l(n)))}},H={matched:!1,value:void 0},Q=/*#__PURE__*/function(){function n(n,t){void 0===t&&(t=H),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]],o=[];3===e.length&&"function"==typeof e[1]?(u.push(e[0]),o.push(e[1])):e.length>2&&u.push.apply(u,e.slice(1,e.length-1));var c={},a=Boolean(u.some(function(n){return f(n,t.input,function(n,t){c[n]=t})})&&o.every(function(n){return n(t.input)}))?{matched:!0,value:r(Object.keys(c).length?i in c?c[i]:c:this.input,this.input)}:H;return new n(this.input,a)},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)}:H)},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input)},t.exhaustive=function(){return this.run()},t.run=function(){if(this.state.matched)return this.state.value;var n;try{n=JSON.stringify(this.input)}catch(t){n=this.input}throw new Error("Pattern matching error: no pattern matches value "+n)},t.returnType=function(){return this},n}();n.P=G,n.Pattern=G,n.isMatching=l,n.match=function(n){return new Q(n)}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -5,7 +5,5 @@ import { Match } from './types/Match.js'; | ||
* `match` creates a **pattern matching expression**. | ||
* * Use `.with(pattern, handler)` to pattern match on the input. | ||
* * Use `.exhaustive()` or `.otherwise(() => defaultValue)` to end the expression and get the result. | ||
* | ||
* Use `.with(pattern, handler)` to pattern match on the input. | ||
* | ||
* Use `.exhaustive()` or `.otherwise(() => defaultValue)` to end the expression and get the result. | ||
* | ||
* [Read the documentation for `match` on GitHub](https://github.com/gvergnaud/ts-pattern#match) | ||
@@ -12,0 +10,0 @@ * |
@@ -54,4 +54,4 @@ import * as symbols from './internals/symbols.js'; | ||
/** | ||
* `P.optional(subpattern)` takes a sub pattern and returns a pattern which matches if the | ||
* key is undefined or if it is defined and the sub pattern matches its value. | ||
* `.optional()` returns a pattern which matches if the | ||
* key is undefined or if it is defined and the previous pattern matches its value. | ||
* | ||
@@ -62,8 +62,8 @@ * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#Poptional-patterns) | ||
* match(value) | ||
* .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }') | ||
* .with({ greeting: P.string.optional() }, () => 'will match { greeting?: string}') | ||
*/ | ||
optional<input>(): Chainable<OptionalP<input, p>, omitted | 'optional'>; | ||
/** | ||
* `P.intersection(...patterns)` returns a pattern which matches | ||
* only if **every** patterns provided in parameter match the input. | ||
* `pattern.and(pattern)` returns a pattern that matches | ||
* if the previous pattern and the next one match the input. | ||
* | ||
@@ -75,10 +75,4 @@ * [Read the documentation for `P.intersection` on GitHub](https://github.com/gvergnaud/ts-pattern#Pintersection-patterns) | ||
* .with( | ||
* { | ||
* user: P.intersection( | ||
* { firstname: P.string }, | ||
* { lastname: P.string }, | ||
* { age: P.when(age => age > 21) } | ||
* ) | ||
* }, | ||
* ({ user }) => 'will match { firstname: string, lastname: string, age: number } if age > 21' | ||
* P.string.and(P.when(isUsername)), | ||
* (username) => '...' | ||
* ) | ||
@@ -88,4 +82,4 @@ */ | ||
/** | ||
* `P.union(...patterns)` returns a pattern which matches | ||
* if **at least one** of the patterns provided in parameter match the input. | ||
* `pattern.or(pattern)` returns a pattern that matches | ||
* if **either** the previous pattern or the next one match the input. | ||
* | ||
@@ -97,4 +91,4 @@ * [Read the documentation for `P.union` on GitHub](https://github.com/gvergnaud/ts-pattern#Punion-patterns) | ||
* .with( | ||
* { type: P.union('a', 'b', 'c') }, | ||
* ({ user }) => 'will match { type: "a" | "b" | "c" }' | ||
* { value: P.string.or(P.number) }, | ||
* ({ value }) => 'value: number | string' | ||
* ) | ||
@@ -104,4 +98,3 @@ */ | ||
/** | ||
* `P.select()` is a pattern which will always match, | ||
* and will inject the selected piece of input in the handler function. | ||
* `P.select()` will inject this property into the handler function's arguments. | ||
* | ||
@@ -112,4 +105,3 @@ * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#Pselect-patterns) | ||
* match<{ age: number }>(value) | ||
* .with({ age: P.select() }, (age) => 'age: number' | ||
* ) | ||
* .with({ age: P.string.select() }, (age) => 'age: number') | ||
*/ | ||
@@ -119,2 +111,27 @@ select<input>(): Chainable<SelectP<symbols.anonymousSelectKey, input, p>, omitted | 'select' | 'or' | 'and'>; | ||
}, omitted>; | ||
type Variadic<p> = p & Iterable<p>; | ||
type ArrayChainable<p, omitted extends string = never> = Variadic<p> & Omit<{ | ||
/** | ||
* `.optional()` returns a pattern which matches if the | ||
* key is undefined or if it is defined and the previous pattern matches its value. | ||
* | ||
* [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#Poptional-patterns) | ||
* | ||
* @example | ||
* match(value) | ||
* .with({ greeting: P.string.optional() }, () => 'will match { greeting?: string}') | ||
*/ | ||
optional<input>(): ArrayChainable<OptionalP<input, p>, omitted | 'optional'>; | ||
/** | ||
* `P.select()` will inject this property into the handler function's arguments. | ||
* | ||
* [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#Pselect-patterns) | ||
* | ||
* @example | ||
* match<{ age: number }>(value) | ||
* .with({ age: P.string.select() }, (age) => 'age: number') | ||
*/ | ||
select<input>(): ArrayChainable<SelectP<symbols.anonymousSelectKey, input, p>, omitted | 'select'>; | ||
select<input, k extends string>(key: k): Chainable<SelectP<k, input, p>, omitted | 'select'>; | ||
}, omitted>; | ||
/** | ||
@@ -146,4 +163,4 @@ * `P.optional(subpattern)` takes a sub pattern and returns a pattern which matches if the | ||
*/ | ||
export declare function array<input>(): Chainable<ArrayP<input, unknown>> & Iterable<ArrayP<input, unknown>>; | ||
export declare function array<input, const p extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: p): Chainable<ArrayP<input, p> & Iterable<ArrayP<input, p>>>; | ||
export declare function array<input>(): ArrayChainable<ArrayP<input, unknown>>; | ||
export declare function array<input, const p extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: p): ArrayChainable<ArrayP<input, p>>; | ||
/** | ||
@@ -203,3 +220,3 @@ * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches | ||
* { type: P.union('a', 'b', 'c') }, | ||
* ({ user }) => 'will match { type: "a" | "b" | "c" }' | ||
* ({ type }) => 'will match { type: "a" | "b" | "c" }' | ||
* ) | ||
@@ -206,0 +223,0 @@ */ |
@@ -20,4 +20,4 @@ import type * as symbols from '../internals/symbols.js'; | ||
/** | ||
* `.with(pattern, handler)` Registers a pattern and an handler function which | ||
* will be called if this pattern matches the input value. | ||
* `.with(pattern, handler)` Registers a pattern and an handler function that | ||
* will be called if the pattern matches the input value. | ||
* | ||
@@ -76,3 +76,3 @@ * [Read the documentation for `.with()` on GitHub](https://github.com/gvergnaud/ts-pattern#with) | ||
* `.when(predicate, handler)` Registers a predicate function and an handler function. | ||
* If the predicate returns true, the handler function will be chosen to handle the input. | ||
* If the predicate returns true, the handler function will be called. | ||
* | ||
@@ -86,4 +86,4 @@ * [Read the documentation for `.when()` on GitHub](https://github.com/gvergnaud/ts-pattern#when) | ||
/** | ||
* `.otherwise()` takes a function returning the **default value**, and | ||
* will be used to handle the input value if no previous pattern matched. | ||
* `.otherwise()` takes a **default handler function** that will be | ||
* called if no previous pattern matched your input. | ||
* | ||
@@ -97,7 +97,7 @@ * Equivalent to `.with(P._, () => x).exhaustive()` | ||
/** | ||
* `.exhaustive()` runs the pattern matching expression and return the result value. | ||
* `.exhaustive()` checks that all cases are handled, and return the result value. | ||
* | ||
* If this is of type `NonExhaustiveError`, it means you aren't matching | ||
* every case, and you should add another `.with(...)` clause | ||
* to prevent potential runtime errors. | ||
* If you get a `NonExhaustiveError`, it means that you aren't handling | ||
* all cases. You should probably add another `.with(...)` clause | ||
* to match the missing case and prevent runtime errors. | ||
* | ||
@@ -109,7 +109,9 @@ * [Read the documentation for `.exhaustive()` on GitHub](https://github.com/gvergnaud/ts-pattern#exhaustive) | ||
/** | ||
* `.run()` runs the pattern matching expression and return the result value. | ||
* `.run()` return the resulting value. | ||
* | ||
* ⚠️ calling this function is unsafe, and may throw if no pattern matches your input. | ||
* */ | ||
run(): PickReturnValue<o, inferredOutput>; | ||
/** | ||
* `.returnType<T>()` Lets you specific a return type for all your code branches. | ||
* `.returnType<T>()` Lets you specific the return type of all of your branches. | ||
* | ||
@@ -116,0 +118,0 @@ * [Read the documentation for `.returnType()` on GitHub](https://github.com/gvergnaud/ts-pattern#returnType) |
{ | ||
"name": "ts-pattern", | ||
"version": "5.0.0-rc.7", | ||
"version": "5.0.0-rc.8", | ||
"description": " The exhaustive Pattern Matching library for TypeScript.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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
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
464056
1609