matheus-rodrigues00 / utils

✨ Library to commonly used cross-projects utilities methods ✨
17 stars 11 forks source link

Create deepPick Method #34

Closed matheus-rodrigues00 closed 1 year ago

matheus-rodrigues00 commented 1 year ago

The pick method implemented by @GustavoHBO works great! It would be even better if we can tell which nested properties we actually want from nested objects.

Method signature:

function deepPick<T extends object, K extends keyof T>(
  source: T,
  keys: K[]
): Pick<T, K> {
   // Gustavo code here 🕺
}
nullsploit01 commented 1 year ago

@matheus-rodrigues00 is this issue already fixed? If not, how will this be different from pick method in objects.ts, could you give more info?

matheus-rodrigues00 commented 1 year ago

@NullSploit01 and @GustavoHBO the deepPick method proposal is to also consider nested object properties. Example:

  const mock_object: any = {
      a: 1,
      b: 2,
      c: {
        d: {
          e: 3,
          f: 4,
          g: 5,
        },
      },
    };
    const keys: string[] = ["a", "b", "c.d.e"];
    console.log(deepPick(mock_object, keys));
    // this log should yield:
    const expected_answer = {
      a: 1,
      b: 2,
      c: {
        d: {
          e: 3,
        },
      },
    };
    // every other key inside c.d.e is filtered from the original object.

After that we can even think about improve it to consider array object property filters. Like, suppose we only want the first or second index of an array, like this call:

    const keys: string[] = ["a", "b[0]"];
deepPick(mock_object, keys)
// basically I'm saying that b value will be an array, and I only want the first index of it. 

One thing one who's going to implement this needs to take care is that multiple properties can be selected from nested object, like:

    const keys: string[] = ["a", "b.c.d", "b.c.e"];
deepPick(mock_object, keys)
nullsploit01 commented 1 year ago

Oh okay got it, thank you for explaining! Will try to implement it