Build 545032stable
Module 889137
7,001 characters · in no known chunkDependencies
Source Raw
function(e,t,n){"use strict";n.d(t,{P:()=>K,YW:()=>$});let i=Symbol.for("@ts-pattern/matcher"),r=Symbol.for("@ts-pattern/isVariadic"),s="@ts-pattern/anonymous-select-key",a=e=>!!(e&&"object"==typeof e),o=e=>e&&!!e[i],l=(e,t,n)=>{if(o(e)){let{matched:r,selections:s}=e[i]().match(t);return r&&s&&Object.keys(s).forEach(e=>n(e,s[e])),r}if(a(e)){if(!a(t))return!1;if(Array.isArray(e)){if(!Array.isArray(t))return!1;let i=[],s=[],a=[];for(let t of e.keys()){let n=e[t];o(n)&&n[r]?a.push(n):a.length?s.push(n):i.push(n)}if(a.length){if(a.length>1)throw Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(t.length<i.length+s.length)return!1;let e=t.slice(0,i.length),r=0===s.length?[]:t.slice(-s.length),o=t.slice(i.length,0===s.length?1/0:-s.length);return i.every((t,i)=>l(t,e[i],n))&&s.every((e,t)=>l(e,r[t],n))&&(0===a.length||l(a[0],o,n))}return e.length===t.length&&e.every((e,i)=>l(e,t[i],n))}return Object.keys(e).every(r=>{let s=e[r];return(r in t||o(s)&&"optional"===s[i]().matcherType)&&l(s,t[r],n)})}return Object.is(t,e)},u=e=>{var t,n,r;return a(e)?o(e)?null!=(t=null==(n=(r=e[i]()).getSelectionKeys)?void 0:n.call(r))?t:[]:Array.isArray(e)?c(e,u):c(Object.values(e),u):[]},c=(e,t)=>e.reduce((e,n)=>e.concat(t(n)),[]);function d(e){return Object.assign(e,{optional:()=>_(e),and:t=>f(e,t),or:t=>h(e,t),select:t=>void 0===t?E(e):E(t,e)})}function _(e){return d({[i]:()=>({match:t=>{let n={},i=(e,t)=>{n[e]=t};return void 0===t?(u(e).forEach(e=>i(e,void 0)),{matched:!0,selections:n}):{matched:l(e,t,i),selections:n}},getSelectionKeys:()=>u(e),matcherType:"optional"})})}function f(...e){return d({[i]:()=>({match:t=>{let n={},i=(e,t)=>{n[e]=t};return{matched:e.every(e=>l(e,t,i)),selections:n}},getSelectionKeys:()=>c(e,u),matcherType:"and"})})}function h(...e){return d({[i]:()=>({match:t=>{let n={},i=(e,t)=>{n[e]=t};return c(e,u).forEach(e=>i(e,void 0)),{matched:e.some(e=>l(e,t,i)),selections:n}},getSelectionKeys:()=>c(e,u),matcherType:"or"})})}function p(e){return{[i]:()=>({match:t=>({matched:!!e(t)})})}}function E(...e){let t="string"==typeof e[0]?e[0]:void 0,n=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return d({[i]:()=>({match:e=>{let i={[null!=t?t:s]:e};return{matched:void 0===n||l(n,e,(e,t)=>{i[e]=t}),selections:i}},getSelectionKeys:()=>[null!=t?t:s].concat(void 0===n?[]:u(n))})})}function m(e){return"number"==typeof e}function g(e){return"string"==typeof e}function A(e){return"bigint"==typeof e}let I=d(p(function(e){return!0})),T=e=>Object.assign(d(e),{startsWith:t=>T(f(e,p(e=>g(e)&&e.startsWith(t)))),endsWith:t=>T(f(e,p(e=>g(e)&&e.endsWith(t)))),minLength:t=>T(f(e,p(e=>g(e)&&e.length>=t))),maxLength:t=>T(f(e,p(e=>g(e)&&e.length<=t))),includes:t=>T(f(e,p(e=>g(e)&&e.includes(t)))),regex:t=>T(f(e,p(e=>g(e)&&!!e.match(t))))}),S=T(p(g)),N=(e,t)=>p(n=>m(n)&&e<=n&&t>=n),y=e=>p(t=>m(t)&&t<e),C=e=>p(t=>m(t)&&t>e),v=e=>p(t=>m(t)&&t<=e),O=e=>p(t=>m(t)&&t>=e),R=()=>p(e=>m(e)&&Number.isInteger(e)),b=()=>p(e=>m(e)&&Number.isFinite(e)),D=()=>p(e=>m(e)&&e>0),L=()=>p(e=>m(e)&&e<0),w=e=>Object.assign(d(e),{between:(t,n)=>w(f(e,N(t,n))),lt:t=>w(f(e,y(t))),gt:t=>w(f(e,C(t))),lte:t=>w(f(e,v(t))),gte:t=>w(f(e,O(t))),int:()=>w(f(e,R())),finite:()=>w(f(e,b())),positive:()=>w(f(e,D())),negative:()=>w(f(e,L()))}),M=w(p(m)),P=(e,t)=>p(n=>A(n)&&e<=n&&t>=n),x=e=>p(t=>A(t)&&t<e),U=e=>p(t=>A(t)&&t>e),k=e=>p(t=>A(t)&&t<=e),G=e=>p(t=>A(t)&&t>=e),F=()=>p(e=>A(e)&&e>0),V=()=>p(e=>A(e)&&e<0),B=e=>Object.assign(d(e),{between:(t,n)=>B(f(e,P(t,n))),lt:t=>B(f(e,x(t))),gt:t=>B(f(e,U(t))),lte:t=>B(f(e,k(t))),gte:t=>B(f(e,G(t))),positive:()=>B(f(e,F())),negative:()=>B(f(e,V()))}),H=B(p(A)),j=d(p(function(e){return"boolean"==typeof e})),Y=d(p(function(e){return"symbol"==typeof e})),W=d(p(function(e){return null==e}));var K={__proto__:null,matcher:i,optional:_,array:function(...e){return function e(t){return Object.assign(Object.assign(t,{*[Symbol.iterator](){yield Object.assign(t,{[r]:!0})}}),{optional:()=>e(_(t)),select:n=>e(void 0===n?E(t):E(n,t))})}({[i]:()=>({match:t=>{if(!Array.isArray(t))return{matched:!1};if(0===e.length)return{matched:!0};let n=e[0],i={};if(0===t.length)return u(n).forEach(e=>{i[e]=[]}),{matched:!0,selections:i};let r=(e,t)=>{i[e]=(i[e]||[]).concat([t])};return{matched:t.every(e=>l(n,e,r)),selections:i}},getSelectionKeys:()=>0===e.length?[]:u(e[0])})})},set:function(...e){return d({[i]:()=>({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};let i=(e,t)=>{n[e]=(n[e]||[]).concat([t])},r=e[0];return{matched:((e,t)=>{for(let n of e)if(!t(n))return!1;return!0})(t,e=>l(r,e,i)),selections:n}},getSelectionKeys:()=>0===e.length?[]:u(e[0])})})},map:function(...e){return d({[i]:()=>({match:t=>{var n;if(!(t instanceof Map))return{matched:!1};let i={};if(0===t.size)return{matched:!0,selections:i};let r=(e,t)=>{i[e]=(i[e]||[]).concat([t])};if(0===e.length)return{matched:!0};if(1===e.length)throw Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(n=e[0])?void 0:n.toString()}`);let[s,a]=e;return{matched:((e,t)=>{for(let[n,i]of e.entries())if(!t(i,n))return!1;return!0})(t,(e,t)=>{let n=l(s,t,r),i=l(a,e,r);return n&&i}),selections:i}},getSelectionKeys:()=>0===e.length?[]:[...u(e[0]),...u(e[1])]})})},intersection:f,union:h,not:function(e){return d({[i]:()=>({match:t=>({matched:!l(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:p,select:E,any:I,_:I,string:S,between:N,lt:y,gt:C,lte:v,gte:O,int:R,finite:b,positive:D,negative:L,number:M,betweenBigInt:P,ltBigInt:x,gtBigInt:U,lteBigInt:k,gteBigInt:G,positiveBigInt:F,negativeBigInt:V,bigint:H,boolean:j,symbol:Y,nullish:W,instanceOf:function(e){return d(p(t=>t instanceof e))},shape:function(e){return d(p(function(...e){if(1===e.length){let[t]=e;return e=>l(t,e,()=>{})}if(2===e.length){let[t,n]=e;return l(t,n,()=>{})}throw Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${e.length}.`)}(e)))}};let z={matched:!1,value:void 0};function $(e){return new q(e,z)}class q{constructor(e,t){this.input=void 0,this.state=void 0,this.input=e,this.state=t}with(...e){let t;if(this.state.matched)return this;let n=e[e.length-1],i=[e[0]];3===e.length&&"function"==typeof e[1]?(i.push(e[0]),t=e[1]):e.length>2&&i.push(...e.slice(1,e.length-1));let r=!1,a={},o=(e,t)=>{r=!0,a[e]=t},u=i.some(e=>l(e,this.input,o))&&(!t||t(this.input))?{matched:!0,value:n(r?s in a?a[s]:a:this.input,this.input)}:z;return new q(this.input,u)}when(e,t){if(this.state.matched)return this;let n=!!e(this.input);return new q(this.input,n?{matched:!0,value:t(this.input,this.input)}:z)}otherwise(e){return this.state.matched?this.state.value:e(this.input)}exhaustive(){return this.run()}run(){let e;if(this.state.matched)return this.state.value;try{e=JSON.stringify(this.input)}catch(t){e=this.input}throw Error(`Pattern matching error: no pattern matches value ${e}`)}returnType(){return this}}}