deno.land / x / rambda@v9.1.1 / source / equals.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { isArray } from './_internals/isArray.js'import { type } from './type.js'
export function _lastIndexOf(valueToFind, list){ if (!isArray(list)) throw new Error(`Cannot read property 'indexOf' of ${ list }`)
const typeOfValue = type(valueToFind) if (![ 'Array', 'NaN', 'Object', 'RegExp' ].includes(typeOfValue)) return list.lastIndexOf(valueToFind)
const { length } = list let index = length let foundIndex = -1
while (--index > -1 && foundIndex === -1) if (equals(list[ index ], valueToFind)) foundIndex = index
return foundIndex}
export function _indexOf(valueToFind, list){ if (!isArray(list)) throw new Error(`Cannot read property 'indexOf' of ${ list }`)
const typeOfValue = type(valueToFind) if (![ 'Array', 'NaN', 'Object', 'RegExp' ].includes(typeOfValue)) return list.indexOf(valueToFind)
let index = -1 let foundIndex = -1 const { length } = list
while (++index < length && foundIndex === -1) if (equals(list[ index ], valueToFind)) foundIndex = index
return foundIndex}
function _arrayFromIterator(iter){ const list = [] let next while (!(next = iter.next()).done) list.push(next.value)
return list}
function _compareSets(a, b){ if (a.size !== b.size) return false
const aList = _arrayFromIterator(a.values()) const bList = _arrayFromIterator(b.values())
const filtered = aList.filter(aInstance => _indexOf(aInstance, bList) === -1)
return filtered.length === 0}
function compareErrors(a, b){ if (a.message !== b.message) return false if (a.toString !== b.toString) return false
return a.toString() === b.toString()}
function parseDate(maybeDate){ if (!maybeDate.toDateString) return [ false ]
return [ true, maybeDate.getTime() ]}
function parseRegex(maybeRegex){ if (maybeRegex.constructor !== RegExp) return [ false ]
return [ true, maybeRegex.toString() ]}
export function equals(a, b){ if (arguments.length === 1) return _b => equals(a, _b)
if (Object.is(a, b)) return true
const aType = type(a)
if (aType !== type(b)) return false if (aType === 'Function') return a.name === undefined ? false : a.name === b.name
if ([ 'NaN', 'Null', 'Undefined' ].includes(aType)) return true
if ([ 'BigInt', 'Number' ].includes(aType)){ if (Object.is(-0, a) !== Object.is(-0, b)) return false
return a.toString() === b.toString() }
if ([ 'Boolean', 'String' ].includes(aType)) return a.toString() === b.toString()
if (aType === 'Array'){ const aClone = Array.from(a) const bClone = Array.from(b)
if (aClone.toString() !== bClone.toString()) return false
let loopArrayFlag = true aClone.forEach((aCloneInstance, aCloneIndex) => { if (loopArrayFlag) if ( aCloneInstance !== bClone[ aCloneIndex ] && !equals(aCloneInstance, bClone[ aCloneIndex ]) ) loopArrayFlag = false
})
return loopArrayFlag }
const aRegex = parseRegex(a) const bRegex = parseRegex(b)
if (aRegex[ 0 ]) return bRegex[ 0 ] ? aRegex[ 1 ] === bRegex[ 1 ] : false else if (bRegex[ 0 ]) return false
const aDate = parseDate(a) const bDate = parseDate(b)
if (aDate[ 0 ]) return bDate[ 0 ] ? aDate[ 1 ] === bDate[ 1 ] : false else if (bDate[ 0 ]) return false
if (a instanceof Error){ if (!(b instanceof Error)) return false
return compareErrors(a, b) }
if (aType === 'Set') return _compareSets(a, b)
if (aType === 'Object'){ const aKeys = Object.keys(a)
if (aKeys.length !== Object.keys(b).length) return false
let loopObjectFlag = true aKeys.forEach(aKeyInstance => { if (loopObjectFlag){ const aValue = a[ aKeyInstance ] const bValue = b[ aKeyInstance ]
if (aValue !== bValue && !equals(aValue, bValue)) loopObjectFlag = false
} })
return loopObjectFlag }
return false}
rambda

Version Info

Tagged at
2 months ago