-
Notifications
You must be signed in to change notification settings - Fork 198
feat(mergeProps): add mergeProps function #740
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
Open
guoyunhe
wants to merge
5
commits into
react-component:master
Choose a base branch
from
guoyunhe:simple-merge-props
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+75
−0
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b13959
feat: mergeProps function
guoyunhe 4287ef6
test: add unit tests for mergeProps function
guoyunhe ee57bb8
refactor: tsx to ts
guoyunhe 57cd197
Update src/mergeProps.ts
guoyunhe 534bcef
docs: add JSDoc comment to mergeProps function explaining behavior wi…
guoyunhe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function mergeProps<A, B>(a: A, b: B): B & A; | ||
| function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A; | ||
| function mergeProps<A, B, C, D>(a: A, b: B, c: C, d: D): D & C & B & A; | ||
| function mergeProps(...items: any[]) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 多个 useMergeProps?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 目前感觉不是很必要,因为每次 render 的 props 实例总是变化的,memo 不住呢 |
||
| const ret: any = {}; | ||
| for (const item of items) { | ||
| if (item) { | ||
| for (const key of Object.keys(item)) { | ||
| if (item[key] !== undefined) { | ||
| ret[key] = item[key]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| export default mergeProps; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import mergeProps from '../src/mergeProps'; | ||
|
|
||
| describe('mergeProps', () => { | ||
| it('merges two objects with later overriding earlier', () => { | ||
| const a = { foo: 1, bar: 2 }; | ||
| const b = { bar: 3, baz: 4 }; | ||
| expect(mergeProps(a, b)).toEqual({ foo: 1, bar: 3, baz: 4 }); | ||
| }); | ||
|
|
||
| it('excludes keys with undefined values', () => { | ||
| const a = { foo: 1, bar: undefined }; | ||
| const b = { bar: 2 }; | ||
| expect(mergeProps(a, b)).toEqual({ foo: 1, bar: 2 }); | ||
| }); | ||
|
|
||
| it('does not include key if value is undefined in last object', () => { | ||
| const a = { foo: 1 }; | ||
| const b = { bar: undefined }; | ||
| expect(mergeProps(a, b)).toEqual({ foo: 1 }); | ||
| }); | ||
|
|
||
| it('skips null and undefined items', () => { | ||
| const a = { foo: 1 }; | ||
| expect(mergeProps(a, null as any)).toEqual({ foo: 1 }); | ||
| expect(mergeProps(a, undefined as any)).toEqual({ foo: 1 }); | ||
| expect(mergeProps(null as any, a)).toEqual({ foo: 1 }); | ||
| expect(mergeProps(undefined as any, a)).toEqual({ foo: 1 }); | ||
| }); | ||
|
|
||
| it('merges three or more objects with rightmost winning', () => { | ||
| const a = { a: 1 }; | ||
| const b = { a: 2, b: 2 }; | ||
| const c = { a: 3, b: 3, c: 3 }; | ||
| expect(mergeProps(a, b, c)).toEqual({ a: 3, b: 3, c: 3 }); | ||
| }); | ||
|
|
||
| it('returns empty object for no args', () => { | ||
| expect((mergeProps as (...items: any[]) => any)()).toEqual({}); | ||
| }); | ||
|
|
||
| it('returns copy of single object', () => { | ||
| const a = { foo: 1, bar: 2 }; | ||
| expect((mergeProps as (...items: any[]) => any)(a)).toEqual({ | ||
| foo: 1, | ||
| bar: 2, | ||
| }); | ||
| }); | ||
|
|
||
| it('handles empty objects', () => { | ||
| expect(mergeProps({}, { a: 1 }, {})).toEqual({ a: 1 }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.