OpenFn / adaptors

The new home for OpenFn adaptors; re-usable connectors for the most common DPGs and DPI building blocks.
GNU General Public License v3.0
7 stars 8 forks source link

Common: add `group` operation #605

Closed josephjclark closed 3 months ago

josephjclark commented 4 months ago

EDIT: forget bin, this is group

The bin operation takes an array of data and sorts it into "bins".

bin(pathToArray, (state) => binNameString)

It works like each, taking an array or jsonpath as an argument, and iterating over each item with scoped data.

The return value of the function is the name of the bin to add the item to.

Bins are added as keys to the state object

For example, for this state

{
  data: [
    { name: 'anna', role: 'patient' },
    { name: 'betty',  role: 'doctor' },
    { name: 'charlie',  role: 'doctor' },
  ]
}

Calling this:

bin('$.data', (state) => state.data.role)

Results in this state:

{
  data: [ /* unchanged */ ]
  patient: [
    { name: 'anna', role: 'patient' },
  ],
  doctor: [
    { name: 'betty',  role: 'doctor' },
    { name: 'charlie',  role: 'doctor' },
  ]
}
mtuchi commented 3 months ago

I am not familiar with a key word bin. But from what i have understood so far, We need to a util helper that will sort an array of objects by a key path. The suggested signature bin(pathToArray, keyPath).

If this is the correct assessment then my suggestion is we don't use the word bin, We should use something very specific to what this function is doing. Eg sortByKey or sortBy. I personally like the word groupBy

I will sync with @josephjclark to get his opinion as well

josephjclark commented 3 months ago

Oh man groupBy is a way better name 🤦🤦🤦

Or maybe just group?

For me sort just means "change the order of an array", whereas this function is "organise this data into groups"

mtuchi commented 3 months ago

@josephjclark should this be an operation or a util helper ?

aleksa-krolls commented 3 months ago

hey @mtuchi @josephjclark can we close this issue out? I see the PR has been merged