Socket
Socket
Sign inDemoInstall

ts-pattern

Package Overview
Dependencies
Maintainers
1
Versions
151
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-pattern - npm Package Compare versions

Comparing version 5.0.0-rc.4 to 5.0.0-rc.5

2

dist/index.js

@@ -1,2 +0,2 @@

function n(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 t(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var u=0;return function(){return u>=t.length?{done:!0}:{done:!1,value:t[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 e=Symbol("@ts-pattern/matcher"),r=Symbol("@ts-pattern/isVariadic"),u="@ts-pattern/anonymous-select-key",i=function(n){return Boolean(n&&"object"==typeof n)},o=function(n){return n&&!!n[e]},c=function n(u,c,a){if(o(u)){var f=u[e]().match(c),s=f.matched,l=f.selections;return s&&l&&Object.keys(l).forEach(function(n){return a(n,l[n])}),s}if(i(u)){if(!i(c))return!1;if(Array.isArray(u)){if(!Array.isArray(c))return!1;for(var h,v=[],g=[],m=[],p=t(u.keys());!(h=p()).done;){var y=u[h.value];o(y)&&y[r]?m.push(y):m.length?g.push(y):v.push(y)}if(m.length){if(m.length>1)throw new Error("Pattern error: Using `...P.array(...)` several time in a single pattern is not allowed.");if(c.length<v.length+g.length)return!1;var d=c.slice(0,v.length),b=0===g.length?[]:c.slice(-g.length),w=c.slice(v.length,0===g.length?Infinity:-g.length);return v.every(function(t,e){return n(t,d[e],a)})&&g.every(function(t,e){return n(t,b[e],a)})&&(0===m.length||n(m[0],w,a))}return u.length===c.length&&u.every(function(t,e){return n(t,c[e],a)})}return Object.keys(u).every(function(t){var r,i=u[t];return(t in c||o(r=i)&&"optional"===r[e]().matcherType)&&n(i,c[t],a)})}return Object.is(c,u)},a=function n(t){var r,u,c;return i(t)?o(t)?null!=(r=null==(u=(c=t[e]()).getSelectionKeys)?void 0:u.call(c))?r:[]:Array.isArray(t)?f(t,n):f(Object.values(t),n):[]},f=function(n,t){return n.reduce(function(n,e){return n.concat(t(e))},[])};function s(){var n=[].slice.call(arguments);if(1===n.length){var t=n[0];return function(n){return c(t,n,function(){})}}if(2===n.length)return c(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 l=function(n){return Object.assign(n,{optional:function(){return h(n)},and:function(t){return p(n,t)},or:function(t){return y(n,t)},select:function(t){return void 0===t?w(n):w(t,n)}})};function h(n){var t;return l(((t={})[e]=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:c(n,t,r),selections:e}},getSelectionKeys:function(){return a(n)},matcherType:"optional"}},t))}function v(){var n,t=[].slice.call(arguments);return l(((n={})[e]=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 c(e,n,u)}),selections:r}},getSelectionKeys:function(){return 0===t.length?[]:a(t[0])}}},n[Symbol.iterator]=function(){/*#__PURE__*/return regeneratorRuntime.mark(function n(){var e;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,Object.assign(0===t.length?v():v(t[0]),((e={})[r]=!0,e));case 2:case"end":return n.stop()}},n)})()},n))}var g=function(n,e){for(var r,u=t(n);!(r=u()).done;)if(!e(r.value))return!1;return!0},m=function(n,e){for(var r,u=t(n.entries());!(r=u()).done;){var i=r.value;if(!e(i[1],i[0]))return!1}return!0};function p(){var n,t=[].slice.call(arguments);return l(((n={})[e]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return{matched:t.every(function(t){return c(t,n,r)}),selections:e}},getSelectionKeys:function(){return f(t,a)},matcherType:"and"}},n))}function y(){var n,t=[].slice.call(arguments);return l(((n={})[e]=function(){return{match:function(n){var e={},r=function(n,t){e[n]=t};return f(t,a).forEach(function(n){return r(n,void 0)}),{matched:t.some(function(t){return c(t,n,r)}),selections:e}},getSelectionKeys:function(){return f(t,a)},matcherType:"or"}},n))}function d(n){var t;return l(((t={})[e]=function(){return{match:function(t){return{matched:!c(n,t,function(){})}},getSelectionKeys:function(){return[]},matcherType:"not"}},t))}function b(n){var t;return l(((t={})[e]=function(){return{match:function(t){return{matched:Boolean(n(t))}}}},t))}function w(){var n,t=[].slice.call(arguments),r="string"==typeof t[0]?t[0]:void 0,i=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return l(((n={})[e]=function(){return{match:function(n){var t,e=((t={})[null!=r?r:u]=n,t);return{matched:void 0===i||c(i,n,function(n,t){e[n]=t}),selections:e}},getSelectionKeys:function(){return[null!=r?r:u].concat(void 0===i?[]:a(i))}}},n))}function S(n){return"number"==typeof n}function j(n){return"string"==typeof n}function A(n){return"bigint"==typeof n}var O=l(b(function(n){return!0})),B=O,I=function n(t){return Object.assign(t,{startsWith:function(e){return n(p(t,(r=e,b(function(n){return j(n)&&n.startsWith(r)}))));var r},endsWith:function(e){return n(p(t,(r=e,b(function(n){return j(n)&&n.endsWith(r)}))));var r},minLength:function(e){return n(p(t,function(n){return b(function(t){return j(t)&&t.length>=n})}(e)))},maxLength:function(e){return n(p(t,function(n){return b(function(t){return j(t)&&t.length<=n})}(e)))},includes:function(e){return n(p(t,(r=e,b(function(n){return j(n)&&n.includes(r)}))));var r},regex:function(e){return n(p(t,(r=e,b(function(n){return j(n)&&Boolean(n.match(r))}))));var r}})}(b(j)),E=function(n,t){return b(function(e){return S(e)&&n<=e&&t>=e})},x=function(n){return b(function(t){return S(t)&&t<n})},K=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})},P=function(){return b(function(n){return S(n)&&Number.isInteger(n)})},_=function(){return b(function(n){return S(n)&&Number.isFinite(n)})},W=function(){return b(function(n){return S(n)&&n>0})},M=function(){return b(function(n){return S(n)&&n<0})},N=function n(t){return Object.assign(t,{between:function(e,r){return n(p(t,E(e,r)))},lt:function(e){return n(p(t,x(e)))},gt:function(e){return n(p(t,K(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,P()))},finite:function(){return n(p(t,_()))},positive:function(){return n(p(t,W()))},negative:function(){return n(p(t,M()))}})}(b(S)),z=function(n,t){return b(function(e){return A(e)&&n<=e&&t>=e})},L=function(n){return b(function(t){return A(t)&&t<n})},R=function(n){return b(function(t){return A(t)&&t>n})},U=function(n){return b(function(t){return A(t)&&t<=n})},C=function(n){return b(function(t){return A(t)&&t>=n})},F=function(){return b(function(n){return A(n)&&n>0})},J=function(){return b(function(n){return A(n)&&n<0})},V=function n(t){return Object.assign(t,{between:function(e,r){return n(p(t,z(e,r)))},lt:function(e){return n(p(t,L(e)))},gt:function(e){return n(p(t,R(e)))},lte:function(e){return n(p(t,U(e)))},gte:function(e){return n(p(t,C(e)))},positive:function(){return n(p(t,F()))},negative:function(){return n(p(t,J()))}})}(b(A)),$=l(b(function(n){return"boolean"==typeof n})),q=l(b(function(n){return"symbol"==typeof n})),D=l(b(function(n){return null==n})),G={__proto__:null,matcher:e,optional:h,array:v,set:function(){var n,t=[].slice.call(arguments);return l(((n={})[e]=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:g(n,function(n){return c(u,n,r)}),selections:e}},getSelectionKeys:function(){return 0===t.length?[]:a(t[0])}}},n))},map:function(){var n,t=[].slice.call(arguments);return l(((n={})[e]=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=c(i,t,u),r=c(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:y,not:d,when:b,select:w,any:O,_:B,string:I,between:E,lt:x,gt:K,lte:k,gte:T,int:P,finite:_,positive:W,negative:M,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 l(b(function(n){return function(t){return t instanceof n}}(n)))},shape:function(n){return l(b(s(n)))},typed:function(){return{array:v,optional:h,intersection:p,union:y,not:d,select:w,when:b}}};function H(n){return new X(n)}var Q={matched:!1},X=/*#__PURE__*/function(){function n(n,t){void 0===t&&(t=Q),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],i=[e[0]],o=[];3===e.length&&"function"==typeof e[1]?(i.push(e[0]),o.push(e[1])):e.length>2&&i.push.apply(i,e.slice(1,e.length-1));var a={},f=Boolean(i.some(function(n){return c(n,t.input,function(n,t){a[n]=t})})&&o.every(function(n){return n(t.input)}))?{matched:!0,value:r(Object.keys(a).length?u in a?a[u]:a:this.input,this.input)}:{matched:!1};return new n(this.input,f)},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)}:{matched:!1})},t.otherwise=function(n){return this.state.matched?this.state.value:n(this.input,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}();export{G as P,G as Pattern,s 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 time 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 h=t=>Object.assign(t,{optional:()=>u(t),and:e=>m(t,e),or:e=>y(t,e),select:e=>void 0===e?v(t):v(e,t)});function u(e){return h({[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"})})}function l(...n){return h({[t]:()=>({match:t=>{if(!Array.isArray(t))return{matched:!1};if(0===n.length)return{matched:!0};const e=n[0];let r={};if(0===t.length)return o(e).forEach(t=>{r[t]=[]}),{matched:!0,selections:r};const i=(t,e)=>{r[t]=(r[t]||[]).concat([e])};return{matched:t.every(t=>s(e,t,i)),selections:r}},getSelectionKeys:()=>0===n.length?[]:o(n[0])}),*[Symbol.iterator](){yield Object.assign(0===n.length?l():l(n[0]),{[e]:!0})}})}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 h({[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 h({[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 h({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})}function d(e){return h({[t]:()=>({match:t=>({matched:Boolean(e(t))})})})}function v(...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 h({[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 b(t){return"number"==typeof t}function w(t){return"string"==typeof t}function S(t){return"bigint"==typeof t}const B=h(d(function(t){return!0})),O=B,j=t=>Object.assign(t,{startsWith:e=>{return j(m(t,(n=e,d(t=>w(t)&&t.startsWith(n)))));var n},endsWith:e=>{return j(m(t,(n=e,d(t=>w(t)&&t.endsWith(n)))));var n},minLength:e=>j(m(t,(t=>d(e=>w(e)&&e.length>=t))(e))),maxLength:e=>j(m(t,(t=>d(e=>w(e)&&e.length<=t))(e))),includes:e=>{return j(m(t,(n=e,d(t=>w(t)&&t.includes(n)))));var n},regex:e=>{return j(m(t,(n=e,d(t=>w(t)&&Boolean(t.match(n))))));var n}}),E=j(d(w)),I=(t,e)=>d(n=>b(n)&&t<=n&&e>=n),K=t=>d(e=>b(e)&&e<t),A=t=>d(e=>b(e)&&e>t),k=t=>d(e=>b(e)&&e<=t),x=t=>d(e=>b(e)&&e>=t),P=()=>d(t=>b(t)&&Number.isInteger(t)),T=()=>d(t=>b(t)&&Number.isFinite(t)),_=()=>d(t=>b(t)&&t>0),W=()=>d(t=>b(t)&&t<0),N=t=>Object.assign(t,{between:(e,n)=>N(m(t,I(e,n))),lt:e=>N(m(t,K(e))),gt:e=>N(m(t,A(e))),lte:e=>N(m(t,k(e))),gte:e=>N(m(t,x(e))),int:()=>N(m(t,P())),finite:()=>N(m(t,T())),positive:()=>N(m(t,_())),negative:()=>N(m(t,W()))}),$=N(d(b)),z=(t,e)=>d(n=>S(n)&&t<=n&&e>=n),L=t=>d(e=>S(e)&&e<t),M=t=>d(e=>S(e)&&e>t),F=t=>d(e=>S(e)&&e<=t),J=t=>d(e=>S(e)&&e>=t),U=()=>d(t=>S(t)&&t>0),V=()=>d(t=>S(t)&&t<0),q=t=>Object.assign(t,{between:(e,n)=>q(m(t,z(e,n))),lt:e=>q(m(t,L(e))),gt:e=>q(m(t,M(e))),lte:e=>q(m(t,F(e))),gte:e=>q(m(t,J(e))),positive:()=>q(m(t,U())),negative:()=>q(m(t,V()))}),C=q(d(S)),D=h(d(function(t){return"boolean"==typeof t})),G=h(d(function(t){return"symbol"==typeof t})),H=h(d(function(t){return null==t}));var Q={__proto__:null,matcher:t,optional:u,array:l,set:function(...e){return h({[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 h({[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:p,when:d,select:v,any:B,_:O,string:E,between:I,lt:K,gt:A,lte:k,gte:x,int:P,finite:T,positive:_,negative:W,number:$,betweenBigInt:z,ltBigInt:L,gtBigInt:M,lteBigInt:F,gteBigInt:J,positiveBigInt:U,negativeBigInt:V,bigint:C,boolean:D,symbol:G,nullish:H,instanceOf:function(t){return h(d(function(t){return e=>e instanceof t}(t)))},shape:function(t){return h(d(a(t)))},typed:function(){return{array:l,optional:u,intersection:m,union:y,not:p,select:v,when:d}}};function R(t){return new Y(t)}const X={matched:!1};class Y{constructor(t,e=X){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)}:{matched:!1};return new Y(this.input,c)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new Y(this.input,n?{matched:!0,value:e(this.input,this.input)}:{matched:!1})}otherwise(t){return this.state.matched?this.state.value:t(this.input,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{Q as P,Q as Pattern,a as isMatching,R as match};
//# sourceMappingURL=index.js.map
{
"name": "ts-pattern",
"version": "5.0.0-rc.4",
"version": "5.0.0-rc.5",
"description": " The exhaustive Pattern Matching library for TypeScript.",

@@ -27,3 +27,3 @@ "type": "module",

"scripts": {
"build": "rimraf dist && microbundle && sh ./scripts/generate-cts.sh",
"build": "rimraf dist && microbundle --format modern,cjs,umd && sh ./scripts/generate-cts.sh",
"dev": "microbundle watch",

@@ -30,0 +30,0 @@ "prepublishOnly": "npm run test && npm run build",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc