You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23โ€“26.RSVP โ†’
Socket
Book a DemoSign in
Socket

tuple-result

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tuple-result - npm Package Compare versions

Comparing version
0.0.4
to
0.0.5
+1
-1
dist/cjs/index.js

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

"use strict";var E=(r,t,n)=>new Promise((w,l)=>{var p=e=>{try{i(n.next(e))}catch(o){l(o)}},h=e=>{try{i(n.throw(e))}catch(o){l(o)}},i=e=>e.done?w(e.value):Promise.resolve(e.value).then(p,h);i((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]}}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")}}function u(r){return new a(r)}const O=u;function s(r){return new c(r)}const k=s;function y(r){return r instanceof a}function d(r){return r instanceof c}function v(r){return r.unwrap()}function m(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 f(r,t){return r[0]?r[2]:t}function g(r){return f(r,null)}function z(r,t){return r[0]?u(t(r[2])):s(r[1])}function A(r,t){return r[0]?u(r[2]):s(t(r[1]))}function R(r){return r[0]?[!0,r[2]]:[!1,r[1]]}function N(r){return r[0]?u(r[1]):s(r[1])}function P(r,...t){try{const n=r(...t);return u(n)}catch(n){return s(n)}}function T(r){return E(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return s(t)}})}exports.Err=s,exports.ErrResult=c,exports.Ok=u,exports.OkResult=a,exports.deserialize=N,exports.err=k,exports.isErr=d,exports.isOk=y,exports.mapErr=A,exports.mapOk=z,exports.ok=O,exports.serialize=R,exports.t=P,exports.tAsync=T,exports.unwrap=v,exports.unwrapErr=x,exports.unwrapOk=m,exports.unwrapOr=f,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 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;

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

var E=(r,t,n)=>new Promise((h,f)=>{var w=e=>{try{o(n.next(e))}catch(i){f(i)}},p=e=>{try{o(n.throw(e))}catch(i){f(i)}},o=e=>e.done?h(e.value):Promise.resolve(e.value).then(w,p);o((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]}}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")}}function u(r){return new c(r)}const k=u;function s(r){return new a(r)}const y=s;function O(r){return r instanceof c}function d(r){return r instanceof a}function v(r){return r.unwrap()}function x(r){if(r[0])return r[2];throw new Error("Expected an Ok result")}function g(r){if(!r[0])return r[1];throw new Error("Expected an Err result")}function l(r,t){return r[0]?r[2]:t}function m(r){return l(r,null)}function A(r,t){return r[0]?u(t(r[2])):s(r[1])}function z(r,t){return r[0]?u(r[2]):s(t(r[1]))}function P(r){return r[0]?[!0,r[2]]:[!1,r[1]]}function R(r){return r[0]?u(r[1]):s(r[1])}function T(r,...t){try{const n=r(...t);return u(n)}catch(n){return s(n)}}function N(r){return E(this,null,function*(){try{const t=yield r;return u(t)}catch(t){return s(t)}})}export{s as Err,a as ErrResult,u as Ok,c as OkResult,R as deserialize,y as err,d as isErr,O as isOk,z as mapErr,A as mapOk,k as ok,P as serialize,T as t,N as tAsync,v as unwrap,g as unwrapErr,x as unwrapOk,l as unwrapOr,m 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 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};
/**
* Represents a successful result as an array with literal types.
* Structure: [true, undefined, T] where T is the success value.
* Extends Array to provide both array destructuring and Result methods.
* Extends Array to provide both array destructuring and common Result methods.
*/

@@ -12,7 +12,32 @@ export declare class OkResult<T, E> extends Array<true | undefined | T> {

constructor(value: T);
/**
* Gets the success value from this result.
* @returns The success value of type T
*/
get value(): T;
/**
* Gets the error value (always undefined for Ok results).
* @returns Always undefined for Ok results
*/
get error(): E | undefined;
/**
* Type guard to check if this result is successful.
* @returns Always true for OkResult instances
*/
isOk(): this is OkResult<T, E>;
/**
* Type guard to check if this result is an error.
* @returns Always false for OkResult instances
*/
isErr(): this is ErrResult<T, E>;
/**
* Extracts the success value from this result.
* @returns The success value of type T
*/
unwrap(): T;
/**
* Converts the result to a plain array for serialization.
* @returns A plain array [true, undefined, T]
*/
toArray(): [true, undefined, T];
}

@@ -22,3 +47,3 @@ /**

* Structure: [false, E, undefined] where E is the error value.
* Extends Array to provide both array destructuring and Result methods.
* Extends Array to provide both array destructuring and common Result methods.
*/

@@ -31,7 +56,32 @@ export declare class ErrResult<T, E> extends Array<false | E | undefined> {

constructor(error: E);
/**
* Gets the success value (always undefined for Err results).
* @returns Always undefined for Err results
*/
get value(): T | undefined;
/**
* Gets the error value from this result.
* @returns The error value of type E
*/
get error(): E;
/**
* Type guard to check if this result is successful.
* @returns Always false for ErrResult instances
*/
isOk(): this is OkResult<T, E>;
/**
* Type guard to check if this result is an error.
* @returns Always true for ErrResult instances
*/
isErr(): this is ErrResult<T, E>;
/**
* Attempts to extract the success value, but always throws since this is an error result.
* @throws The error contained in this result
*/
unwrap(): never;
/**
* Converts the result to a plain array for serialization.
* @returns A plain array [false, E, undefined]
*/
toArray(): [false, E, undefined];
}

@@ -44,2 +94,7 @@ /**

/**
* Represents a result as a plain array.
* Can be destructured as [boolean, E | undefined, T | undefined].
*/
export type TResultArray<T, E> = [true, undefined, T] | [false, E, undefined];
/**
* Creates a successful result containing the given value.

@@ -63,3 +118,3 @@ * @param value - The success value to wrap

*/
export declare function isOk<T, E>(result: TResult<T, E>): result is OkResult<T, E>;
export declare function isOk<T, E>(result: TResult<T, E> | TResultArray<T, E>): result is OkResult<T, E>;
/**

@@ -70,3 +125,3 @@ * Type guard to check if a result is an error.

*/
export declare function isErr<T, E>(result: TResult<T, E>): result is ErrResult<T, E>;
export declare function isErr<T, E>(result: TResult<T, E> | TResultArray<T, E>): result is ErrResult<T, E>;
/**

@@ -78,3 +133,3 @@ * Extracts the value from a result, throwing if it's an error.

*/
export declare function unwrap<T, E>(result: TResult<T, E>): T;
export declare function unwrap<T, E>(result: TResult<T, E> | TResultArray<T, E>): T;
/**

@@ -86,3 +141,3 @@ * Extracts the value from an Ok result, throwing if it's an error.

*/
export declare function unwrapOk<T, E>(result: TResult<T, E>): T;
export declare function unwrapOk<T, E>(result: TResult<T, E> | TResultArray<T, E>): T;
/**

@@ -94,3 +149,3 @@ * Extracts the error from an Err result, throwing if it's successful.

*/
export declare function unwrapErr<T, E>(result: TResult<T, E>): E;
export declare function unwrapErr<T, E>(result: TResult<T, E> | TResultArray<T, E>): E;
/**

@@ -102,3 +157,3 @@ * Extracts the value from a result, returning a default if it's an error.

*/
export declare function unwrapOr<T, E>(result: TResult<T, E>, defaultValue: T): T;
export declare function unwrapOr<T, E>(result: TResult<T, E> | TResultArray<T, E>, defaultValue: T): T;
/**

@@ -109,43 +164,43 @@ * Extracts the value from a result, returning null if it's an error.

*/
export declare function unwrapOrNull<T, E>(result: TResult<T, E>): T | null;
export declare function unwrapOrNull<T, E>(result: TResult<T, E> | TResultArray<T, E>): T | null;
/**
* Maps the value inside an Ok result using the provided function.
* If the input is an Err result, it returns the Err result unchanged.
* Returns a new result with the mapped value or the original error.
* @param result - The result to map
* @param mapFn - Function to transform the success value
* @returns A new Result with the mapped value or the original error
* @returns A new result with the mapped value or the original error
*/
export declare function mapOk<T, E, U>(result: TResult<T, E>, mapFn: (value: T) => U): TResult<U, E>;
export declare function mapOk<T, E, U>(result: TResult<T, E> | TResultArray<T, E>, mapFn: (value: T) => U): TResult<U, E>;
/**
* Maps the error inside an Err result using the provided function.
* If the input is an Ok result, it returns the Ok result unchanged.
* Returns a new result with the mapped error or the original success value.
* @param result - The result to map
* @param mapFn - Function to transform the error value
* @returns A new Result with the mapped error or the original success value
* @returns A new result with the mapped error or the original success value
*/
export declare function mapErr<T, E, F>(result: TResult<T, E>, mapFn: (error: E) => F): TResult<T, F>;
export declare function mapErr<T, E, F>(result: TResult<T, E> | TResultArray<T, E>, mapFn: (error: E) => F): TResult<T, F>;
/**
* Converts a result to a wire-friendly format.
* @param result - The result to serialize
* @returns A tuple [boolean, T | E] for transmission
* Converts a result to a plain array for serialization.
* @param result - The result to convert
* @returns A plain array representation
*/
export declare function serialize<T, E>(result: TResult<T, E>): [boolean, T | E];
export declare function toArray<T, E>(result: TResult<T, E> | TResultArray<T, E>): TResultArray<T, E>;
/**
* Converts a serialized result back to a TResult.
* @param serialized - The serialized tuple [boolean, T | E]
* @returns A TResult instance
* Creates a result from a plain array.
* @param array - The array to convert
* @returns A result instance with methods
*/
export declare function deserialize<T, E>(serialized: [boolean, T | E]): TResult<T, E>;
export declare function fromArray<T, E>(array: TResult<T, E> | TResultArray<T, E>): TResult<T, E>;
/**
* Wraps a synchronous function call in a Result.
* Wraps a synchronous function call in a result.
* @param fn - The function to wrap
* @param args - Arguments to pass to the function
* @returns A Result containing the function's return value or error
* @returns A result containing the function's return value or error
*/
export declare function t<T, Args extends any[]>(fn: (...args: Args) => T, ...args: Args): TResult<T, unknown>;
/**
* Wraps a Promise in a Result.
* Wraps a Promise in a result.
* @param promise - The promise to wrap
* @returns A Promise that resolves to a Result
* @returns A Promise that resolves to a result
*/
export declare function tAsync<T>(promise: Promise<T>): Promise<TResult<T, unknown>>;
{
"name": "tuple-result",
"version": "0.0.4",
"version": "0.0.5",
"private": false,

@@ -28,4 +28,4 @@ "description": "A minimal, functional, and tree-shakable Result library for TypeScript that prioritizes simplicity and serialization",

"@types/node": "^24.1.0",
"rollup-presets": "0.0.22",
"@blgc/config": "0.0.36"
"@blgc/config": "0.0.36",
"rollup-presets": "0.0.22"
},

@@ -32,0 +32,0 @@ "size-limit": [

+72
-26

@@ -30,2 +30,6 @@ <h1 align="center">

### ๐Ÿ“š Examples
- [React Router v7](https://github.com/builder-group/community/tree/develop/examples/tuple-result/react-router/basic) ([CodeSandbox](https://codesandbox.io/p/devbox/zkdsr2))
### ๐ŸŒŸ Motivation

@@ -47,3 +51,3 @@

```ts
import { Ok, Err } from 'tuple-result';
import { Err, Ok } from 'tuple-result';

@@ -87,9 +91,6 @@ const success = Ok(42);

```ts
import { unwrapOr, unwrapErr } from 'tuple-result';
import { unwrapErr, unwrapOr } from 'tuple-result';
// Provide defaults
const value = unwrapOr(failure, 0); // 0
// Extract errors safely
const error = unwrapErr(failure); // 'Something went wrong'
```

@@ -100,9 +101,9 @@

```ts
import { mapOk, mapErr } from 'tuple-result';
import { mapErr, mapOk } from 'tuple-result';
// Transform success values
const doubled = mapOk(success, x => x * 2); // Ok(84)
const doubled = mapOk(success, (x) => x * 2); // Ok(84)
// Transform errors
const wrapped = mapErr(failure, e => `Error: ${e}`); // Err('Error: Something went wrong')
const wrapped = mapErr(failure, (e) => `Error: ${e}`); // Err('Error: Something went wrong')
```

@@ -113,9 +114,7 @@

```ts
import { serialize, deserialize } from 'tuple-result';
// Convert to serializable format
const serialized = success.toArray(); // [true, undefined, 42]
// Convert to wire format
const wireFormat = serialize(success); // [true, 42]
// Reconstruct from wire format
const reconstructed = deserialize(wireFormat); // Back to TResult
// Reconstruct from serialized format
const reconstructed = fromArray(serialized); // Back to TResult with methods
```

@@ -128,2 +127,3 @@

#### `Ok<T, E>(value: T): OkResult<T, E>`
Creates a successful result containing the given value.

@@ -134,6 +134,7 @@

console.log(result.unwrap()); // 42
console.log(result.isOk()); // true
console.log(result.isOk()); // true
```
#### `Err<T, E>(error: E): ErrResult<T, E>`
Creates an error result containing the given error.

@@ -143,4 +144,4 @@

const result = Err('Something went wrong');
console.log(result.isErr()); // true
console.log(result.error); // 'Something went wrong'
console.log(result.isErr()); // true
console.log(result.error); // 'Something went wrong'
```

@@ -151,2 +152,3 @@

#### `isOk<T, E>(result: TResult<T, E>): result is OkResult<T, E>`
Type guard to check if a result is successful.

@@ -162,2 +164,3 @@

#### `isErr<T, E>(result: TResult<T, E>): result is ErrResult<T, E>`
Type guard to check if a result is an error.

@@ -175,2 +178,3 @@

#### `unwrap<T, E>(result: TResult<T, E>): T`
Extracts the value from a result, throwing if it's an error.

@@ -187,2 +191,3 @@

#### `unwrapOk<T, E>(result: TResult<T, E>): T`
Extracts the value from an Ok result, throwing if it's an error.

@@ -195,2 +200,3 @@

#### `unwrapErr<T, E>(result: TResult<T, E>): E`
Extracts the error from an Err result, throwing if it's successful.

@@ -203,2 +209,3 @@

#### `unwrapOr<T, E>(result: TResult<T, E>, defaultValue: T): T`
Extracts the value from a result, returning a default if it's an error.

@@ -211,2 +218,3 @@

#### `unwrapOrNull<T, E>(result: TResult<T, E>): T | null`
Extracts the value from a result, returning null if it's an error.

@@ -221,13 +229,15 @@

#### `mapOk<T, E, U>(result: TResult<T, E>, mapFn: (value: T) => U): TResult<U, E>`
Maps the value inside an Ok result using the provided function.
```ts
const doubled = mapOk(Ok(21), x => x * 2); // Ok(42)
const doubled = mapOk(Ok(21), (x) => x * 2); // Ok(42)
```
#### `mapErr<T, E, F>(result: TResult<T, E>, mapFn: (error: E) => F): TResult<T, F>`
Maps the error inside an Err result using the provided function.
```ts
const wrapped = mapErr(Err(404), code => `HTTP ${code}`); // Err('HTTP 404')
const wrapped = mapErr(Err(404), (code) => `HTTP ${code}`); // Err('HTTP 404')
```

@@ -238,2 +248,3 @@

#### `t<T, Args extends any[]>(fn: (...args: Args) => T, ...args: Args): TResult<T, unknown>`
Wraps a synchronous function call in a Result.

@@ -247,2 +258,3 @@

#### `tAsync<T>(promise: Promise<T>): Promise<TResult<T, unknown>>`
Wraps a Promise in a Result.

@@ -256,16 +268,50 @@

#### `serialize<T, E>(result: TResult<T, E>): [boolean, T | E]`
Converts a result to a wire-friendly format.
#### `toArray()` (Instance Method)
Converts a result to a plain array for serialization.
```ts
const wireFormat = serialize(Ok(42)); // [true, 42]
const result = Ok(42);
const serialized = result.toArray(); // [true, undefined, 42]
```
#### `deserialize<T, E>(serialized: [boolean, T | E]): TResult<T, E>`
Converts a serialized result back to a TResult.
#### `fromArray<T, E>(array: TResultArray<T, E>): TResult<T, E>`
Creates a result instance from a plain array.
```ts
const result = deserialize([true, 42]); // Ok(42)
const result = fromArray([true, undefined, 42]); // Ok(42) with methods
```
## โ“ FAQ
### Why both `TResult` and `TResultArray`?
**`TResultArray` is a subset of `TResult`** - same array structure, but `TResult` adds convenience methods.
- **`TResult`**: Full-featured classes with `.isOk()`, `.unwrap()`, `.value` methods
- **`TResultArray`**: Plain arrays perfect for serialization (React Router, APIs, JSON)
**Key benefit:** All helper functions work with both types seamlessly.
```typescript
const classResult = Ok('hello');
const arrayResult = [true, undefined, 'hello'] as const;
isOk(classResult); // โœ… works
isOk(arrayResult); // โœ… also works
```
### When do I use each?
**Use `TResult` by default.** You get `TResultArray` from:
- React Router loaders: `useLoaderData()`
- JSON parsing: `JSON.parse(response)`
- API responses
**For serialization:** `result.toArray()` โ†’ send over network โ†’ use helpers directly on received arrays or deserialize using `fromArray(result)`.
No conversion needed - helpers work with both!
## ๐Ÿ’ก Resources / References

@@ -275,2 +321,2 @@

- [ts-results](https://github.com/vultix/ts-results)
- [neverthrow](https://github.com/supermacro/neverthrow)
- [neverthrow](https://github.com/supermacro/neverthrow)