-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
57 lines (46 loc) · 1.41 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { FixedPromise } from '@/types'
export function isPromise<A>(args: unknown): args is FixedPromise<A> {
if (args instanceof Promise) {
return true
}
if (
args !== null &&
typeof args === 'object' &&
typeof (args as any).then === 'function' &&
typeof (args as any).catch === 'function'
) {
return true
}
return false
}
export function isIterable<T = unknown>(args: unknown): args is Iterable<T> {
return typeof (args as any)?.[Symbol.iterator] === 'function'
}
export function isAsyncIterable<T = unknown>(args: unknown): args is AsyncIterable<T> {
return typeof (args as any)?.[Symbol.asyncIterator] === 'function'
}
export function call<A, F extends (args: Awaited<A>) => any>(
args: A,
fn: F
): A extends Promise<unknown> ? FixedPromise<ReturnType<F>> : ReturnType<F> {
return isPromise(args) ? args.then(fn as any) : fn(args as any)
}
export class IterableTypeException extends Error {
public constructor() {
super('"iterable" must be type of Iterable or AsyncIterable')
}
}
export class IterableAsyncFnException extends Error {
public constructor() {
super('Iterable cannot work with async function')
}
}
export class FlowMissModifierException extends Error {
public constructor() {
super('"modifier" is required')
}
}
export function toStr(x: unknown) {
return JSON.stringify(x, Object.getOwnPropertyNames(x), 1)
}
export function* empty() {}