UNPKG

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