sameValueZero

说明

根据 SameValueZero 策略判断两个值是否相等

源码

用法

sameValueZero.test.ts
1
import sameValueZero from "../../utility/others/sameValueZero";
2
3
4
describe('sameValueZero tests()...', () => {
5
test('sameValueZero should compare the value that passed', () => {
6
let a = Symbol('a');
7
let b = Symbol('b');
8
let c = [1, 2, 3];
9
let d = { a, b, c };
10
11
const received = [
12
{
13
a: 10,
14
b: 'duan',
15
},
16
{
17
a: undefined,
18
b: undefined,
19
},
20
{
21
a: null,
22
b: null,
23
},
24
{
25
a: NaN,
26
b: NaN,
27
},
28
{
29
a: +0,
30
b: -0,
31
},
32
{
33
a: -0,
34
b: +0,
35
},
36
{
37
a: 999,
38
b: 999,
39
},
40
{
41
a: 99999,
42
b: 11111,
43
},
44
{
45
a: 'duan',
46
b: 'duan',
47
},
48
{
49
a: 'duan',
50
b: 'duanzhaoyang',
51
},
52
{
53
a: true,
54
b: true,
55
},
56
{
57
a: true,
58
b: false,
59
},
60
{
61
a: a,
62
b: b,
63
},
64
{
65
a: a,
66
b: a,
67
},
68
{
69
a: c,
70
b: d,
71
},
72
{
73
a: c,
74
b: c,
75
},
76
];
77
const expected = [
78
false,
79
true,
80
true,
81
true,
82
true,
83
true,
84
true,
85
false,
86
true,
87
false,
88
true,
89
false,
90
false,
91
true,
92
false,
93
true
94
];
95
const result = received.map((v) => {
96
return sameValueZero(v.a, v.b);
97
});
98
99
result.forEach((v, i) => {
100
expect(v).toBe(expected[i]);
101
});
102
});
103
});
Copied!
最近更新 9mo ago
复制链接