@etsoo/shared 1.2.54 → 1.2.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/ArrayUtils.ts +126 -126
- package/__tests__/ColorUtils.ts +24 -24
- package/__tests__/ContentDisposition.ts +18 -18
- package/__tests__/DataTypes.ts +222 -204
- package/__tests__/DateUtils.ts +82 -82
- package/__tests__/DomUtils.ts +352 -352
- package/__tests__/EHistory.ts +62 -62
- package/__tests__/ExtendUtils.ts +40 -40
- package/__tests__/Keyboard.ts +14 -14
- package/__tests__/NumberUtils.ts +37 -39
- package/__tests__/StorageUtils.ts +27 -27
- package/__tests__/Utils.ts +361 -363
- package/__tests__/tsconfig.json +16 -15
- package/lib/cjs/Utils.d.ts +1 -1
- package/lib/mjs/Utils.d.ts +1 -1
- package/package.json +6 -22
- package/src/Utils.ts +6 -6
- package/tsconfig.cjs.json +1 -1
- package/tsconfig.json +3 -3
- package/vite.config.mts +11 -0
package/__tests__/Utils.ts
CHANGED
|
@@ -1,420 +1,418 @@
|
|
|
1
|
-
import { Utils } from
|
|
2
|
-
|
|
3
|
-
test(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const result2 = url.addUrlParams(data, true);
|
|
42
|
-
|
|
43
|
-
test('addUrlParams with array format', () => {
|
|
44
|
-
expect(result2).toBe(
|
|
45
|
-
'https://www.etsoo.com/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1%2C2&f=a%2Cb&g='
|
|
46
|
-
);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const result22 = '/'.addUrlParams(data);
|
|
50
|
-
test('addUrlParams with relative path', () => {
|
|
51
|
-
expect(result22).toBe(
|
|
52
|
-
'/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g='
|
|
53
|
-
);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
global.URL = undefined as any;
|
|
57
|
-
|
|
58
|
-
const result3 = url.addUrlParams(data);
|
|
59
|
-
|
|
60
|
-
test('addUrlParams with traditional way', () => {
|
|
61
|
-
expect(result3).toBe(result1);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const result4 = url.addUrlParams(data, true);
|
|
65
|
-
|
|
66
|
-
test('addUrlParams with traditional way and array format', () => {
|
|
67
|
-
expect(result4).toBe(result2);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('Tests for containChinese', () => {
|
|
72
|
-
expect('123 abC'.containChinese()).toBeFalsy();
|
|
73
|
-
expect('亿速思维'.containChinese()).toBeTruthy();
|
|
74
|
-
expect('亿速Etsoo'.containChinese()).toBeTruthy();
|
|
75
|
-
expect('김 민수'.containKorean()).toBeTruthy();
|
|
76
|
-
expect('ぁ-ん'.containJapanese()).toBeTruthy();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('Tests for correctTypes', () => {
|
|
80
|
-
const input = {
|
|
81
|
-
id: '1',
|
|
82
|
-
ignore: '2',
|
|
83
|
-
price: '6.0',
|
|
84
|
-
amount: '',
|
|
85
|
-
date: '2022/01/28',
|
|
86
|
-
enabled: 'true',
|
|
87
|
-
ids: ['1', '2']
|
|
88
|
-
};
|
|
89
|
-
Utils.correctTypes(input, {
|
|
90
|
-
id: 'number',
|
|
91
|
-
price: 'number',
|
|
92
|
-
amount: 'number',
|
|
93
|
-
date: 'date',
|
|
94
|
-
enabled: 'boolean',
|
|
95
|
-
ids: 'number[]'
|
|
96
|
-
});
|
|
97
|
-
expect(typeof input.id).toBe('number');
|
|
98
|
-
expect(typeof input.price).toBe('number');
|
|
99
|
-
expect(input.amount).toBeUndefined();
|
|
100
|
-
expect((input.date as any) instanceof Date ? true : false).toBeTruthy();
|
|
101
|
-
expect(input.enabled).toBeTruthy();
|
|
102
|
-
expect(input.ids).toStrictEqual([1, 2]);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('Tests for getDataChanges', () => {
|
|
106
|
-
const input = {
|
|
107
|
-
id: 1,
|
|
108
|
-
name: 'Name',
|
|
109
|
-
gender: 'F',
|
|
110
|
-
brand: '',
|
|
111
|
-
price: '6.0',
|
|
112
|
-
amount: '',
|
|
113
|
-
enabled: true,
|
|
114
|
-
value: undefined,
|
|
115
|
-
date: new Date('2023/03/18'),
|
|
116
|
-
ids: [1, 2],
|
|
117
|
-
data: { d1: 1, d2: false, d3: 1.2, d4: 'Hello' }
|
|
118
|
-
};
|
|
119
|
-
const initData = {
|
|
120
|
-
id: 1,
|
|
121
|
-
name: 'Name',
|
|
122
|
-
gender: 'M',
|
|
123
|
-
brand: 'ETSOO',
|
|
124
|
-
price: 6,
|
|
125
|
-
amount: 0,
|
|
126
|
-
date: '2023/03/18',
|
|
127
|
-
enabled: true,
|
|
128
|
-
ids: [1, 2],
|
|
129
|
-
data: { d1: 1, d3: 1.2, d4: 'Hello', d2: false }
|
|
130
|
-
};
|
|
131
|
-
const fields = Utils.getDataChanges(input, initData);
|
|
132
|
-
expect(fields).toStrictEqual(['gender', 'brand', 'amount']);
|
|
133
|
-
expect(input.price).toBeUndefined();
|
|
134
|
-
expect(input.amount).toBeUndefined();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('Tests for object array getDataChanges', () => {
|
|
138
|
-
const input = {
|
|
139
|
-
id: 1,
|
|
140
|
-
ids: [1, 2],
|
|
141
|
-
items: [
|
|
142
|
-
{ id: 1, label: 'a' },
|
|
143
|
-
{ id: 2, label: 'b' }
|
|
144
|
-
]
|
|
145
|
-
};
|
|
146
|
-
const initData = {
|
|
147
|
-
id: 1,
|
|
148
|
-
ids: [1],
|
|
149
|
-
items: [
|
|
150
|
-
{ id: 1, label: 'a' },
|
|
151
|
-
{ id: 2, label: 'b' }
|
|
152
|
-
]
|
|
153
|
-
};
|
|
154
|
-
const fields = Utils.getDataChanges(input, initData);
|
|
155
|
-
expect(fields).toStrictEqual(['ids']);
|
|
156
|
-
expect(input.items).toBeUndefined();
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test('Tests for exclude', () => {
|
|
160
|
-
const options = [
|
|
161
|
-
{ id1: 1, name: 'a' },
|
|
162
|
-
{ id1: 2, name: 'b' }
|
|
163
|
-
];
|
|
164
|
-
const result = Utils.exclude(options, 'id1', 1);
|
|
165
|
-
expect(result.length).toBe(1);
|
|
166
|
-
expect(result[0].id1).toBe(2);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
test('Tests for formatInitial', () => {
|
|
170
|
-
expect(Utils.formatInitial('HelloWorld')).toBe('helloWorld');
|
|
171
|
-
expect('HelloWorld'.formatInitial(false)).toBe('helloWorld');
|
|
172
|
-
expect('hello'.formatInitial(true)).toBe('Hello');
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('Tests for formatString', () => {
|
|
176
|
-
const template = '{0} is first item, {1} is second item, {0} repeat';
|
|
177
|
-
const result = 'aa is first item, bb is second item, aa repeat';
|
|
178
|
-
expect(Utils.formatString(template, 'aa', 'bb')).toBe(result);
|
|
179
|
-
expect(template.format('aa', 'bb')).toBe(result);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
test('Tests for hideData', () => {
|
|
183
|
-
expect('xz@etsoo.com'.hideEmail()).toBe('x***@etsoo.com');
|
|
184
|
-
expect('info@etsoo.com'.hideEmail()).toBe('in***@etsoo.com');
|
|
185
|
-
expect('info@etsoo.com'.hideData('@')).toBe('in***@etsoo.com');
|
|
186
|
-
expect('12345678'.hideData()).toBe('123***678');
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
test('Tests for isDigits', () => {
|
|
190
|
-
expect(Utils.isDigits('1')).toBeTruthy();
|
|
191
|
-
expect(Utils.isDigits('12', 3)).toBeFalsy();
|
|
192
|
-
expect(Utils.isDigits('123', 3)).toBeTruthy();
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test('Tests for isEmail', () => {
|
|
196
|
-
expect(Utils.isEmail('abc')).toBeFalsy();
|
|
197
|
-
expect(Utils.isEmail('a@')).toBeFalsy();
|
|
198
|
-
expect(Utils.isEmail('xz@etsoo.com')).toBeTruthy();
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test('Tests for joinItems', () => {
|
|
202
|
-
expect(Utils.joinItems(['a', undefined, ' b', '', 'c '], ',')).toBe(
|
|
203
|
-
'a,b,c'
|
|
1
|
+
import { Utils } from "../src/Utils";
|
|
2
|
+
|
|
3
|
+
test("Tests for addBlankItem", () => {
|
|
4
|
+
const options = [
|
|
5
|
+
{ id: 1, name: "a" },
|
|
6
|
+
{ id: 2, name: "b" }
|
|
7
|
+
];
|
|
8
|
+
Utils.addBlankItem(options, "id", "name");
|
|
9
|
+
expect(options.length).toBe(3);
|
|
10
|
+
expect(options[0].id).toBe("");
|
|
11
|
+
expect(options[0].name).toBe("---");
|
|
12
|
+
Utils.addBlankItem(options, "id", "name");
|
|
13
|
+
expect(options.length).toBe(3);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("Tests for addUrlParam", () => {
|
|
17
|
+
const url = "https://www.etsoo.com";
|
|
18
|
+
const result = url.addUrlParam("a", "b");
|
|
19
|
+
expect(result).toBe("https://www.etsoo.com/?a=b");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("Tests for addUrlParams", () => {
|
|
23
|
+
const url = "https://www.etsoo.com";
|
|
24
|
+
const data = {
|
|
25
|
+
a: "a",
|
|
26
|
+
b: false,
|
|
27
|
+
c: 123,
|
|
28
|
+
d: new Date(Date.UTC(2022, 0, 28, 10)),
|
|
29
|
+
e: [1, 2],
|
|
30
|
+
f: ["a", "b"],
|
|
31
|
+
g: null
|
|
32
|
+
};
|
|
33
|
+
const result1 = url.addUrlParams(data);
|
|
34
|
+
|
|
35
|
+
test("addUrlParams", () => {
|
|
36
|
+
expect(result1).toBe(
|
|
37
|
+
"https://www.etsoo.com/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g="
|
|
204
38
|
);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
test('Tests for newGUID', () => {
|
|
208
|
-
// Arrange
|
|
209
|
-
const id1 = Utils.newGUID();
|
|
210
|
-
const id2 = Utils.newGUID();
|
|
39
|
+
});
|
|
211
40
|
|
|
212
|
-
|
|
213
|
-
expect(id1).not.toEqual(id2);
|
|
214
|
-
expect(id1.length).toBe(id2.length);
|
|
215
|
-
});
|
|
41
|
+
const result2 = url.addUrlParams(data, true);
|
|
216
42
|
|
|
217
|
-
test(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
});
|
|
43
|
+
test("addUrlParams with array format", () => {
|
|
44
|
+
expect(result2).toBe(
|
|
45
|
+
"https://www.etsoo.com/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1%2C2&f=a%2Cb&g="
|
|
46
|
+
);
|
|
47
|
+
});
|
|
223
48
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
49
|
+
const result22 = "/".addUrlParams(data);
|
|
50
|
+
test("addUrlParams with relative path", () => {
|
|
51
|
+
expect(result22).toBe(
|
|
52
|
+
"/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g="
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
global.URL = undefined as any;
|
|
57
|
+
|
|
58
|
+
const result3 = url.addUrlParams(data);
|
|
59
|
+
|
|
60
|
+
test("addUrlParams with traditional way", () => {
|
|
61
|
+
expect(result3).toBe(result1);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const result4 = url.addUrlParams(data, true);
|
|
65
|
+
|
|
66
|
+
test("addUrlParams with traditional way and array format", () => {
|
|
67
|
+
expect(result4).toBe(result2);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("Tests for containChinese", () => {
|
|
72
|
+
expect("123 abC".containChinese()).toBeFalsy();
|
|
73
|
+
expect("亿速思维".containChinese()).toBeTruthy();
|
|
74
|
+
expect("亿速Etsoo".containChinese()).toBeTruthy();
|
|
75
|
+
expect("김 민수".containKorean()).toBeTruthy();
|
|
76
|
+
expect("ぁ-ん".containJapanese()).toBeTruthy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("Tests for correctTypes", () => {
|
|
80
|
+
const input = {
|
|
81
|
+
id: "1",
|
|
82
|
+
ignore: "2",
|
|
83
|
+
price: "6.0",
|
|
84
|
+
amount: "",
|
|
85
|
+
date: "2022/01/28",
|
|
86
|
+
enabled: "true",
|
|
87
|
+
ids: ["1", "2"]
|
|
88
|
+
};
|
|
89
|
+
Utils.correctTypes(input, {
|
|
90
|
+
id: "number",
|
|
91
|
+
price: "number",
|
|
92
|
+
amount: "number",
|
|
93
|
+
date: "date",
|
|
94
|
+
enabled: "boolean",
|
|
95
|
+
ids: "number[]"
|
|
96
|
+
});
|
|
97
|
+
expect(typeof input.id).toBe("number");
|
|
98
|
+
expect(typeof input.price).toBe("number");
|
|
99
|
+
expect(input.amount).toBeUndefined();
|
|
100
|
+
expect((input.date as any) instanceof Date ? true : false).toBeTruthy();
|
|
101
|
+
expect(input.enabled).toBeTruthy();
|
|
102
|
+
expect(input.ids).toStrictEqual([1, 2]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("Tests for getDataChanges", () => {
|
|
106
|
+
const input = {
|
|
107
|
+
id: 1,
|
|
108
|
+
name: "Name",
|
|
109
|
+
gender: "F",
|
|
110
|
+
brand: "",
|
|
111
|
+
price: "6.0",
|
|
112
|
+
amount: "",
|
|
113
|
+
enabled: true,
|
|
114
|
+
value: undefined,
|
|
115
|
+
date: new Date("2023/03/18"),
|
|
116
|
+
ids: [1, 2],
|
|
117
|
+
data: { d1: 1, d2: false, d3: 1.2, d4: "Hello" }
|
|
118
|
+
};
|
|
119
|
+
const initData = {
|
|
120
|
+
id: 1,
|
|
121
|
+
name: "Name",
|
|
122
|
+
gender: "M",
|
|
123
|
+
brand: "ETSOO",
|
|
124
|
+
price: 6,
|
|
125
|
+
amount: 0,
|
|
126
|
+
date: "2023/03/18",
|
|
127
|
+
enabled: true,
|
|
128
|
+
ids: [1, 2],
|
|
129
|
+
data: { d1: 1, d3: 1.2, d4: "Hello", d2: false }
|
|
130
|
+
};
|
|
131
|
+
const fields = Utils.getDataChanges(input, initData);
|
|
132
|
+
expect(fields).toStrictEqual(["gender", "brand", "amount"]);
|
|
133
|
+
expect(input.price).toBeUndefined();
|
|
134
|
+
expect(input.amount).toBeUndefined();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("Tests for object array getDataChanges", () => {
|
|
138
|
+
const input = {
|
|
139
|
+
id: 1,
|
|
140
|
+
ids: [1, 2],
|
|
141
|
+
items: [
|
|
142
|
+
{ id: 1, label: "a" },
|
|
143
|
+
{ id: 2, label: "b" }
|
|
144
|
+
]
|
|
145
|
+
};
|
|
146
|
+
const initData = {
|
|
147
|
+
id: 1,
|
|
148
|
+
ids: [1],
|
|
149
|
+
items: [
|
|
150
|
+
{ id: 1, label: "a" },
|
|
151
|
+
{ id: 2, label: "b" }
|
|
152
|
+
]
|
|
153
|
+
};
|
|
154
|
+
const fields = Utils.getDataChanges(input, initData);
|
|
155
|
+
expect(fields).toStrictEqual(["ids"]);
|
|
156
|
+
expect(input.items).toBeUndefined();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("Tests for exclude", () => {
|
|
160
|
+
const options = [
|
|
161
|
+
{ id1: 1, name: "a" },
|
|
162
|
+
{ id1: 2, name: "b" }
|
|
163
|
+
];
|
|
164
|
+
const result = Utils.exclude(options, "id1", 1);
|
|
165
|
+
expect(result.length).toBe(1);
|
|
166
|
+
expect(result[0].id1).toBe(2);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("Tests for formatInitial", () => {
|
|
170
|
+
expect(Utils.formatInitial("HelloWorld")).toBe("helloWorld");
|
|
171
|
+
expect("HelloWorld".formatInitial(false)).toBe("helloWorld");
|
|
172
|
+
expect("hello".formatInitial(true)).toBe("Hello");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("Tests for formatString", () => {
|
|
176
|
+
const template = "{0} is first item, {1} is second item, {0} repeat";
|
|
177
|
+
const result = "aa is first item, bb is second item, aa repeat";
|
|
178
|
+
expect(Utils.formatString(template, "aa", "bb")).toBe(result);
|
|
179
|
+
expect(template.format("aa", "bb")).toBe(result);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("Tests for hideData", () => {
|
|
183
|
+
expect("xz@etsoo.com".hideEmail()).toBe("x***@etsoo.com");
|
|
184
|
+
expect("info@etsoo.com".hideEmail()).toBe("in***@etsoo.com");
|
|
185
|
+
expect("info@etsoo.com".hideData("@")).toBe("in***@etsoo.com");
|
|
186
|
+
expect("12345678".hideData()).toBe("123***678");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("Tests for isDigits", () => {
|
|
190
|
+
expect(Utils.isDigits("1")).toBeTruthy();
|
|
191
|
+
expect(Utils.isDigits("12", 3)).toBeFalsy();
|
|
192
|
+
expect(Utils.isDigits("123", 3)).toBeTruthy();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("Tests for isEmail", () => {
|
|
196
|
+
expect(Utils.isEmail("abc")).toBeFalsy();
|
|
197
|
+
expect(Utils.isEmail("a@")).toBeFalsy();
|
|
198
|
+
expect(Utils.isEmail("xz@etsoo.com")).toBeTruthy();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("Tests for joinItems", () => {
|
|
202
|
+
expect(Utils.joinItems(["a", undefined, " b", "", "c "], ",")).toBe("a,b,c");
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("Tests for newGUID", () => {
|
|
206
|
+
// Arrange
|
|
207
|
+
const id1 = Utils.newGUID();
|
|
208
|
+
const id2 = Utils.newGUID();
|
|
209
|
+
|
|
210
|
+
// Assert
|
|
211
|
+
expect(id1).not.toEqual(id2);
|
|
212
|
+
expect(id1.length).toBe(id2.length);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("Tests for numberToChars and charsToNumber", () => {
|
|
216
|
+
const num = 1638777042242;
|
|
217
|
+
const chars = Utils.numberToChars(num);
|
|
218
|
+
expect(chars).toEqual("QmpkdVgv");
|
|
219
|
+
expect(Utils.charsToNumber(chars)).toEqual(num);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("Tests for removeNonLetters", () => {
|
|
223
|
+
const input = "1234-5678@abc.";
|
|
224
|
+
const result = "12345678abc";
|
|
225
|
+
expect(Utils.removeNonLetters(input)).toBe(result);
|
|
226
|
+
expect(input.removeNonLetters()).toBe(result);
|
|
229
227
|
});
|
|
230
228
|
|
|
231
|
-
test(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
229
|
+
test("Tests for replaceNullOrEmpty", () => {
|
|
230
|
+
expect(Utils.replaceNullOrEmpty("a", "s")).toBe("a");
|
|
231
|
+
expect(Utils.replaceNullOrEmpty(null, "s")).toBe("s");
|
|
232
|
+
expect(Utils.replaceNullOrEmpty(" ", "s")).toBe("s");
|
|
235
233
|
});
|
|
236
234
|
|
|
237
|
-
test(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
235
|
+
test("Tests for objectEqual", () => {
|
|
236
|
+
const obj1 = { a: 1, b: "abc", c: true, d: null, f: [1, 2] };
|
|
237
|
+
const obj2 = { a: "1", b: "abc", c: true, f: [1, 2] };
|
|
238
|
+
expect(Utils.objectEqual(obj1, obj2)).toBeFalsy();
|
|
239
|
+
expect(Utils.objectEqual(obj1, obj2, [], 0)).toBeTruthy();
|
|
240
|
+
expect(Utils.objectEqual(obj1, obj2, ["a"])).toBeTruthy();
|
|
241
|
+
expect(Utils.objectEqual(obj1, obj2, ["a"], 2)).toBeFalsy();
|
|
244
242
|
});
|
|
245
243
|
|
|
246
|
-
test(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
244
|
+
test("Tests for parseJsonArray", () => {
|
|
245
|
+
expect(Utils.parseJsonArray('[1, 3, "a"]', 0)).toBeUndefined();
|
|
246
|
+
expect(Utils.parseJsonArray('[1, 3, "a"]')).not.toBeUndefined();
|
|
247
|
+
expect(Utils.parseJsonArray("1, 3, 9")).not.toBeUndefined();
|
|
250
248
|
});
|
|
251
249
|
|
|
252
|
-
test(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
250
|
+
test("Tests for objectUpdated", () => {
|
|
251
|
+
const objPrev = { a: 1, b: "abc", c: true, d: null, f: [1, 2] };
|
|
252
|
+
const objNew = { a: 2, b: "abc", d: new Date(), f: [1, 2, 3], g: true };
|
|
253
|
+
const fields = Utils.objectUpdated(objNew, objPrev, ["d"]);
|
|
254
|
+
expect(fields.sort()).toStrictEqual(["a", "c", "f", "g"]);
|
|
257
255
|
});
|
|
258
256
|
|
|
259
|
-
test(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
257
|
+
test("Tests for parseString", () => {
|
|
258
|
+
expect(Utils.parseString<string>("test")).toBe("test");
|
|
259
|
+
expect(Utils.parseString("test", "")).toBe("test");
|
|
260
|
+
expect(Utils.parseString("true", false)).toBe(true);
|
|
261
|
+
expect(Utils.parseString("", false)).toBeFalsy();
|
|
262
|
+
expect(Utils.parseString<boolean>("")).toBeUndefined();
|
|
263
|
+
expect(Utils.parseString<number>(undefined)).toBeUndefined();
|
|
264
|
+
expect(Utils.parseString("3.14", 0)).toBe(3.14);
|
|
265
|
+
expect(Utils.parseString(null, 0)).toBe(0);
|
|
266
|
+
expect(Utils.parseString("2021/4/13", new Date())).toStrictEqual(
|
|
267
|
+
new Date("2021/4/13")
|
|
268
|
+
);
|
|
271
269
|
});
|
|
272
270
|
|
|
273
|
-
test(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
271
|
+
test("Test for setLabels", () => {
|
|
272
|
+
// Arrange
|
|
273
|
+
const source = { label: "Hello" };
|
|
274
|
+
const newLabel = "world";
|
|
277
275
|
|
|
278
|
-
|
|
279
|
-
|
|
276
|
+
// Act
|
|
277
|
+
Utils.setLabels(source, { label1: newLabel }, { label: "label1" });
|
|
280
278
|
|
|
281
|
-
|
|
282
|
-
|
|
279
|
+
// Assert
|
|
280
|
+
expect(source.label).toBe(newLabel);
|
|
283
281
|
});
|
|
284
282
|
|
|
285
|
-
test(
|
|
286
|
-
|
|
287
|
-
|
|
283
|
+
test("Test for snakeNameToWord", () => {
|
|
284
|
+
expect(Utils.snakeNameToWord("snake_name")).toBe("Snake Name");
|
|
285
|
+
expect(Utils.snakeNameToWord("snake_name", true)).toBe("Snake name");
|
|
288
286
|
});
|
|
289
287
|
|
|
290
|
-
test(
|
|
291
|
-
|
|
288
|
+
test("Tests for mergeClasses", () => {
|
|
289
|
+
expect(Utils.mergeClasses("a", "", "b ", undefined, "c")).toBe("a b c");
|
|
292
290
|
});
|
|
293
291
|
|
|
294
|
-
test(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
292
|
+
test("Tests for getNestedValue", () => {
|
|
293
|
+
const obj = { jsonData: { photoSize: [200, 100], supportResizing: true } };
|
|
294
|
+
expect(Utils.getNestedValue(obj, "jsonData.supportResizing")).toBeTruthy();
|
|
295
|
+
expect(
|
|
296
|
+
Array.isArray(Utils.getNestedValue(obj, "jsonData.photoSize"))
|
|
297
|
+
).toBeTruthy();
|
|
298
|
+
expect(Utils.getNestedValue(obj, "jsonData.unknown")).toBeUndefined();
|
|
301
299
|
});
|
|
302
300
|
|
|
303
|
-
test(
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
301
|
+
test("Tests for getResult", () => {
|
|
302
|
+
// Arrange
|
|
303
|
+
type test = ((visible: boolean) => number) | number;
|
|
304
|
+
const input: test = (visible) => (visible ? 1 : 0);
|
|
305
|
+
const inputNumber: test = 5;
|
|
308
306
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
307
|
+
// Act & assert
|
|
308
|
+
const result = Utils.getResult(input, true);
|
|
309
|
+
expect(result).toBe(1);
|
|
310
|
+
expect(Utils.getResult(input, false)).toBe(0);
|
|
313
311
|
|
|
314
|
-
|
|
315
|
-
|
|
312
|
+
const valueResult = Utils.getResult(inputNumber);
|
|
313
|
+
expect(valueResult).toBe(5);
|
|
316
314
|
});
|
|
317
315
|
|
|
318
|
-
test(
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
316
|
+
test("Tests for parsePath, file only", () => {
|
|
317
|
+
const result = Utils.parsePath("a.jpg");
|
|
318
|
+
expect(result.root).toBe("");
|
|
319
|
+
expect(result.dir).toBe("");
|
|
320
|
+
expect(result.base).toBe("a.jpg");
|
|
321
|
+
expect(result.ext).toBe(".jpg");
|
|
322
|
+
expect(result.name).toBe("a");
|
|
325
323
|
});
|
|
326
324
|
|
|
327
|
-
test(
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
325
|
+
test("Tests for parsePath, root file only", () => {
|
|
326
|
+
const result = Utils.parsePath("/a.JPG");
|
|
327
|
+
expect(result.root).toBe("/");
|
|
328
|
+
expect(result.dir).toBe("/");
|
|
329
|
+
expect(result.base).toBe("a.JPG");
|
|
330
|
+
expect(result.ext).toBe(".JPG");
|
|
331
|
+
expect(result.name).toBe("a");
|
|
334
332
|
});
|
|
335
333
|
|
|
336
|
-
test(
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
334
|
+
test("Tests for parsePath, Linux path", () => {
|
|
335
|
+
const result = Utils.parsePath("/home/user/dir/file.txt");
|
|
336
|
+
expect(result.root).toBe("/");
|
|
337
|
+
expect(result.dir).toBe("/home/user/dir");
|
|
338
|
+
expect(result.base).toBe("file.txt");
|
|
339
|
+
expect(result.ext).toBe(".txt");
|
|
340
|
+
expect(result.name).toBe("file");
|
|
343
341
|
});
|
|
344
342
|
|
|
345
|
-
test(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
343
|
+
test("Tests for parsePath, Windows path", () => {
|
|
344
|
+
const result = Utils.parsePath("C:\\path\\dir\\file.txt");
|
|
345
|
+
expect(result.root).toBe("C:\\");
|
|
346
|
+
expect(result.dir).toBe("C:\\path\\dir");
|
|
347
|
+
expect(result.base).toBe("file.txt");
|
|
348
|
+
expect(result.ext).toBe(".txt");
|
|
349
|
+
expect(result.name).toBe("file");
|
|
352
350
|
});
|
|
353
351
|
|
|
354
|
-
test(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
352
|
+
test("Tests for removeEmptyValues", () => {
|
|
353
|
+
const obj = { a: 1, b: "", c: null, d: undefined, e: "e" };
|
|
354
|
+
Utils.removeEmptyValues(obj);
|
|
355
|
+
expect(obj).toEqual({ a: 1, e: "e" });
|
|
358
356
|
});
|
|
359
357
|
|
|
360
|
-
test(
|
|
361
|
-
|
|
358
|
+
test("Tests for setNestedValue", () => {
|
|
359
|
+
const obj = { jsonData: { photoSize: [200, 100], supportResizing: true } };
|
|
362
360
|
|
|
363
|
-
|
|
364
|
-
|
|
361
|
+
Utils.setNestedValue(obj, "jsonData.supportResizing", false);
|
|
362
|
+
expect(obj.jsonData.supportResizing).toBeFalsy();
|
|
365
363
|
|
|
366
|
-
|
|
367
|
-
|
|
364
|
+
Utils.setNestedValue(obj, "jsonData.newProperty.value", 125);
|
|
365
|
+
expect(Reflect.get((obj.jsonData as any).newProperty, "value")).toBe(125);
|
|
368
366
|
});
|
|
369
367
|
|
|
370
|
-
test(
|
|
371
|
-
|
|
368
|
+
test("Tests for setNestedValue removal", () => {
|
|
369
|
+
const obj = { jsonData: { photoSize: [200, 100], supportResizing: true } };
|
|
372
370
|
|
|
373
|
-
|
|
374
|
-
|
|
371
|
+
Utils.setNestedValue(obj, "jsonData.photoSize", undefined);
|
|
372
|
+
expect(obj.jsonData.photoSize).toBeUndefined();
|
|
375
373
|
|
|
376
|
-
|
|
377
|
-
|
|
374
|
+
Utils.setNestedValue(obj, "jsonData.supportResizing", undefined);
|
|
375
|
+
expect(obj.jsonData.supportResizing).toBeUndefined();
|
|
378
376
|
});
|
|
379
377
|
|
|
380
|
-
test(
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
378
|
+
test("Tests for sortByFavor", () => {
|
|
379
|
+
const items = [1, 2, 3, 4, 5, 6, 7];
|
|
380
|
+
expect(Utils.sortByFavor(items, [5, 1, 3])).toStrictEqual([
|
|
381
|
+
5, 1, 3, 2, 4, 6, 7
|
|
382
|
+
]);
|
|
385
383
|
});
|
|
386
384
|
|
|
387
|
-
test(
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
385
|
+
test("Tests for sortByFieldFavor", () => {
|
|
386
|
+
const options = [
|
|
387
|
+
{ id: "a", name: "a1" },
|
|
388
|
+
{ id: "b", name: "b2" },
|
|
389
|
+
{ id: "c", name: "c3" },
|
|
390
|
+
{ id: "d", name: "d4" },
|
|
391
|
+
{ id: "e", name: "e5" },
|
|
392
|
+
{ id: "f", name: "f6" }
|
|
393
|
+
];
|
|
394
|
+
expect(
|
|
395
|
+
Utils.sortByFieldFavor(options, "id", ["e", "a", "c"]).map(
|
|
396
|
+
(option) => option.name
|
|
397
|
+
)
|
|
398
|
+
).toStrictEqual(["e5", "a1", "c3", "b2", "d4", "f6"]);
|
|
401
399
|
});
|
|
402
400
|
|
|
403
|
-
test(
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
401
|
+
test("Tests for trim", () => {
|
|
402
|
+
expect(Utils.trim("//a/", "/")).toBe("a");
|
|
403
|
+
expect(Utils.trim("/*/a/", ...["/", "*"])).toBe("a");
|
|
404
|
+
expect(Utils.trim("abc", ...["/", "*"])).toBe("abc");
|
|
407
405
|
});
|
|
408
406
|
|
|
409
|
-
test(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
407
|
+
test("Tests for trimStart", () => {
|
|
408
|
+
expect(Utils.trimStart("//a/", "/")).toBe("a/");
|
|
409
|
+
expect(Utils.trimStart("/*/a/", ...["/", "*"])).toBe("a/");
|
|
410
|
+
expect(Utils.trimStart("abc", ...["/", "*"])).toBe("abc");
|
|
413
411
|
});
|
|
414
412
|
|
|
415
|
-
test(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
413
|
+
test("Tests for trimEnd", () => {
|
|
414
|
+
expect(Utils.trimEnd("//a/", "/")).toBe("//a");
|
|
415
|
+
expect(Utils.trimEnd("/*/a*/", ...["/", "*"])).toBe("/*/a");
|
|
416
|
+
expect(Utils.trimEnd("abc", ...["/", "*"])).toBe("abc");
|
|
417
|
+
expect(Utils.trimEnd("12.0.0.0", ".0")).toBe("12");
|
|
420
418
|
});
|