/
eq.js
105 lines (95 loc) · 2.34 KB
/
eq.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
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
'use strict';
var O, isArray, isPrimitive;
if (require) {
O = require('./object');
isArray = O.isArray;
isPrimitive = O.isPrimitive;
}
/**
* format function.toString()
* @param {string} expr String of function.
* @return {string} format function expression.
*/
function replaceSpaces(expr) {
return expr.replace(/\s*\{\s*/,
' { ').replace(/\s*\}\s*/,
' }').replace(/\s*,\s*/,
', ').replace(/\s*;\s*/,
'; ').replace(/\s+/, ' ');
}
/**
* @param {function(...*):*} a function object.
* @param {function(...*):*} b function object.
* @return {boolean} a & b is same meaning or not.
*/
function isSameFunction(a, b) {
if (a === b) {
return true;
}
if (a.name !== b.name || a.length !== b.length) {
return false;
}
var as = replaceSpaces(a.toString()),
bs = replaceSpaces(b.toString());
return as === bs;
}
/**
* @param {*} a something object.
* @param {*} b something object.
* @return {boolean} a & b are same object.
*/
function deepEq(a, b) {
var i, l, a_ps, b_ps;
// Same identifier, or alias
if (a === b) {
return true;
}
// Primitive
if (isPrimitive(a) && isPrimitive(b)) {
return a === b;
}
// Array
if (isArray(a) && isArray(b)) {
if (a.length === b.length) {
for (i = 0, l = a.length; i < l; i++) {
if (!deepEq(a[i], b[i])) {
return false;
}
}
return true;
}
return false;
}
// Function
if (typeof a === 'function' && typeof b === 'function') {
return isSameFunction(a, b);
}
// Object
if (a.constructor === b.constructor) {
a_ps = Object.getOwnPropertyNames(a);
b_ps = Object.getOwnPropertyNames(b);
if (a_ps.length !== b_ps.length) {
return false;
}
for (i = 0, l = a_ps.length; i < l; i++) {
if (!deepEq(a[a_ps[i]], b[b_ps[i]])) {
return false;
}
}
return true;
}
return false;
}
if (typeof exports !== 'undefined') {
/** @type {function(*):boolean} */
exports.isArray = isArray;
/** @type {function(*):boolean} */
exports.isPrimitive = isPrimitive;
/** @type {function(*):replaceSpaces} */
exports.replaceSpaces = replaceSpaces;
/** @type {function(function(...*):*, function(...*):*): boolean} */
exports.isSameFunction = isSameFunction;
/** @type {function(*,*):boolean} */
exports.deepEq = deepEq;
}
// EOF