]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/util/array.ts
LP1837478 Angular Catalog Recent Searches & Templates
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / util / array.ts
1
2 /* Utility code for arrays */
3
4 export class ArrayUtil {
5
6     // Returns true if the two arrays contain the same values as
7     // reported by the provided comparator function or ===
8     static equals(arr1: any[], arr2: any[],
9         comparator?: (a: any, b: any) => boolean): boolean {
10
11         if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
12             return false;
13         }
14
15         if (arr1 === arr2) {
16             // Same array
17             return true;
18         }
19
20         if (arr1.length !== arr2.length) {
21             return false;
22         }
23
24         for (let i = 0; i < arr1.length; i++) {
25             if (comparator) {
26                 if (!comparator(arr1[i], arr2[i])) {
27                     return false;
28                 }
29             } else {
30                 if (arr1[i] !== arr2[i]) {
31                     return false;
32                 }
33             }
34         }
35
36         return true;
37     }
38 }
39