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