Skip to content

Commit

Permalink
Update traverse to recognize Symbol properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Americas committed Dec 20, 2023
1 parent f94bda4 commit 059f5fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,25 @@ function forEach(xs, fn) {
}

// TODO: use object-keys
var objectKeys = Object.keys || function keys(obj) {
function objectKeys(obj) {
var res = [];
for (var key in obj) { res.push(key); } // eslint-disable-line no-restricted-syntax

for (var key in obj) { // eslint-disable-line no-restricted-syntax
res.push(key);
}

if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(obj);

for (var i = 0; i < symbols.length; i++) {
if (Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) {
res.push(symbols[i]);
}
}
}

return res;
};
}

// TODO: use object.hasown
var hasOwnProperty = Object.prototype.hasOwnProperty || function (obj, key) {
Expand Down
21 changes: 21 additions & 0 deletions test/has.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,26 @@ test('has', function (t) {
t.equal(traverse(obj).has(['a']), true);
t.equal(traverse(obj).has(['a', 2]), false);

if (global.Symbol) {
var globalSymbol = Symbol.for('d'); // eslint-disable-line no-restricted-properties
var localSymbol = Symbol('e');
var nonEnumerableSymbol = Symbol('e');

obj[globalSymbol] = {};
obj[globalSymbol][localSymbol] = 7;
obj[localSymbol] = 8;
Object.defineProperty(obj, nonEnumerableSymbol, { value: 42, enumerable: false });

t.equal(traverse(obj).has([globalSymbol]), true);
t.equal(traverse(obj).has([globalSymbol, globalSymbol]), false);
t.equal(traverse(obj).has([globalSymbol, localSymbol]), true);
t.equal(traverse(obj).has([localSymbol]), true);
t.equal(traverse(obj).has([localSymbol]), true);
t.equal(traverse(obj).has([nonEnumerableSymbol]), true);
t.equal(traverse(obj).has([Symbol('e')]), false);
t.equal(traverse(obj).has([Symbol.for('d')]), true); // eslint-disable-line no-restricted-properties
t.equal(traverse(obj).has([Symbol.for('e')]), false); // eslint-disable-line no-restricted-properties
}

t.end();
});

0 comments on commit 059f5fc

Please sign in to comment.