← run

ts-02-groupby

1.000
6/6 tests· typing
Challenge · difficulty 2/5
# Generic `groupBy`

Implement **`solution.ts`** exporting a generic function:

```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]>
```

Group the `items` into a record keyed by the result of `keyFn(item)`. Each value is
the array of items that produced that key, **in their original order**.

- `groupBy([1, 2, 3, 4], (n) => (n % 2 === 0 ? "even" : "odd"))`
  → `{ odd: [1, 3], even: [2, 4] }`
- `groupBy([], (n: number) => n)` → `{}`
- A numeric key works too:
  `groupBy(["a", "bb", "ccc", "dd"], (s) => s.length)`
  → `{ 1: ["a"], 2: ["bb", "dd"], 3: ["ccc"] }`

Keep it fully typed (must pass `tsc --noEmit` in strict mode). Do not use `any` in the
public API.
tests/solution.test.ts
import { test } from "node:test";
import { strict as assert } from "node:assert";
import { groupBy } from "./solution.ts";

test("groups by string key, preserving order", () => {
  const r = groupBy([1, 2, 3, 4, 5], (n) => (n % 2 === 0 ? "even" : "odd"));
  assert.deepEqual(r, { odd: [1, 3, 5], even: [2, 4] });
});

test("empty input yields empty record", () => {
  assert.deepEqual(groupBy<number, number>([], (n) => n), {});
});

test("groups by numeric key", () => {
  const r = groupBy(["a", "bb", "ccc", "dd"], (s) => s.length);
  assert.deepEqual(r, { 1: ["a"], 2: ["bb", "dd"], 3: ["ccc"] });
});

test("groups objects by a property", () => {
  const people = [
    { name: "Ana", city: "NYC" },
    { name: "Bo", city: "LA" },
    { name: "Cy", city: "NYC" },
  ];
  const r = groupBy(people, (p) => p.city);
  assert.deepEqual(r, {
    NYC: [
      { name: "Ana", city: "NYC" },
      { name: "Cy", city: "NYC" },
    ],
    LA: [{ name: "Bo", city: "LA" }],
  });
});

test("single group when keyFn is constant", () => {
  const r = groupBy([1, 2, 3], () => "all");
  assert.deepEqual(r, { all: [1, 2, 3] });
});

test("does not mutate the input array", () => {
  const input = [3, 1, 2];
  groupBy(input, (n) => n);
  assert.deepEqual(input, [3, 1, 2]);
});
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]> {
  const result: Record<K, T[]> = {};

  for (const item of items) {
    const key = keyFn(item);
    if (!result[key]) {
      result[key] = [];
    }
    result[key].push(item);
  }

  return result;
}
```

Test output (stdout)

TAP version 13
# Subtest: groups by string key, preserving order
ok 1 - groups by string key, preserving order
  ---
  duration_ms: 0.669061
  type: 'test'
  ...
# Subtest: empty input yields empty record
ok 2 - empty input yields empty record
  ---
  duration_ms: 0.06408
  type: 'test'
  ...
# Subtest: groups by numeric key
ok 3 - groups by numeric key
  ---
  duration_ms: 0.06685
  type: 'test'
  ...
# Subtest: groups objects by a property
ok 4 - groups objects by a property
  ---
  duration_ms: 0.07235
  type: 'test'
  ...
# Subtest: single group when keyFn is constant
ok 5 - single group when keyFn is constant
  ---
  duration_ms: 0.05596
  type: 'test'
  ...
# Subtest: does not mutate the input array
ok 6 - does not mutate the input array
  ---
  duration_ms: 0.04331
  type: 'test'
  ...
1..6
# tests 6
# suites 0
# pass 6
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 88.937534
Reasoning (chain-of-thought)
The task is to implement a generic `groupBy` function in TypeScript that:

1. Takes an array of items of type `T`
2. Takes a key function that maps each item to a key of type `K` (where `K` extends `string | number`)
3. Returns a record where keys are of type `K` and values are arrays of items that produced that key
4. Must preserve the original order of items within each group
5. Must be fully typed and pass `tsc --noEmit` in strict mode
6. Cannot use `any` in the public API

The key challenge here is that TypeScript's type system needs to know that the keys in the returned record will be of type `K`, but `K` is a generic parameter that could be `string`, `number`, or a union of both.

Let me think about the implementation:

```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]>
```

The return type `Record<K, T[]>` should work because:
- `Record<K, T[]>` creates an object type where all keys are of type `K` and all values are arrays of type `T`
- This should work for both string and number keys

For the implementation, I need to:
1. Create an empty object to store the groups
2. Iterate through the items
3. For each item, compute the key using `keyFn`
4. Add the item to the appropriate group array

Let me write the code:

```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]> {
  const result: Record<K, T[]> = {};
  
  for (const item of items) {
    const key = keyFn(item);
    if (!(key in result)) {
      result[key] = [];
    }
    result[key].push(item);
  }
  
  return result;
}
```

This should work correctly. The `key in result` check ensures we only create new arrays for keys we haven't seen before, and we push items to the existing arrays.

Wait, I need to think about the type more carefully. When we use `key in result`, TypeScript needs to know that `key` is a valid key for the `result` object. Since `result` is typed as `Record<K, T[]>`, the `in` operator should work correctly.

Actually, there's a potential issue with the `in` operator. In TypeScript, `key in result` where `key` is of type `K` and `result` is `Record<K, T[]>` should work, but let me verify this is correct.

Actually, I think there might be a type issue. When we do `key in result`, TypeScript might infer that `key` is a string, not `K`. Let me think about this more carefully.

Actually, for `Record<K, T[]>`, the keys are of type `K`, so `key in result` should work. But to be safe, I could use a different approach:

```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]> {
  const result: Record<K, T[]> = {};
  
  for (const item of items) {
    const key = keyFn(item);
    if (!result[key]) {
      result[key] = [];
    }
    result[key].push(item);
  }
  
  return result;
}
```

This is cleaner and should work the same way. The `!result[key]` check will be `true` if `result[key]` is `undefined`, which means we haven't seen this key before.

Actually, I realize there's a subtle issue. When we access `result[key]`, TypeScript might infer that `result[key]` is `T[] | undefined`, so `!result[key]` would be `true` when `result[key]` is `undefined`. This should work correctly.

Let me verify the type constraints:
- `K extends string | number` - this means `K` can be `string`, `number`, or a union of both
- `Record<K, T[]>` - this creates an object type where all keys are of type `K` and all values are arrays of type `T`

This should work for all the test cases provided.

Let me also consider edge cases:
1. Empty array - should return `{}`
2. Numeric keys - should work
3. String keys - should work
4. Union of string and number keys - should work

The implementation looks correct. Let me finalize it:

```ts
export function groupBy<T, K extends string | number>(
  items: T[],
  keyFn: (item: T) => K,
): Record<K, T[]> {
  const result: Record<K, T[]> = {};
  
  for (const item of items) {
    const key = keyFn(item);
    if (!result[key]) {
      result[key] = [];
    }
    result[key].push(item);
  }
  
  return result;
}
```

This should pass `tsc --noEmit` in strict mode and work correctly for all the test cases.