-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeply-equivalent.js
29 lines (27 loc) · 982 Bytes
/
deeply-equivalent.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
'use strict'
module.exports = deeplyEquivalent
const kindOf = require('./kind-of.js')
// deep equals, but can compare the values of dates, the contents of buffers
// and the values of numbers with the values of bigints.
function deeplyEquivalent (aa, bb) {
const aaKind = kindOf(aa)
const bbKind = kindOf(bb)
if (aaKind !== bbKind) return false
if (aaKind === 'array') {
if (aa.length !== bb.length) return false
return aa.every((_, ii) => deeplyEquivalent(_, bb[ii]))
} else if (aaKind === 'object') {
const aaKeys = Object.keys(aa)
const bbKeys = Object.keys(bb)
if (!deeplyEquivalent(aaKeys, bbKeys)) return false
return aaKeys.every(_ => deeplyEquivalent(aa[_], bb[_]))
} else if (aaKind === 'buffer') {
return aa.equals(bb)
} else if (aaKind === 'date') {
return aa.valueOf() === bb.valueOf()
} else {
// guaranteed the same kind already, this allows equivalent numbers and
// bigints to match
return aa == bb
}
}