Skip to content

Commit cee4cea

Browse files
committed
fix: omit name property to compare functions to support on-the-fly arrow functions
1 parent b35491e commit cee4cea

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// //////////////////////////////////////////////////////////////////////////////// //
22
// MIT License
33
//
4-
// Copyright (c) 2018 Jan Küster
4+
// Copyright (c) 2018 - today Jan Küster
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -154,11 +154,12 @@ scope.Set.prototype.add =
154154
* @private
155155
*/
156156
function resolve (obj, circ = new originals.constructor([obj])) {
157-
if (typeof obj === 'undefined' ||
158-
typeof obj === 'string' ||
159-
typeof obj === 'number' ||
160-
typeof obj === 'boolean' ||
161-
obj === null) {
157+
const type = typeof obj
158+
159+
if (
160+
['undefined', 'string', 'number', 'boolean'].includes(type) ||
161+
obj === null
162+
) {
162163
return obj
163164
}
164165

@@ -167,15 +168,18 @@ function resolve (obj, circ = new originals.constructor([obj])) {
167168
obj = Array.from(obj)
168169
}
169170

170-
if (typeof obj === 'function') {
171+
if (type === 'function') {
171172
const fctObj = { fctStr: String(obj).replace(/\s+/g, '') } // function body to string
172173
// resolve all function properties / attached references
173-
fctObj.refs = Object.getOwnPropertyNames(obj).map(key => originals.has.call(circ, obj[key]) ? 'circular' : resolve(obj[key], circ))
174+
fctObj.refs = Object
175+
.getOwnPropertyNames(obj)
176+
.filter(prop => prop !== 'name')
177+
.map(key => originals.has.call(circ, obj[key]) ? 'circular' : resolve(obj[key], circ))
174178
return fctObj
175179
}
176180

177181
const isArray = Array.isArray(obj)
178-
if (typeof obj !== 'object' && !isArray) {
182+
if (type !== 'object' && !isArray) {
179183
return obj
180184
}
181185

@@ -224,12 +228,14 @@ function resolve (obj, circ = new originals.constructor([obj])) {
224228
*/
225229
scope.Set.prototype.has = function has (value) {
226230
const valType = typeof value
231+
227232
if (valType === 'string' || valType === 'number' || valType === 'boolean') {
228233
return originals.has.call(this, value)
229234
}
230235

231236
const iterator = this.values()
232237
let element
238+
233239
while ((element = iterator.next().value) !== undefined) {
234240
const elType = typeof element
235241

@@ -258,9 +264,11 @@ scope.Set.prototype.has = function has (value) {
258264
// version of both and compare their strings.
259265
// - functions are string-ed and their properties are resolved
260266
// like objects
261-
if ((elType === 'function' && valType === 'function') ||
267+
if (
268+
(elType === 'function' && valType === 'function') ||
262269
(!setCompare && elType === 'object' && valType === 'object') ||
263-
(Array.isArray(element) && Array.isArray(value))) {
270+
(Array.isArray(element) && Array.isArray(value))
271+
) {
264272
const sortedElmnt = resolve(element)
265273
const sortedValue = resolve(value)
266274

src/index.tests.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ describe('Relations', function () {
8888
assert.isTrue(set([{ 1: [{ foo: 'bar' }] }]).has([{ 1: [{ foo: 'bar' }] }]))
8989
assert.isTrue(set('foo').has('foo'))
9090
assert.isTrue(set(NaN).has(NaN))
91-
assert.isTrue(set(isInt).has(function isInt (n) {
92-
return Number.isInteger(n)
93-
}))
91+
assert.isTrue(set(isInt).has(isInt))
92+
assert.isTrue(set(isInt).has(n => Number.isInteger(n)))
9493
})
9594

9695
it('returns false if a set has not a given element', function () {
@@ -102,7 +101,9 @@ describe('Relations', function () {
102101
assert.isFalse(set('foo').has('foo '))
103102
assert.isFalse(set([undefined]).has(null))
104103
assert.isFalse(set([null]).has(undefined))
105-
assert.isFalse(set(isInt).has(n => Number.isInteger(n)))
104+
assert.isFalse(set(isInt).has(function isInt (n) {
105+
return Number.isInteger(n)
106+
}))
106107
})
107108

108109
it('works recursively for nested sets', function () {
@@ -112,10 +113,10 @@ describe('Relations', function () {
112113
assert.isTrue(set(set([1, 2, 3]), set([4, 5, 6])).has(set([1, 2, 3])))
113114
assert.isFalse(set(set([1, 2, 3]), set([4, 5, 6])).has(set([1, 2, 3, 4])))
114115

115-
assert.isTrue(set(set(isInt)).has(set(function isInt (n) {
116+
assert.isTrue(set(set(isInt)).has(set(n => Number.isInteger(n))))
117+
assert.isFalse(set(set(isInt)).has(set(function isInt (n) {
116118
return Number.isInteger(n)
117119
})))
118-
assert.isFalse(set(set(isInt)).has(set(n => Number.isInteger(n))))
119120

120121
const s1 = set(3)
121122
const s2 = set(3)

0 commit comments

Comments
 (0)