chore: add safeMerge shared util - #166
Open
taran-a wants to merge 1 commit into
Open
Conversation
| RemoteFeatureFlagsProvider, | ||
| type RemoteFeatureFlagsProviderMessenger, | ||
| } from './providers/remote-feature-flags/RemoteFeatureFlagsProvider'; | ||
| export { safeMerge } from './safeMerge'; |
Contributor
There was a problem hiding this comment.
not related to this PR
but can we add export of logger too? it seem missing?
| * const overridee = { name: 'John' }; | ||
| * const overrider = { name: undefined, age: 30 }; | ||
| * const merged = safeMerge(overridee, overrider); | ||
| * // merged is { name: 'John' } |
Contributor
There was a problem hiding this comment.
Safe megre is actually:
Not a deep merge (only one level)
BigNumber is not support (it is fine, but since we use bignumber rather than bigint)
not support Date, Map, Set, empty arrays <-- empty array is more important
consider using lodash with a wrapper to handle null case
something like
import { isPlainObject, mergeWith } from 'lodash';
export const safeMerge = <TOverridee extends object, TOverrider extends object>(
overridee: TOverridee,
overrider: TOverrider,
): TOverridee & TOverrider=>
mergeWith({}, overridee, overrider, (objValue, srcValue) => {
// handle null case
if (srcValue === undefined || srcValue === null) {
return objValue;
}
// handle {}
if (isPlainObject(srcValue) && Object.keys(srcValue).length === 0) {
return objValue;
}
if (!isPlainObject(objValue) || !isPlainObject(srcValue)) {
return srcValue;
}
// let lodash recurse
return undefined;
});
but if we just wanna to limit up our usecase, it also fine, but worth to mention all limitation in the jsdoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
Add safeMerge shared util.
Use it in tron and solana snaps.
Add two test cases.
References
Checklist