@std/collections `zip` with customizable handling of where to truncate (longest vs shortest)
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
Sometimes it's useful to zip arrays together without truncating the shorter ones, instead filling with `undefined`.
**Describe the solution you'd like**
Allow passing an option such as `options.truncate = 'longest'` to continue zipping until the longest array ends, filling missing values with `undefined`.
**Describe alternatives you've considered**
Not really sure how best to add this to the current function signature, given the variadic `...arrays` param.
One possible option would be taking options as an _initial_ parameter, which could be detected at runtime with `Array.isArray`:
```ts
type ZipOptions = {
truncate?: 'shortest' | 'longest';
}
export function zip(
options: O,
...arrays: { [K in keyof T]: ReadonlyArray }
): {
[K in keyof T]: O extends { truncate: "longest" } ? T[K] | undefined : T[K];
}[];
export function zip(
...arrays: { [K in keyof T]: ReadonlyArray }
): T[];
export function zip(
...args: unknown[]
): unknown[] {
const [options, arrays] = args.length === 0 || Array.isArray(args[0]) ?
[{}, args as unknown[][]] :
[args[0] as ZipOptions, args.slice(1) as unknown[][]];
```
Otherwise, it might need to be a separate `zipWithOptions` function.
Contributor guide
Assessment
This issue has not been assessed yet.