generated from garronej/ts-ci
-
Notifications
You must be signed in to change notification settings - Fork 6
/
withDefault.ts
120 lines (86 loc) 路 2.49 KB
/
withDefault.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { withDefaults } from "../withDefaults.ts";
import { same } from "https://raw.githubusercontent.com/garronej/evt/v1.9.14/deno_dist/tools/inDepth/same.ts";
import { assert } from "../assert.ts";
{
const f = (params: { foo: string; bar: number }): string => {
const { foo, bar } = params;
return `${foo} ${bar}`;
};
const got = withDefaults(f, { "foo": "xxx" })({ "bar": 44 });
const expected = `xxx 44`;
assert(got === expected);
console.log("PASS TEST 1");
}
{
const f = (params: { a: number; b: number }) => {
return params;
};
const got = withDefaults(f, { "a": 44 })({ "defaultsOverwrite": { "a": [33] }, "b": 22 });
const expected = {
"a": 33,
"b": 22,
};
assert(same(got, expected));
console.log("PASS TEST 2");
}
{
const f = (params: { a?: number; b?: number }) => {
return params;
};
const got = withDefaults(f, { "a": 44 })({});
const expected = {
"a": 44,
"b": undefined,
};
assert(same(got, expected));
console.log("PASS TEST 3");
}
{
const f = (params: { a: number; b: number }) => {
const { a, b } = params;
return `${a}${b}`;
};
const fWd = withDefaults(f, { "a": 44 });
const got = fWd({ "b": 33 }) + fWd({ "b": 12 });
const expected = "44334412";
assert(got === expected);
console.log("PASS TEST 4");
}
{
const f = (params: { a: number; b: number }) => {
const { a, b } = params;
return `${a}${b}`;
};
const fWd = withDefaults(f, { "a": 44 });
const got = fWd({ "defaultsOverwrite": { "a": undefined }, "b": 33 });
const expected = "4433";
assert(got === expected);
console.log("PASS TEST 5");
}
{
const f = (params: { a: number | undefined; b: number }) => {
return params;
};
const fWd = withDefaults(f, { "a": 33 });
const got = fWd({ "b": 44, "defaultsOverwrite": { "a": [undefined] } });
const expected = {
"a": undefined,
"b": 44,
};
assert(same(got, expected));
console.log("PASS TEST 6");
}
{
const f = (params: { a?: number; b: number }) => {
return params;
};
const fWd = withDefaults(f, { "a": 33 });
const got = fWd({ "b": 44, "defaultsOverwrite": { "a": [undefined] } });
const expected = {
"a": undefined,
"b": 44,
};
assert(same(got, expected));
console.log("PASS TEST 7");
}