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

[Feature Request-Object] PickBy #157

Open
lveillard opened this issue Nov 15, 2022 · 10 comments · May be fixed by #402
Open

[Feature Request-Object] PickBy #157

lveillard opened this issue Nov 15, 2022 · 10 comments · May be fixed by #402

Comments

@lveillard
Copy link

lveillard commented Nov 15, 2022

Hello!
I was wondering if somebody is up to build this one

const root = {a: 2, b: 3, c: {d: 7}, e:8}
pickBy(root, ((x) => x>2) // result: {b:3, e:8}
pickBy(root, ((x) => x.d == 7) // result {c: {d: 7}}

is similar to array.find() but for objects

Smething like this:

const pickBy = (obj, fn) => Object.fromEntries(Object.entries(obj).filter(fn))
PickBy(root, ((k,v])=> v>2)
@sodiray
Copy link
Owner

sodiray commented Nov 16, 2022

Hey @lveillard 👋 I agree, being able to filter in a more dynamic way would be great! I'm hesitant to follow the path that lodash took. Honestly, the whole By suffix always confused me. What if we just call it filter?

const filter = <T>(
  obj: T,
  fn: (k: keyof T, v: T) => boolean
): Partial<T>

@lveillard
Copy link
Author

Why not! I tend to call these oFilter, oMap... in order to make them obviously different to filter(), map()...

The o for "object" as they are object to object functions.

I always imagined these working natively like this:

{a:1, b:3}.filter((k,v)=>v>2)
// result: {b:3}

@sodiray
Copy link
Owner

sodiray commented Nov 23, 2022

Funny, I call them filtero and mapo 🍻

This is a pretty simple one, would you be interested in implementing it?

@lveillard
Copy link
Author

lveillard commented Nov 28, 2022

😂
Now that i think about it, is just FilterEntries(), isnt? If we keep current naming logic 🤔

And right now i'm figthing against some deadlines but happy to collaborate after that!

@lveillard
Copy link
Author

lveillard commented Dec 2, 2022

I'm struggling a bit with the Filter function's types but i added a find function.
#178

For the filter i'm here:

export const oFilter = <RemovedKeys extends string, T extends Record<string|number|symbol,any>>(obj: T, fn: (k: keyof T, v:any)=>boolean):Omit<T, RemovedKeys> => Object.fromEntries(Object.entries(obj).filter(([k,v])=> fn(k,v)))

but i'm not there yet

@lveillard
Copy link
Author

function OFilter<T, K extends keyof T>(obj: T, fn: (key: K, value: T[K]) => boolean): Pick<T, K> {
  return Object.fromEntries(Object.entries(obj).filter(([k,v]) => fn(k,v))) as Pick<T, K>;
}

What about this one?

@sodiray
Copy link
Owner

sodiray commented Dec 12, 2022

I think the return type you want is Partial<T>, I don't think you'll be able to type the specific keys that will be returned since it's dynamic at runtime.

Question, you're saying filter here but your PR says find. Did I miss something there?

@lveillard
Copy link
Author

Yay, they are two different functions but im not sure about the naming.

Find gets only property and returns directly its value
Filter shakes an object by a fn(k, v) and is the one that indeed could be typed as Partial

@prigaux
Copy link

prigaux commented Jul 31, 2023

For the record, radash shake can behave as lodash omitBy (which is alike pickBy, but inverted condition)

@yubaoquan
Copy link

Any progress on this?

@yubaoquan yubaoquan linked a pull request May 2, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants