const p=Symbol.for("@ts-pattern/matcher"),W=Symbol.for("@ts-pattern/isVariadic"),x="@ts-pattern/anonymous-select-key",B=e=>!!(e&&typeof e=="object"),S=e=>e&&!!e[p],g=(e,t,r)=>{if(S(e)){const n=e[p](),{matched:i,selections:s}=n.match(t);return i&&s&&Object.keys(s).forEach(a=>r(a,s[a])),i}if(B(e)){if(!B(t))return!1;if(Array.isArray(e)){if(!Array.isArray(t))return!1;let n=[],i=[],s=[];for(const a of e.keys()){const l=e[a];S(l)&&l[W]?s.push(l):s.length?i.push(l):n.push(l)}if(s.length){if(s.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(t.lengthg(w,a[j],r))&&i.every((w,j)=>g(w,l[j],r))&&(s.length===0||g(s[0],K,r))}return e.length===t.length&&e.every((a,l)=>g(a,t[l],r))}return Reflect.ownKeys(e).every(n=>{const i=e[n];return(n in t||S(s=i)&&s[p]().matcherType==="optional")&&g(i,t[n],r);var s})}return Object.is(t,e)},b=e=>{var t,r,n;return B(e)?S(e)?(t=(r=(n=e[p]()).getSelectionKeys)==null?void 0:r.call(n))!=null?t:[]:Array.isArray(e)?O(e,b):O(Object.values(e),b):[]},O=(e,t)=>e.reduce((r,n)=>r.concat(t(n)),[]);function c(e){return Object.assign(e,{optional:()=>N(e),and:t=>u(e,t),or:t=>k(e,t),select:t=>t===void 0?T(e):T(t,e)})}function N(e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return t===void 0?(b(e).forEach(i=>n(i,void 0)),{matched:!0,selections:r}):{matched:g(e,t,n),selections:r}},getSelectionKeys:()=>b(e),matcherType:"optional"})})}function u(...e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return{matched:e.every(i=>g(i,t,n)),selections:r}},getSelectionKeys:()=>O(e,b),matcherType:"and"})})}function k(...e){return c({[p]:()=>({match:t=>{let r={};const n=(i,s)=>{r[i]=s};return O(e,b).forEach(i=>n(i,void 0)),{matched:e.some(i=>g(i,t,n)),selections:r}},getSelectionKeys:()=>O(e,b),matcherType:"or"})})}function o(e){return{[p]:()=>({match:t=>({matched:!!e(t)})})}}function T(...e){const t=typeof e[0]=="string"?e[0]:void 0,r=e.length===2?e[1]:typeof e[0]=="string"?void 0:e[0];return c({[p]:()=>({match:n=>{let i={[t??x]:n};return{matched:r===void 0||g(r,n,(s,a)=>{i[s]=a}),selections:i}},getSelectionKeys:()=>[t??x].concat(r===void 0?[]:b(r))})})}function h(e){return typeof e=="number"}function y(e){return typeof e=="string"}function m(e){return typeof e=="bigint"}c(o(function(e){return!0}));const v=e=>Object.assign(c(e),{startsWith:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.startsWith(r)))));var r},endsWith:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.endsWith(r)))));var r},minLength:t=>v(u(e,(r=>o(n=>y(n)&&n.length>=r))(t))),length:t=>v(u(e,(r=>o(n=>y(n)&&n.length===r))(t))),maxLength:t=>v(u(e,(r=>o(n=>y(n)&&n.length<=r))(t))),includes:t=>{return v(u(e,(r=t,o(n=>y(n)&&n.includes(r)))));var r},regex:t=>{return v(u(e,(r=t,o(n=>y(n)&&!!n.match(r)))));var r}}),L=v(o(y)),f=e=>Object.assign(c(e),{between:(t,r)=>f(u(e,((n,i)=>o(s=>h(s)&&n<=s&&i>=s))(t,r))),lt:t=>f(u(e,(r=>o(n=>h(n)&&nf(u(e,(r=>o(n=>h(n)&&n>r))(t))),lte:t=>f(u(e,(r=>o(n=>h(n)&&n<=r))(t))),gte:t=>f(u(e,(r=>o(n=>h(n)&&n>=r))(t))),int:()=>f(u(e,o(t=>h(t)&&Number.isInteger(t)))),finite:()=>f(u(e,o(t=>h(t)&&Number.isFinite(t)))),positive:()=>f(u(e,o(t=>h(t)&&t>0))),negative:()=>f(u(e,o(t=>h(t)&&t<0)))});f(o(h));const d=e=>Object.assign(c(e),{between:(t,r)=>d(u(e,((n,i)=>o(s=>m(s)&&n<=s&&i>=s))(t,r))),lt:t=>d(u(e,(r=>o(n=>m(n)&&nd(u(e,(r=>o(n=>m(n)&&n>r))(t))),lte:t=>d(u(e,(r=>o(n=>m(n)&&n<=r))(t))),gte:t=>d(u(e,(r=>o(n=>m(n)&&n>=r))(t))),positive:()=>d(u(e,o(t=>m(t)&&t>0))),negative:()=>d(u(e,o(t=>m(t)&&t<0)))});d(o(m));c(o(function(e){return typeof e=="boolean"}));c(o(function(e){return typeof e=="symbol"}));const P=c(o(function(e){return e==null}));c(o(function(e){return e!=null}));var z={__proto__:null,string:L,nullish:P};class _ extends Error{constructor(t){let r;try{r=JSON.stringify(t)}catch{r=t}super(`Pattern matching error: no pattern matches value ${r}`),this.input=void 0,this.input=t}}const E={matched:!1,value:void 0};function F(e){return new A(e,E)}class A{constructor(t,r){this.input=void 0,this.state=void 0,this.input=t,this.state=r}with(...t){if(this.state.matched)return this;const r=t[t.length-1],n=[t[0]];let i;t.length===3&&typeof t[1]=="function"?i=t[1]:t.length>2&&n.push(...t.slice(1,t.length-1));let s=!1,a={};const l=(w,j)=>{s=!0,a[w]=j},K=!n.some(w=>g(w,this.input,l))||i&&!i(this.input)?E:{matched:!0,value:r(s?x in a?a[x]:a:this.input,this.input)};return new A(this.input,K)}when(t,r){if(this.state.matched)return this;const n=!!t(this.input);return new A(this.input,n?{matched:!0,value:r(this.input,this.input)}:E)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(t=$){return this.state.matched?this.state.value:t(this.input)}run(){return this.exhaustive()}returnType(){return this}narrow(){return this}}function $(e){throw new _(e)}export{z as N,F as z};

AltStyle によって変換されたページ (->オリジナル) /