-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_deep_foundations_equality_resolved.js
78 lines (71 loc) · 2.92 KB
/
03_deep_foundations_equality_resolved.js
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
function findAll(match,arr) {
var ret = [];
for (let v of arr) {
if (Object.is(match,v)) {
ret.push(v);
}
else if (match == null && v == null) {
ret.push(v);
}
else if (typeof match == "boolean") {
if (match === v) {
ret.push(v);
}
}
else if (typeof match == "string" && match.trim() != "" && typeof v == "number" && !Object.is(-0,v)) {
if (match == v) {
ret.push(v);
}
}
else if (typeof match == "number" && !Object.is(match,-0) && !Object.is(match,NaN) && !Object.is(match,Infinity) && !Object.is(match,-Infinity) && typeof v == "string" && v.trim() != "") {
if (match == v) {
ret.push(v);
}
}
}
return ret;
}
// tests:
var myObj = { a: 2 };
var values = [
null, undefined, -0, 0, 13, 42, NaN, -Infinity, Infinity,
"", "0", "42", "42hello", "true", "NaN", true, false, myObj
];
console.log(setsMatch(findAll(null,values),[null,undefined]) === true);
console.log(setsMatch(findAll(undefined,values),[null,undefined]) === true);
console.log(setsMatch(findAll(0,values),[0,"0"]) === true);
console.log(setsMatch(findAll(-0,values),[-0]) === true);
console.log(setsMatch(findAll(13,values),[13]) === true);
console.log(setsMatch(findAll(42,values),[42,"42"]) === true);
console.log(setsMatch(findAll(NaN,values),[NaN]) === true);
console.log(setsMatch(findAll(-Infinity,values),[-Infinity]) === true);
console.log(setsMatch(findAll(Infinity,values),[Infinity]) === true);
console.log(setsMatch(findAll("",values),[""]) === true);
console.log(setsMatch(findAll("0",values),[0,"0"]) === true);
console.log(setsMatch(findAll("42",values),[42,"42"]) === true);
console.log(setsMatch(findAll("42hello",values),["42hello"]) === true);
console.log(setsMatch(findAll("true",values),["true"]) === true);
console.log(setsMatch(findAll(true,values),[true]) === true);
console.log(setsMatch(findAll(false,values),[false]) === true);
console.log(setsMatch(findAll(myObj,values),[myObj]) === true);
console.log(setsMatch(findAll(null,values),[null,0]) === false);
console.log(setsMatch(findAll(undefined,values),[NaN,0]) === false);
console.log(setsMatch(findAll(0,values),[0,-0]) === false);
console.log(setsMatch(findAll(42,values),[42,"42hello"]) === false);
console.log(setsMatch(findAll(25,values),[25]) === false);
console.log(setsMatch(findAll(Infinity,values),[Infinity,-Infinity]) === false);
console.log(setsMatch(findAll("",values),["",0]) === false);
console.log(setsMatch(findAll("false",values),[false]) === false);
console.log(setsMatch(findAll(true,values),[true,"true"]) === false);
console.log(setsMatch(findAll(true,values),[true,1]) === false);
console.log(setsMatch(findAll(false,values),[false,0]) === false);
// ***************************
function setsMatch(arr1,arr2) {
if (Array.isArray(arr1) && Array.isArray(arr2) && arr1.length == arr2.length) {
for (let v of arr1) {
if (!arr2.includes(v)) return false;
}
return true;
}
return false;
}