Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ignore symbol properties #23

Merged
merged 5 commits into from
Jul 26, 2022
Merged

Do not ignore symbol properties #23

merged 5 commits into from
Jul 26, 2022

Conversation

ehmicky
Copy link
Contributor

@ehmicky ehmicky commented Jul 24, 2022

Fixes #21.

This ensures symbol properties are kept.
This is a breaking change since the function predicate passed as argument needs to handle the potential case that the key might be a symbol instead of a string.

for (const key of Object.keys(object)) {
if (set.has(key)) {
for (const key of Reflect.ownKeys(object)) {
if (isEnumerable.call(object, key) && set.has(key)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike Object.keys(), Reflect.ownKeys() also returns symbols. However, it also returns non-enumerable properties, so we need an additional check.

}
}
}

return result;
}

const {propertyIsEnumerable: isEnumerable} = Object.prototype;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propertyIsEnumerable is also a global function, so the property is renamed to avoid shadowing.

readme.md Outdated
@@ -48,15 +46,15 @@ The source object to filter properties from.

#### filter

Type: `(sourceKey, sourceValue, source) => boolean`
Type: `(sourceKey: string | symbol, sourceValue: any, source: object) => boolean`
Copy link
Contributor Author

@ehmicky ehmicky Jul 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflect.ownKeys() returns array indices as strings (like Object.keys() does), so sourceKey cannot be a number.

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
index.test-d.ts Outdated
@@ -1,15 +1,17 @@
import {expectType, expectError} from 'tsd';
import {includeKeys, excludeKeys} from './index.js';

const propSymbol = Symbol('test');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const propSymbol = Symbol('test');
const propertySymbol = Symbol('test');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c4552a0

ehmicky and others added 3 commits July 25, 2022 18:21

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Verified

This commit was signed with the committer’s verified signature.
Byron Sebastian Thiel
@ehmicky ehmicky requested a review from sindresorhus July 25, 2022 16:22
@ehmicky
Copy link
Contributor Author

ehmicky commented Jul 25, 2022

Fixed 👍

@sindresorhus sindresorhus merged commit 1c166d3 into sindresorhus:main Jul 26, 2022
@sindresorhus
Copy link
Owner

Thanks :)

@ehmicky ehmicky deleted the symbols branch July 26, 2022 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Symbol properties should be kept
2 participants