This repository has been archived by the owner on Feb 28, 2021. It is now read-only.
/
index.js
111 lines (96 loc) · 3.57 KB
/
index.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
106
107
108
109
110
111
'use strict';
const undef = require('ifnotundef');
/**
*
* [![GitHub version](https://badge.fury.io/gh/pouc%2Farr-equal.svg)](https://badge.fury.io/gh/pouc%2Farr-equal)
* [![npm version](https://badge.fury.io/js/arr-equal.svg)](https://badge.fury.io/js/arr-equal)
* [![NPM monthly downloads](https://img.shields.io/npm/dm/arr-equal.svg?style=flat)](https://npmjs.org/package/arr-equal)
* [![Build Status](https://travis-ci.org/pouc/arr-equal.svg?branch=master)](https://travis-ci.org/pouc/arr-equal)
* [![Dependency Status](https://gemnasium.com/badges/github.com/pouc/arr-equal.svg)](https://gemnasium.com/github.com/pouc/arr-equal)
* [![Coverage Status](https://coveralls.io/repos/github/pouc/arr-equal/badge.svg?branch=master)](https://coveralls.io/github/pouc/arr-equal?branch=master)
* [![Known Vulnerabilities](https://snyk.io/test/github/pouc/arr-equal/badge.svg)](https://snyk.io/test/github/pouc/arr-equal)
*
* Check if two arrays are equal with options (deep? shallow? ...)
*
* Deep comparison compares arrays recursively.
*
* Other type of objects (objets, buffers, ...) are still compared with strict equality (===).
*
* @module arr-equal
* @typicalname equal
* @author Loïc Formont
*
* @license MIT Licensed
*
* @example
* ```javascript
* var equal = require("arr-equal");
*
* equal([1, 2, 3], [1, 2, 3]) ==> true
*
* equal([1, 2, 3], [1, 3, 2]) ==> false
* equal([1, 2, 3], [1, 3, 2], {order: false}) ==> true
*
* equal([1, [2, 3]], [1, [2, 3]]) ==> false
* equal([1, [2, 3]], [1, [2, 3]], {deep: true}) ==> true
* ```
*/
module.exports = function(arr1, arr2, options) {
const order = undef.child(options, 'order', true);
const deep = undef.child(options, 'deep', false);
if (!Array.isArray(arr1)) return false;
if (!Array.isArray(arr2)) return false;
if (arr1 === arr2) return true;
if (arr1.length != arr2.length) return false;
// If the order matters
if (order) {
for (var i = 0; i < arr1.length; i++) {
if (!deep && arr1[i] !== arr2[i]) {
return false;
} else if (deep) {
if (Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
if (!module.exports(arr1[i], arr2[i], options)) {
return false;
}
} else if (arr1[i] !== arr2[i]) {
return false;
}
}
}
return true;
// If the order doesn't matter
} else if (!order) {
var foundMap = {};
for (var i = 0; i < arr1.length; i++) {
var found = false;
for (var j = 0; j < arr2.length; j++) {
if (undef.check(foundMap[j])) {
continue;
}
if (!deep && arr1[i] === arr2[j]) {
foundMap[j] = arr2[j];
found = true;
break;
} else if (deep) {
if (Array.isArray(arr1[i]) && Array.isArray(arr2[j])) {
if (module.exports(arr1[i], arr2[j], options)) {
foundMap[j] = arr2[j];
found = true;
break;
}
} else if (arr1[i] === arr2[j]) {
foundMap[j] = arr2[j];
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
return true;
}
// Should not happen
return false;
};