From 61415359cd8ebd64cfa9e4dae7d3e69da4e77175 Mon Sep 17 00:00:00 2001 From: Connor Sinnott Date: Wed, 17 Apr 2024 08:42:34 -0700 Subject: [PATCH] Add non-spread overload for Result.all --- src/result.ts | 25 ++++++++++++++++ test/result.test.ts | 70 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/src/result.ts b/src/result.ts index acb66d3..25d0435 100644 --- a/src/result.ts +++ b/src/result.ts @@ -437,9 +437,19 @@ export namespace Result { * Parse a set of `Result`s, returning an array of all `Ok` values. * Short circuits with the first `Err` found, if any */ + export function all[]>(results: T): Result, ResultErrTypes[number]> export function all[]>( ...results: T + ): Result, ResultErrTypes[number]> + export function all[]>( + arg0: Head | T, ...argN: Tail ): Result, ResultErrTypes[number]> { + const results = arg0 === undefined + ? [] + : Array.isArray(arg0) + ? arg0 as T + : [arg0, ...argN] as T; + const okResult = []; for (let result of results) { if (result.isOk()) { @@ -456,9 +466,19 @@ export namespace Result { * Parse a set of `Result`s, short-circuits when an input value is `Ok`. * If no `Ok` is found, returns an `Err` containing the collected error values */ + export function any[]>(results: T): Result[number], ResultErrTypes> export function any[]>( ...results: T + ): Result[number], ResultErrTypes> + export function any[]>( + arg0: Head | T, ...argN: Tail ): Result[number], ResultErrTypes> { + const results = arg0 === undefined + ? [] + : Array.isArray(arg0) + ? arg0 as T + : [arg0, ...argN] as T; + const errResult = []; // short-circuits @@ -504,3 +524,8 @@ export namespace Result { return val instanceof Err || val instanceof Ok; } } + +// Utility types +type Head = T extends [any, ...infer R] ? + T extends [...infer F, ...R] ? F : never : never +type Tail = T extends [any, ...infer R] ? R : never diff --git a/test/result.test.ts b/test/result.test.ts index ddfd1e6..ad3effb 100644 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -110,20 +110,39 @@ test('Result.all', () => { expect(all0).toMatchResult(Ok([])); eq>(true); + const all0_array = Result.all([]); + expect(all0_array).toMatchResult(Ok([])); + eq>(true); + const all1 = Result.all(ok0, ok1); expect(all1).toMatchResult(Ok([3, true])); eq>(true); - const all3 = Result.all(err0, err1); - expect(all3).toMatchResult(Err(err0.error)); - eq>(true); + const all1Array = Result.all([ok0, ok1]); + expect(all1Array).toMatchResult(Ok([3, true])); + eq>(true); + + const all2 = Result.all(err0, err1); + expect(all2).toMatchResult(Err(err0.error)); + eq>(true); + + const all2Array = Result.all([err0, err1]); + expect(all2Array).toMatchResult(Err(err0.error)); + eq>(true); + + const all3 = Result.all(...([] as Result[])); + eq>(true); + + const all3Array = Result.all([] as Result[]); + eq>(true); - const all4 = Result.all(...([] as Result[])); - eq>(true); + const all4 = Result.all(ok0, ok1, ok2, err2); + expect(all4).toMatchResult(Err(9)); + eq>(true); - const all5 = Result.all(ok0, ok1, ok2, err2); - expect(all5).toMatchResult(Err(9)); - eq>(true); + const all4Array = Result.all([ok0, ok1, ok2, err2]); + expect(all4Array).toMatchResult(Err(9)); + eq>(true); }); test('Result.any', () => { @@ -138,20 +157,39 @@ test('Result.any', () => { expect(any0).toMatchResult(Err([])); eq>(true); + const any0Array = Result.any(); + expect(any0Array).toMatchResult(Err([])); + eq>(true); + const any1 = Result.any(ok0, ok1); expect(any1).toMatchResult(Ok(3)); eq>(true); - const any3 = Result.any(err0, err1); - expect(any3).toMatchResult(Err([err0.error, err1.error])); - eq>(true); + const any1Array = Result.any([ok0, ok1]); + expect(any1Array).toMatchResult(Ok(3)); + eq>(true); + + const any2 = Result.any(err0, err1); + expect(any2).toMatchResult(Err([err0.error, err1.error])); + eq>(true); + + const any2Array = Result.any([err0, err1]); + expect(any2Array).toMatchResult(Err([err0.error, err1.error])); + eq>(true); + + const any3 = Result.any(...([] as Result[])); + eq>(true); + + const any3Array = Result.any([] as Result[]); + eq>(true); - const any4 = Result.any(...([] as Result[])); - eq>(true); + const any4 = Result.any(err0, err1, err2, ok2); + expect(any4).toMatchResult(Ok(8)); + eq>(true); - const any5 = Result.any(err0, err1, err2, ok2); - expect(any5).toMatchResult(Ok(8)); - eq>(true); + const any4Array = Result.any([err0, err1, err2, ok2]); + expect(any4Array).toMatchResult(Ok(8)); + eq>(true); }); test('Result.wrap', () => {