-
Notifications
You must be signed in to change notification settings - Fork 31
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
Remove dependency on the deprecated lodash.get #254
base: main
Are you sure you want to change the base?
Conversation
lib/create-matcher.js
Outdated
if ( | ||
actual === undefined || | ||
actual === null || | ||
get(actual, property) === undefined | ||
getNestedProperty(actual, property) === undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we just use ?.
operator, ie actual?.property === undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for ?.property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing it up. I updated the PR to use ?.
instead of .reduce
and a helper function. We still need slightly more than just a direct substitution of .get()
with ?.
though because ?.
handles individual properties and hasNested function needs to handle nested paths which are not single properties.
To handle nested paths, the regex is breaking the property string into its individual components and removes empty values. The loop traverses the object, moving one level deeper for each part of the path so we can safely access the next property (part) of the current object (current = current?.[part]
). This handles cases where any intermediate property might be undefined
or null
. If current
becomes undefined
at any point we know the path doesn't exist, so we can immediately return false
. After the loop is finished then the value of current
is the value we want to get.
9e69aa1
to
bebdcd2
Compare
We need slightly more than a direct substitution of `.get()` with `?.` because `?.` handles individual properties and hasNested function needs to handle nested paths which are not single properties. To handle nested paths, the regex is breaking the property string into its individual components and removes empty values. The loop traverses the object, moving one level deeper for each part of the path so we can safely access the next property (part) of the current object (`current = current?.[part]`). This handles cases where any intermediate property might be `undefined` or `null`. If `current` becomes `undefined` at any point we know the path doesn't exist, so we can immediately return `false`. After the loop is finished then the value of `current` is the value we want to get.
lodash.get has been deprecated and consumers of samsam get the following deprecation warning during their npm install:
Background
Fixes issue: #253
How to verify
npm ci
npm run test