radashi-org / radashi

The modern, community-first TypeScript toolkit with all of the fast, readable, and minimal utility functions you need. Type-safe, dependency-free, tree-shakeable, fully tested.
https://radashi.js.org
MIT License
315 stars 25 forks source link

Add `unzipFromObject` function #275

Closed ilxqx closed 1 month ago

ilxqx commented 1 month ago

The zipToObject function exists in the library to combine a key array and a value array into an object, but there is no method to convert an object into two arrays (one array of keys and one array of values, where keys and values correspond to each other in the same array positions).

Below is a scenario that supports my need:

Suppose I want to dynamically create a function, and the parameters of this function are uncertain, depending on how many properties exist in a given context object (the order of the parameters does not matter). Below is a piece of pseudocode:


const context = {
  // some key values
}

const [keys, values] = unzipFromObject(context)
const dynFn = new Function(...[ ...keys, 'function body' ])
dynFn.apply(undefined, values) // invoke the dynamic fn created above
``