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

Remove dependency on the deprecated lodash.get #254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

YasharF
Copy link

@YasharF YasharF commented Feb 21, 2025

lodash.get has been deprecated and consumers of samsam get the following deprecation warning during their npm install:

npm warn deprecated [email protected]: This package is deprecated. Use the optional chaining (?.) operator instead.

Background

Fixes issue: #253

How to verify

  1. Check out this branch
  2. npm ci
  3. npm run test

if (
actual === undefined ||
actual === null ||
get(actual, property) === undefined
getNestedProperty(actual, property) === undefined
Copy link

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 ?

Choose a reason for hiding this comment

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

+1 for ?.property

Copy link
Author

@YasharF YasharF Mar 2, 2025

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.

@YasharF YasharF force-pushed the main branch 2 times, most recently from 9e69aa1 to bebdcd2 Compare March 2, 2025 07:51
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.
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.

3 participants