-
Notifications
You must be signed in to change notification settings - Fork 361
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
feat(compat): Implement compat/pickBy #950
Open
gs18004
wants to merge
4
commits into
toss:main
Choose a base branch
from
gs18004:feature/946
base: main
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,59 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { pickBy } from './pickBy'; | ||
|
||
describe('pickBy', () => { | ||
it('should pick properties based on the predicate function', () => { | ||
const obj = { a: 1, b: 'pick', c: 3 }; | ||
const shouldPick = (value: string | number) => typeof value === 'string'; | ||
const result = pickBy(obj, shouldPick); | ||
expect(result).toEqual({ b: 'pick' }); | ||
}); | ||
|
||
it('should return an empty object if no properties satisfy the predicate', () => { | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const shouldPick = (value: number) => typeof value === 'string'; | ||
const result = pickBy(obj, shouldPick); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return the same object if all properties satisfy the predicate', () => { | ||
const obj = { a: 'pick', b: 'pick', c: 'pick' }; | ||
const shouldPick = (value: string) => typeof value === 'string'; | ||
const result = pickBy(obj, shouldPick); | ||
expect(result).toEqual(obj); | ||
}); | ||
|
||
it('should work with an empty object', () => { | ||
const obj = {}; | ||
const shouldPick = (value: never) => value; | ||
const result = pickBy(obj, shouldPick); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should work with nested objects', () => { | ||
const obj = { a: 1, b: { nested: 'pick' }, c: 3 }; | ||
const shouldPick = (value: number | { nested: string }, key: string) => key === 'b'; | ||
const result = pickBy(obj, shouldPick); | ||
expect(result).toEqual({ b: { nested: 'pick' } }); | ||
}); | ||
|
||
it('should work with no predicate function', () => { | ||
const obj = { a: 1, b: 'pick', c: 3 }; | ||
const result = pickBy(obj); | ||
expect(result).toEqual(obj); | ||
}); | ||
|
||
it('should return an empty object if the object is null', () => { | ||
const obj = null; | ||
const shouldPick = (value: string) => typeof value === 'string'; | ||
const result = pickBy(obj as unknown as object, shouldPick); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return an empty object if the object is undefined', () => { | ||
const obj = undefined; | ||
const shouldPick = (value: string) => typeof value === 'string'; | ||
const result = pickBy(obj as unknown as object, shouldPick); | ||
expect(result).toEqual({}); | ||
}); | ||
}); |
This file contains 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,43 @@ | ||
/** | ||
* Creates a new object composed of the properties that satisfy the predicate function. | ||
* | ||
* This function takes an object and a predicate function, and returns a new object that | ||
* includes only the properties for which the predicate function returns true. | ||
* | ||
* @template T - The type of object. | ||
* @param {T} obj - The object to pick properties from. | ||
* @param {(value: T[keyof T], key: keyof T) => boolean} shouldPick - A predicate function that determines | ||
* whether a property should be picked. It takes the property's key and value as arguments and returns `true` | ||
* if the property should be picked, and `false` otherwise. | ||
* @returns {Partial<T>} A new object with the properties that satisfy the predicate function. | ||
* | ||
* @example | ||
* const obj = { a: 1, b: 'pick', c: 3 }; | ||
* const shouldPick = (value) => typeof value === 'string'; | ||
* const result = pickBy(obj, shouldPick); | ||
* // result will be { b: 'pick' } | ||
*/ | ||
export function pickBy<T extends Record<string, any>>( | ||
obj: T, | ||
shouldPick?: (value: T[keyof T], key: keyof T) => boolean | ||
): Partial<T> { | ||
if (obj === null || obj === undefined) { | ||
return {}; | ||
} | ||
const result: Partial<T> = {}; | ||
|
||
if (shouldPick === undefined) { | ||
return obj; | ||
} | ||
const keys = Object.keys(obj) as Array<keyof T>; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const value = obj[key]; | ||
|
||
if (shouldPick(value, key)) { | ||
result[key] = value; | ||
} | ||
} | ||
|
||
return result; | ||
} |
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.
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.
Hi!
If you could add small fix here it would be great.
Function doesn't warn you if you pass null as obj, and if you do so, it will crash :)
however, lodash pickBy in that case returns {}
you can look here:
https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L13582
Please add same here, or some warning that obj can't be null.
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.
Thank you for your suggestion! I fixed this problem. Would you check it?
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.
Great! Thank you very much)
Maybe add some tests for it? I'm not sure.
And wait for approval from someone from repo)
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.
Sure! I will add some tests :)