diff --git a/collections/group_by.ts b/collections/group_by.ts deleted file mode 100644 index 2cbeea6fb2f1..000000000000 --- a/collections/group_by.ts +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -// This module is browser compatible. - -/** - * Applies the given selector to each element in the given array, returning a - * Record containing the results as keys and all values that produced that key - * as values. - * - * @example - * ```ts - * import { groupBy } from "https://deno.land/std@$STD_VERSION/collections/group_by.ts"; - * import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts"; - * - * const people = [ - * { name: "Anna" }, - * { name: "Arnold" }, - * { name: "Kim" }, - * ]; - * const peopleByFirstLetter = groupBy(people, (it) => it.name.charAt(0)); - * - * assertEquals( - * peopleByFirstLetter, - * { - * "A": [{ name: "Anna" }, { name: "Arnold" }], - * "K": [{ name: "Kim" }], - * }, - * ); - * ``` - * - * @deprecated (will be removed in 0.211.0) Use {@linkcode Object.groupBy} instead. - */ -export function groupBy( - iterable: Iterable, - selector: (element: T, index: number) => K, -): Partial> { - const ret: Partial> = {}; - let i = 0; - - for (const element of iterable) { - const key = selector(element, i++); - const arr: T[] = ret[key] ??= []; - arr.push(element); - } - - return ret; -} diff --git a/collections/group_by_test.ts b/collections/group_by_test.ts deleted file mode 100644 index 62368249f843..000000000000 --- a/collections/group_by_test.ts +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -import { assertEquals } from "../assert/mod.ts"; -import { groupBy } from "./group_by.ts"; - -function groupByTest( - input: [Array, (el: T) => PropertyKey], - expected: { [x: string]: Array }, - message?: string, -) { - const actual = groupBy(...input); - assertEquals(actual, expected, message); -} - -Deno.test({ - name: "groupBy() handles no mutation", - fn() { - const arrayA = [1.1, 4.2, 4.5]; - groupBy(arrayA, () => "test"); - - assertEquals(arrayA, [1.1, 4.2, 4.5]); - }, -}); - -Deno.test({ - name: "groupBy() handles empty input", - fn() { - groupByTest( - [[], () => "a"], - {}, - ); - }, -}); - -Deno.test({ - name: "groupBy() handles constant key", - fn() { - groupByTest( - [[1, 3, 5, 6], () => "a"], - { a: [1, 3, 5, 6] }, - ); - }, -}); -Deno.test({ - name: "groupBy() handles non-string key", - fn() { - groupByTest( - [ - [ - { number: 1, name: "a" }, - { number: 1, name: "b" }, - { number: 2, name: "c" }, - { number: 3, name: "d" }, - ], - ({ number }) => number, - ], - { - 1: [{ number: 1, name: "a" }, { number: 1, name: "b" }], - 2: [{ number: 2, name: "c" }], - 3: [{ number: 3, name: "d" }], - }, - ); - }, -}); - -Deno.test({ - name: "groupBy() handles empty key", - fn() { - groupByTest( - [ - ["Foo", "b"], - (it) => it.charAt(1), - ], - { - "o": ["Foo"], - "": ["b"], - }, - ); - }, -}); - -Deno.test({ - name: "groupBy() handles groups", - fn() { - groupByTest( - [ - ["Anna", "Marija", "Karl", "Arnold", "Martha"], - (it) => it.charAt(0), - ], - { - "A": ["Anna", "Arnold"], - "M": ["Marija", "Martha"], - "K": ["Karl"], - }, - ); - groupByTest( - [ - [1.2, 2, 2.3, 6.3, 6.9, 6], - (it) => Math.floor(it).toString(), - ], - { - "1": [1.2], - "2": [2, 2.3], - "6": [6.3, 6.9, 6], - }, - ); - }, -}); - -Deno.test({ - name: "groupBy() handles callback index", - fn() { - const actual = groupBy( - ["a", "b", "c", "d"], - (_, i) => i % 2 === 0 ? "even" : "odd", - ); - - const expected = { even: ["a", "c"], odd: ["b", "d"] }; - - assertEquals(actual, expected); - }, -}); - -Deno.test({ - name: "groupBy() handles iterable input", - fn() { - function* count(): Generator { - for (let i = 0; i < 5; i += 1) yield i; - } - - const actual = groupBy(count(), (n) => n % 2 === 0 ? "even" : "odd"); - const expected = { even: [0, 2, 4], odd: [1, 3] }; - - assertEquals(actual, expected); - }, -}); diff --git a/collections/mod.ts b/collections/mod.ts index 74afcb183b00..f9dff0b6b3f9 100644 --- a/collections/mod.ts +++ b/collections/mod.ts @@ -21,7 +21,6 @@ export * from "./drop_while.ts"; export * from "./filter_entries.ts"; export * from "./filter_keys.ts"; export * from "./filter_values.ts"; -export * from "./group_by.ts"; export * from "./intersect.ts"; export * from "./map_entries.ts"; export * from "./map_keys.ts";