tuple-result
Advanced tools
@@ -1,1 +0,1 @@ | ||
| "use strict";var p=(r,t,n)=>new Promise((w,f)=>{var l=e=>{try{s(n.next(e))}catch(i){f(i)}},h=e=>{try{s(n.throw(e))}catch(i){f(i)}},s=e=>e.done?w(e.value):Promise.resolve(e.value).then(l,h);s((n=n.apply(r,t)).next())});class a extends Array{constructor(t){super(3),this[0]=!0,this[1]=void 0,this[2]=t}get value(){return this[2]}get error(){return this[1]}isOk(){return!0}isErr(){return!1}unwrap(){return this[2]}toArray(){return[!0,void 0,this[2]]}}class c extends Array{constructor(t){super(3),this[0]=!1,this[1]=t,this[2]=void 0}get value(){return this[2]}get error(){return this[1]}isOk(){return!1}isErr(){return!0}unwrap(){const t=this[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}toArray(){return[!1,this[1],void 0]}}function u(r){return new a(r)}const E=u;function o(r){return new c(r)}const y=o;function k(r){return r[0]}function O(r){return!r[0]}function v(r){if(r[0])return r[2];const t=r[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}function d(r){if(r[0])return r[2];throw new Error("Expected an Ok result")}function A(r){if(!r[0])return r[1];throw new Error("Expected an Err result")}function m(r,t){return r[0]?r[2]:t}function g(r){return r[0]?r[2]:null}function x(r,t){return r[0]?u(t(r[2])):o(r[1])}function R(r,t){return r[0]?u(r[2]):o(t(r[1]))}function N(r){return r[0]?[!0,void 0,r[2]]:[!1,r[1],void 0]}function P(r){return r[0]?new a(r[2]):new c(r[1])}function T(r,...t){try{const n=r(...t);return u(n)}catch(n){return o(n)}}function U(r){return p(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return o(t)}})}exports.Err=o,exports.ErrResult=c,exports.Ok=u,exports.OkResult=a,exports.err=y,exports.fromArray=P,exports.isErr=O,exports.isOk=k,exports.mapErr=R,exports.mapOk=x,exports.ok=E,exports.t=T,exports.tAsync=U,exports.toArray=N,exports.unwrap=v,exports.unwrapErr=A,exports.unwrapOk=d,exports.unwrapOr=m,exports.unwrapOrNull=g; | ||
| "use strict";var p=(r,t,n)=>new Promise((w,f)=>{var l=e=>{try{s(n.next(e))}catch(i){f(i)}},h=e=>{try{s(n.throw(e))}catch(i){f(i)}},s=e=>e.done?w(e.value):Promise.resolve(e.value).then(l,h);s((n=n.apply(r,t)).next())});class a extends Array{constructor(t){super(3),this[0]=!0,this[1]=void 0,this[2]=t}get value(){return this[2]}get error(){return this[1]}isOk(){return!0}isErr(){return!1}unwrap(){return this[2]}toArray(){return[!0,void 0,this[2]]}}class c extends Array{constructor(t){super(3),this[0]=!1,this[1]=t,this[2]=void 0}get value(){return this[2]}get error(){return this[1]}isOk(){return!1}isErr(){return!0}unwrap(){const t=this[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}toArray(){return[!1,this[1],void 0]}}function u(r){return new a(r)}const E=u;function o(r){return new c(r)}const k=o;function y(r){return r[0]}function O(r){return!r[0]}function v(r){if(r[0])return r[2];const t=r[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}function d(r){if(r[0])return r[2];throw new Error("Expected an Ok result")}function m(r){if(!r[0])return r[1];throw new Error("Expected an Err result")}function A(r,t){return r[0]?r[2]:t}function g(r){return r[0]?r[2]:null}function x(r,t){return r[0]?u(t(r[2])):o(r[1])}function R(r,t){return r[0]?u(r[2]):o(t(r[1]))}function N(r){return r[0]?[!0,void 0,r[2]]:[!1,r[1],void 0]}function P(r){return r[0]?new a(r[2]):new c(r[1])}function U(r,...t){try{const n=r(...t);return u(n)}catch(n){return o(n)}}function T(r){return p(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return o(t)}})}function b(r,t){return r[0]?t.ok(r[2]):t.err(r[1])}exports.Err=o,exports.ErrResult=c,exports.Ok=u,exports.OkResult=a,exports.err=k,exports.fromArray=P,exports.isErr=O,exports.isOk=y,exports.mapErr=R,exports.mapOk=x,exports.match=b,exports.ok=E,exports.t=U,exports.tAsync=T,exports.toArray=N,exports.unwrap=v,exports.unwrapErr=m,exports.unwrapOk=d,exports.unwrapOr=A,exports.unwrapOrNull=g; |
@@ -1,1 +0,1 @@ | ||
| var p=(r,t,n)=>new Promise((h,f)=>{var l=e=>{try{s(n.next(e))}catch(i){f(i)}},w=e=>{try{s(n.throw(e))}catch(i){f(i)}},s=e=>e.done?h(e.value):Promise.resolve(e.value).then(l,w);s((n=n.apply(r,t)).next())});class c extends Array{constructor(t){super(3),this[0]=!0,this[1]=void 0,this[2]=t}get value(){return this[2]}get error(){return this[1]}isOk(){return!0}isErr(){return!1}unwrap(){return this[2]}toArray(){return[!0,void 0,this[2]]}}class a extends Array{constructor(t){super(3),this[0]=!1,this[1]=t,this[2]=void 0}get value(){return this[2]}get error(){return this[1]}isOk(){return!1}isErr(){return!0}unwrap(){const t=this[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}toArray(){return[!1,this[1],void 0]}}function u(r){return new c(r)}const E=u;function o(r){return new a(r)}const y=o;function v(r){return r[0]}function d(r){return!r[0]}function k(r){if(r[0])return r[2];const t=r[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}function O(r){if(r[0])return r[2];throw new Error("Expected an Ok result")}function x(r){if(!r[0])return r[1];throw new Error("Expected an Err result")}function A(r,t){return r[0]?r[2]:t}function g(r){return r[0]?r[2]:null}function m(r,t){return r[0]?u(t(r[2])):o(r[1])}function P(r,t){return r[0]?u(r[2]):o(t(r[1]))}function R(r){return r[0]?[!0,void 0,r[2]]:[!1,r[1],void 0]}function T(r){return r[0]?new c(r[2]):new a(r[1])}function U(r,...t){try{const n=r(...t);return u(n)}catch(n){return o(n)}}function N(r){return p(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return o(t)}})}export{o as Err,a as ErrResult,u as Ok,c as OkResult,y as err,T as fromArray,d as isErr,v as isOk,P as mapErr,m as mapOk,E as ok,U as t,N as tAsync,R as toArray,k as unwrap,x as unwrapErr,O as unwrapOk,A as unwrapOr,g as unwrapOrNull}; | ||
| var p=(r,t,n)=>new Promise((h,f)=>{var l=e=>{try{s(n.next(e))}catch(i){f(i)}},w=e=>{try{s(n.throw(e))}catch(i){f(i)}},s=e=>e.done?h(e.value):Promise.resolve(e.value).then(l,w);s((n=n.apply(r,t)).next())});class c extends Array{constructor(t){super(3),this[0]=!0,this[1]=void 0,this[2]=t}get value(){return this[2]}get error(){return this[1]}isOk(){return!0}isErr(){return!1}unwrap(){return this[2]}toArray(){return[!0,void 0,this[2]]}}class a extends Array{constructor(t){super(3),this[0]=!1,this[1]=t,this[2]=void 0}get value(){return this[2]}get error(){return this[1]}isOk(){return!1}isErr(){return!0}unwrap(){const t=this[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}toArray(){return[!1,this[1],void 0]}}function u(r){return new c(r)}const E=u;function o(r){return new a(r)}const y=o;function v(r){return r[0]}function d(r){return!r[0]}function k(r){if(r[0])return r[2];const t=r[1];throw t instanceof Error?t:typeof t=="string"?new Error(t):new Error("Unknown error")}function O(r){if(r[0])return r[2];throw new Error("Expected an Ok result")}function x(r){if(!r[0])return r[1];throw new Error("Expected an Err result")}function A(r,t){return r[0]?r[2]:t}function g(r){return r[0]?r[2]:null}function m(r,t){return r[0]?u(t(r[2])):o(r[1])}function P(r,t){return r[0]?u(r[2]):o(t(r[1]))}function R(r){return r[0]?[!0,void 0,r[2]]:[!1,r[1],void 0]}function U(r){return r[0]?new c(r[2]):new a(r[1])}function N(r,...t){try{const n=r(...t);return u(n)}catch(n){return o(n)}}function T(r){return p(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return o(t)}})}function b(r,t){return r[0]?t.ok(r[2]):t.err(r[1])}export{o as Err,a as ErrResult,u as Ok,c as OkResult,y as err,U as fromArray,d as isErr,v as isOk,P as mapErr,m as mapOk,b as match,E as ok,N as t,T as tAsync,R as toArray,k as unwrap,x as unwrapErr,O as unwrapOk,A as unwrapOr,g as unwrapOrNull}; |
@@ -191,3 +191,3 @@ /** | ||
| /** | ||
| * Wraps a Promise in a result. | ||
| * Wraps a Promise in a Result. | ||
| * @param promise - The promise to wrap | ||
@@ -197,1 +197,12 @@ * @returns A Promise that resolves to a result | ||
| export declare function tAsync<T>(promise: Promise<T>): Promise<TResult<T, unknown>>; | ||
| /** | ||
| * Pattern matches on a result, calling the appropriate handler. | ||
| * Similar to Rust's match! macro but following KISS principles. | ||
| * @param result - The result to match on | ||
| * @param handlers - Object with ok and err handlers | ||
| * @returns The result of calling the appropriate handler | ||
| */ | ||
| export declare function match<T, E, R>(result: TResult<T, E> | TResultArray<T, E>, handlers: { | ||
| ok: (value: T) => R; | ||
| err: (error: E) => R; | ||
| }): R; |
+1
-1
| { | ||
| "name": "tuple-result", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "private": false, | ||
@@ -5,0 +5,0 @@ "description": "A minimal, functional, and tree-shakable Result library for TypeScript that prioritizes simplicity and serialization", |
+32
-1
@@ -107,5 +107,23 @@ <h1 align="center"> | ||
| ### 6. Serialization | ||
| ### 6. Pattern Matching | ||
| ```ts | ||
| import { match } from 'tuple-result'; | ||
| // Clean conditional logic | ||
| const message = match(result, { | ||
| ok: (value) => `Success: ${value}`, | ||
| err: (error) => `Error: ${error}` | ||
| }); | ||
| // Complex transformations | ||
| const processed = match(result, { | ||
| ok: (user) => ({ ...user, displayName: user.name.toUpperCase() }), | ||
| err: (error) => ({ id: 0, name: 'Unknown', error: error.message }) | ||
| }); | ||
| ``` | ||
| ### 7. Serialization | ||
| ```ts | ||
| // Convert to serializable format | ||
@@ -230,2 +248,15 @@ const serialized = success.toArray(); // [true, undefined, 42] | ||
| ### Pattern Matching Functions | ||
| #### `match<T, E, R>(result: TResult<T, E> | TResultArray<T, E>, handlers: { ok: (value: T) => R; err: (error: E) => R }): R` | ||
| Pattern matches on a result, calling the appropriate handler. Similar to Rust's `match!` macro. | ||
| ```ts | ||
| const message = match(result, { | ||
| ok: (value) => `Success: ${value}`, | ||
| err: (error) => `Error: ${error}` | ||
| }); | ||
| ``` | ||
| ### Function Wrappers | ||
@@ -232,0 +263,0 @@ |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
23871
6.07%221
5.24%334
10.23%