Skip to content

Commit

Permalink
refactor: rename to MaybePromise
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Oct 31, 2022
1 parent 440d172 commit 6401455
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ JSONTypes.get<string>(someJson, 'a', 'b', 1, 'c') // miku
- `SpreadRecord<A, B>`: type for `{...a, ...b}` when both `a` and `b` are `Record`\
for array, just do `[...A, ...B]`.

### Promise function
### Promise utilities

- `isPromise<R>(subject: any)`: `isPromise()` type guard.
- `PromiseOrValue<T>`: Alias of `T | Promise<T>`.
- `MaybePromise<T>`: Alias of `T | Promise<T>`.
- `PromiseValue<P>`: Gets the type within the Promise.
- `PromiseValueMerge<P1, P2, ...P9>`: Merge the values of multiple promises.
- `mapSeries()`: Similar to `bluebird.mapSeries()` but works with `async`/`await`.
- `transformPromiseOrValue(value, transformer)`: Apply the `transformer` to the `value`.\
It is also exported under `PromiseOrValue.transform()`.
- `transformMaybePromise(value, transformer)`: Apply the `transformer` to the `value`.\
It is also exported under `MaybePromise.transform()`.

### Type manipulation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { isType, PromiseOrValue } from '../index.js'
import { isType, MaybePromise } from '../index.js'

describe(`${PromiseOrValue.transform.name}()`, () => {
describe(`${MaybePromise.transform.name}()`, () => {
it('returns the result from the handler directly if it is not a promise', () => {
const r = PromiseOrValue.transform(1, v => String(v))
const r = MaybePromise.transform(1, v => String(v))

isType.equal<true, string, typeof r>()
expect(r).toEqual('1')
})

it('returns a promise that resolves to the result from the handler', async () => {
const r = PromiseOrValue.transform(Promise.resolve(1), v => String(v))
const r = MaybePromise.transform(Promise.resolve(1), v => String(v))

isType.equal<true, Promise<string>, typeof r>()
expect(r).not.toEqual('1')
expect(await r).toEqual('1')
})

it('preserves the type of it is PromiseOrValue', () => {
const v: PromiseOrValue<number> = 1 as PromiseOrValue<number>
const r = PromiseOrValue.transform(v, v => String(v))
it('preserves the type of it is MaybePromise', () => {
const v: MaybePromise<number> = 1 as MaybePromise<number>
const r = MaybePromise.transform(v, v => String(v))
isType.equal<true, string | Promise<string>, typeof r>()
expect(r).toEqual('1')
})
Expand Down
12 changes: 6 additions & 6 deletions ts/promise/PromiseOrValue.ts → ts/promise/MaybePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { isPromise } from './isPromise.js'
/**
* `T | Promise<T>`
*/
export type PromiseOrValue<T> = T | Promise<T>
export type MaybePromise<T> = T | Promise<T>

/**
* Transforms the value within the promise.
*
* @return a new promise with the transformed result.
*/
export function transformPromiseOrValue<T, R>(value: Promise<T>, transformer: (value: T) => R): Promise<R>
export function transformMaybePromise<T, R>(value: Promise<T>, transformer: (value: T) => R): Promise<R>
/**
* Transforms the value, or if the value is a promise,
* transform the resolved value of the promise.
*
* @return the transformed result,
* or if the value is a promise, a new promse with the transformed result.
*/
export function transformPromiseOrValue<T, R>(value: T, transformer: (value: T) => R): T extends Promise<any> ? Promise<R> : R
export function transformPromiseOrValue<T, R>(value: PromiseOrValue<T>, transformer: (value: T) => R) {
export function transformMaybePromise<T, R>(value: T, transformer: (value: T) => R): T extends Promise<any> ? Promise<R> : R
export function transformMaybePromise<T, R>(value: MaybePromise<T>, transformer: (value: T) => R) {
return isPromise(value) ? value.then(transformer) : transformer(value)
}

export const PromiseOrValue = {
transform: transformPromiseOrValue
export const MaybePromise = {
transform: transformMaybePromise
}
2 changes: 1 addition & 1 deletion ts/promise/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './isPromise.js'
export * from './mapSeries.js'
export * from './PromiseOrValue.js'
export * from './MaybePromise.js'
export type { PromiseValue } from './PromiseValue.js'
export type { PromiseValueMerge } from './PromiseValueMerge.js'

0 comments on commit 6401455

Please sign in to comment.