-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix seriesAll test type error * update docs command pnpm bump -> pnpm release * namedSeriesAll
- Loading branch information
Showing
6 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, test } from '@jest/globals' | ||
import { namedSeriesAll } from './namedSeriesAll' | ||
import { sleep } from './sleep' | ||
|
||
describe('namedSeriesAll', () => { | ||
test('simple', async () => { | ||
expect( | ||
await namedSeriesAll({ | ||
a: Promise.resolve(true), | ||
b: sleep(1).then(() => 42), | ||
c: () => Promise.resolve('foo'), | ||
d: async () => [69], | ||
e: async () => { | ||
await sleep(1) | ||
return null | ||
}, | ||
f: async () => { | ||
return sleep(1).then(() => {}) | ||
}, | ||
}), | ||
).toStrictEqual({ | ||
a: true, | ||
b: 42, | ||
c: 'foo', | ||
d: [69], | ||
e: null, | ||
f: undefined, | ||
}) | ||
}) | ||
|
||
test('throw new Error', () => { | ||
expect( | ||
namedSeriesAll({ | ||
first: () => { | ||
throw new Error('1') | ||
}, | ||
second: () => { | ||
throw new Error('2') | ||
}, | ||
}), | ||
).rejects.toThrowError('1') | ||
}) | ||
|
||
test('Promise.reject', () => { | ||
expect( | ||
namedSeriesAll({ | ||
first: Promise.reject('3'), | ||
second: () => Promise.reject('4'), | ||
}), | ||
).rejects.toEqual('3') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isFunction, isPromise } from '../validators' | ||
|
||
type NamedSeriesResult<T extends Record<string, unknown>> = { | ||
[K in keyof T]: T[K] extends Promise<infer U> | ||
? U | ||
: T[K] extends () => Promise<infer U> | ||
? U | ||
: never | ||
} & {} | ||
|
||
/** | ||
* @description Run a series of (async) functions in order and return the results as an object with the same keys. | ||
* @example | ||
* const { foo, bar } = await namedSeriesAll({ | ||
* foo: Promise.resolve(1), | ||
* bar: async () => 2, | ||
* }); // => { foo: 1, bar: 2 } | ||
*/ | ||
export const namedSeriesAll = async <T extends Record<string, unknown>>( | ||
series: T, | ||
): Promise<NamedSeriesResult<T>> => { | ||
const results = {} as NamedSeriesResult<T> | ||
|
||
for (const key of Object.keys(series) as (keyof T)[]) { | ||
const fn = series[key] | ||
if (isPromise(fn)) | ||
results[key] = (await fn) as NamedSeriesResult<T>[typeof key] | ||
else if (isFunction(fn)) results[key] = await fn() | ||
else throw new Error('namedSeriesAll: invalid type') | ||
} | ||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters