-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
stub.ts
81 lines (75 loc) · 2.22 KB
/
stub.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { requiredDeep } from 'unpartial'
import type { AnyFunction } from '../function/any_function.js'
import type { RecursivePartial } from '../object/recursive_partial.js'
import type { NoInfer } from '../type/no_infer.js'
/**
* stub a value.
*
* If the value is a function, it will be passed through as-is.
*
* 🦴 `utilities`
*/
export function stub<T extends AnyFunction>(stub: T): T
export function stub<T>(stub: RecursivePartial<NoInfer<T>>): T
export function stub<T>(stub: unknown): T {
return stub as T
}
/**
* builds a stub function
*/
function build<T>(
init: RecursivePartial<T> | ((stub?: RecursivePartial<T>) => RecursivePartial<T>)
): (stub?: RecursivePartial<T>) => T
function build<T>(init: RecursivePartial<T> | ((stub?: RecursivePartial<T>) => RecursivePartial<T>)) {
return builder(init).create()
}
/**
* Create a builder for a stub function of type T.
*
* The builder contains two methods:
*
* `.with()`: adds additional handler or partial stub.
* `.create()`: creates the final stub function.
*
* @example
* ```ts
* const b = stub.builder<{ a: number; b: string }>({ a: 1 }).with({ b: 'b' }).create()
* b({ a: 2 }) // { a: 2, b: 'b' }
* ```
*/
function builder<T>(init: RecursivePartial<T> | ((stub?: RecursivePartial<T>) => RecursivePartial<T>)) {
return builderInternal([init])
}
function builderInternal<T>(
initializers: Array<RecursivePartial<T> | ((stub?: RecursivePartial<T>) => RecursivePartial<T>)>
) {
const builder = {
/**
* Adds an init object or handler to the builder.
*
* If `init` is an object, it will be merged with the stub object.
* If `init` is a function, it will be called with the stub object.
*
* @return {Builder<T>} The builder instance.
*/
with(init: RecursivePartial<T> | ((stub?: RecursivePartial<T>) => RecursivePartial<T>)) {
return builderInternal([...initializers, init])
},
/**
* Creates the resulting stub function.
*/
create() {
return (stub?: RecursivePartial<T>) => {
return initializers.reduce((acc, init) => {
if (typeof init === 'function') {
return init(acc)
}
return requiredDeep<RecursivePartial<T>>(init, acc)
}, stub) as T
}
}
}
return builder
}
stub.build = build
stub.builder = builder