-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathequal.spec.ts
322 lines (239 loc) · 9.85 KB
/
equal.spec.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {
isDate,
isEmpty,
isJSON,
isNotEmpty,
isIP,
isIPUrl,
objsComposeById,
objsComposeByCode,
isNumber, arrayCompose
} from './equal';
const testEmptyFn = (name: string) => `mr_${name}`;
const testClientFn = (name?: string) => {
// 試試看拿掉 ! 會不會判別錯誤
if (!isEmpty(name)) {
testEmptyFn(name);
const nickName: string = name; // 这里 TypeScript 应该能够推断 test 不是 null 或 undefined
}
// 試試看拿掉 ! 會不會判別錯誤
if (isNotEmpty(name)) {
testEmptyFn(name);
const nickName: string = name; // 这里 TypeScript 应该能够推断 test 不是 null 或 undefined
}
};
describe('isEmpty', () => {
it('should return a true for empty string', () => {
expect(isEmpty('')).toBeTruthy();
});
it('should return a true for empty object', () => {
expect(isEmpty({})).toBeTruthy();
});
it('should return a true for zero (isZero: true)', () => {
expect(isEmpty(0, {isZero: true})).toBeTruthy();
});
it('should return a true for string number (isZero: true)', () => {
expect(isEmpty('0', {isZero: true})).toBeTruthy();
});
it('should return a false for number (isZero: false)', () => {
expect(isEmpty(0, {isZero: false})).toBeFalsy();
});
it('should return a false for string number (isZero: false)', () => {
expect(isEmpty('0', {isZero: false})).toBeFalsy();
});
it('should return a true for string false (isFalse: false)', () => {
expect(isEmpty('false', {isFalse: true})).toBeTruthy();
});
it('should return a true for false (isFalse: false)', () => {
expect(isEmpty(false, {isFalse: true})).toBeTruthy();
});
it('should return a false for string false (isFalse: false)', () => {
expect(isEmpty('false', {isFalse: false})).toBeFalsy();
});
it('should return a false for false (isFalse: false)', () => {
expect(isEmpty(false, {isFalse: false})).toBeFalsy();
});
it('should return a true for empty array', () => {
expect(isEmpty([])).toBeTruthy();
});
it('should return a true for null', () => {
expect(isEmpty(null)).toBeTruthy();
});
it('should return a true for undefined', () => {
expect(isEmpty(undefined)).toBeTruthy();
});
it('should return a false for string', () => {
expect(isEmpty('helloWorld')).toBeFalsy();
});
});
describe('isNotEmpty', () => {
it('should return a false for empty string', () => {
expect(isNotEmpty('')).toBeFalsy();
});
it('should return a false for number zero (isZero: true)', () => {
expect(isNotEmpty(0, {isZero: true})).toBeFalsy();
});
it('should return a false for string zero (isZero: true)', () => {
expect(isNotEmpty('0', {isZero: true})).toBeFalsy();
});
it('should return a true for number zero (isZero: false)', () => {
expect(isNotEmpty(0, {isZero: false})).toBeTruthy();
});
it('should return a true for string zero (isZero: false)', () => {
expect(isNotEmpty('0', {isZero: false})).toBeTruthy();
});
it('should return a false for string false (isFalse: true)', () => {
expect(isNotEmpty('false', {isFalse: true})).toBeFalsy();
});
it('should return a false for false (isFalse: true)', () => {
expect(isNotEmpty(false, {isFalse: true})).toBeFalsy();
});
it('should return a true for string false (isFalse: false)', () => {
expect(isNotEmpty('false', {isFalse: false})).toBeTruthy();
});
it('should return a true for boolean false (isFalse: false)', () => {
expect(isNotEmpty(false, {isFalse: false})).toBeTruthy();
});
it('should return a false for empty array', () => {
expect(isNotEmpty([])).toBeFalsy();
});
it('should return a false for null', () => {
expect(isNotEmpty(null)).toBeFalsy();
});
it('should return a false for undefined', () => {
expect(isNotEmpty(undefined)).toBeFalsy();
});
it('should return a true for string', () => {
expect(isNotEmpty('helloWorld')).toBeTruthy();
});
});
describe('isDate', () => {
it('should return a true for - separator format date', () => {
expect(isDate('2022-01-09')).toBeTruthy();
});
it('should return a true for / separator format date', () => {
expect(isDate('2022/01/09')).toBeTruthy();
});
it('should return a true for . separator format date', () => {
expect(isDate('2022.01.09')).toBeTruthy();
});
it('should return a trfalseue for no separator format date', () => {
expect(isDate('20220109')).toBeFalsy();
});
it('should return a false for value exceeded date', () => {
expect(isDate('2022/13/40')).toBeFalsy();
});
it('should return a false for non-date format', () => {
expect(isDate('helloWorld')).toBeFalsy();
});
});
describe('isNumber', () => {
it('should return a true for number', () => {
// expect(isNumber('192.168')).toBeTruthy();
expect(isNumber(192.168)).toBeTruthy();
expect(isNumber('100')).toBeTruthy();
expect(isNumber(100)).toBeTruthy();
});
it('should return a false for number', () => {
expect(isNumber('192.168.223')).toBeFalsy();
expect(isNumber('100x')).toBeFalsy();
expect(isNumber('100e')).toBeFalsy();
expect(isNumber(undefined)).toBeFalsy();
expect(isNumber(null)).toBeFalsy();
expect(isNumber('')).toBeFalsy();
});
});
describe('isIp', () => {
it('should return a true for ip', () => {
expect(isIP('192.168.1.10')).toBeTruthy();
expect(isIP('1.168.11.11')).toBeTruthy();
});
it('should return a false for value exceeded date', () => {
expect(isIP('192.168.299.299')).toBeFalsy();
expect(isIP('a.d.12312')).toBeFalsy();
expect(isIP('2402:7500:a17:f8')).toBeFalsy();
expect(isIP('::1')).toBeFalsy();
});
it('should return a false for non-ip format', () => {
expect(isIP('http://58.180.27.75/img_item/2022/11/21/LEE18DRX22112112_154V.jpg')).toBeFalsy();
});
});
describe('isIPUrl', () => {
it('should return a true for ip url', () => {
expect(isIPUrl('http://58.180.27.75/img_item/2022/11/21/LEE18DRX22112112_154V.jpg')).toBeTruthy();
});
it('should return a false for non-ip url', () => {
expect(isIPUrl('http://www.google.com/img_item/2022/11/21/LEE18DRX22112112_154V.jpg')).toBeFalsy();
});
});
describe('isJSON', () => {
const jsonString = JSON.stringify({name: 'jack'});
it('should return a true for json string', () => {
expect(isJSON(jsonString)).toBeTruthy();
});
it('should return a false for non-json string', () => {
expect(isJSON('{name:_\'jack\'}')).toBeFalsy();
});
});
describe('objsComposeById', () => {
const prev1 = [{id: 1, name: 'imagine'}, {id: 2, name: 'jack'}];
const prev2 = [{id: 1, name: 'imagine'}, {id: 3, name: 'jack'}];
const next1 = [{id: 1, name: 'imagine'}, {id: 2, name: 'jack'}];
const next2 = [{id: 1, name: 'imagine'}, {id: 2, name: '___jack___'}];
it('should return a true for same id array object', () => {
expect(objsComposeById(prev1, next1)).toBeTruthy();
expect(objsComposeById(prev1, next2)).toBeTruthy();
});
it('should return a false for not same id array object', () => {
expect(objsComposeById(prev2, next1)).toBeFalsy();
});
});
describe('objsComposeByCode', () => {
const prev1 = [{code: 1, name: 'imagine'}, {code: 2, name: 'jack'}];
const prev2 = [{code: 1, name: 'imagine'}, {code: 3, name: 'jack'}];
const next1 = [{code: 1, name: 'imagine'}, {code: 2, name: 'jack'}];
const next2 = [{code: 1, name: 'imagine__'}, {code: 2, name: '___jack___'}];
it('should return a true for same id array object', () => {
expect(objsComposeByCode(prev1, next1)).toBeTruthy();
expect(objsComposeByCode(prev1, next2)).toBeTruthy();
});
it('should return a false for not same id array object', () => {
expect(objsComposeByCode(prev2, next1)).toBeFalsy();
});
});
describe('arrayCompose', () => {
test('should return true for arrays with the same elements in different orders', () => {
expect(arrayCompose([1, 2, 3], [3, 2, 1])).toBe(true);
});
test('should return false for arrays with different elements', () => {
expect(arrayCompose([1, 2, 3], [3, 2, 4])).toBe(false);
});
test('should return true for arrays with duplicate elements and the same frequencies', () => {
expect(arrayCompose([1, 2, 2, 3], [3, 2, 2, 1])).toBe(true);
});
test('should return false for arrays with duplicate elements but different frequencies', () => {
expect(arrayCompose([1, 2, 2], [2, 1, 1])).toBe(false);
});
test('should return true for empty arrays', () => {
expect(arrayCompose([], [])).toBe(true);
});
test('should return false when one array is empty and the other is not', () => {
expect(arrayCompose([1], [])).toBe(false);
expect(arrayCompose([], [1])).toBe(false);
});
test('should return true for arrays with mixed types but same values', () => {
expect(arrayCompose([1, "2", true], [true, "2", 1])).toBe(true);
});
test('should return false for arrays with objects (reference comparison)', () => {
expect(arrayCompose([{ a: 1 }], [{ a: 1 }])).toBe(false);
});
test('should handle special cases like NaN correctly', () => {
expect(arrayCompose([NaN], [NaN])).toBe(true);
});
test('should treat 0 and -0 as equal', () => {
expect(arrayCompose([0, -0], [-0, 0])).toBe(true);
});
test('should return true for arrays with null and undefined in different orders', () => {
expect(arrayCompose([null, undefined], [undefined, null])).toBe(true);
});
});