UNPKG

1.87 kBJavaScriptView Raw
1/* tslint:disable */
2/**
3 * @name equals
4 *
5 * @description
6 * Determines if two objects or two values are equivalent.
7 *
8 * Two objects or values are considered equivalent if at least one of the following is true:
9 *
10 * * Both objects or values pass `===` comparison.
11 * * Both objects or values are of the same type and all of their properties are equal by
12 * comparing them with `equals`.
13 *
14 * @param {*} o1 Object or value to compare.
15 * @param {*} o2 Object or value to compare.
16 * @returns {boolean} True if arguments are equal.
17 */
18export function equals(o1, o2) {
19 if (o1 === o2)
20 return true;
21 if (o1 === null || o2 === null)
22 return false;
23 if (o1 !== o1 && o2 !== o2)
24 return true; // NaN === NaN
25 var t1 = typeof o1, t2 = typeof o2, length, key, keySet;
26 if (t1 == t2 && t1 == 'object') {
27 if (Array.isArray(o1)) {
28 if (!Array.isArray(o2))
29 return false;
30 if ((length = o1.length) == o2.length) {
31 for (key = 0; key < length; key++) {
32 if (!equals(o1[key], o2[key]))
33 return false;
34 }
35 return true;
36 }
37 }
38 else {
39 if (Array.isArray(o2)) {
40 return false;
41 }
42 keySet = Object.create(null);
43 for (key in o1) {
44 if (!equals(o1[key], o2[key])) {
45 return false;
46 }
47 keySet[key] = true;
48 }
49 for (key in o2) {
50 if (!(key in keySet) && typeof o2[key] !== 'undefined') {
51 return false;
52 }
53 }
54 return true;
55 }
56 }
57 return false;
58}
59/* tslint:enable */
60export function isDefined(value) {
61 return typeof value !== 'undefined' && value !== null;
62}