deno.land / std@0.224.0 / collections / reduce_groups_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { reduceGroups } from "./reduce_groups.ts";
function reduceGroupsTest<T, A>( input: [ record: Record<string, ReadonlyArray<T>>, reducer: (accumulator: A, current: T) => A, initialValue: A, ], expected: Record<string, A>, message?: string,) { const actual = reduceGroups(...input); assertEquals(actual, expected, message);}
Deno.test({ name: "mapEntries() handles no mutation", fn() { const input = { Woody: [2, 3, 1, 4], Buzz: [5, 9], };
reduceGroups(input, (sum, it) => sum + it, 0);
assertEquals(input, { Woody: [2, 3, 1, 4], Buzz: [5, 9], }); },});
Deno.test({ name: "mapEntries() handles array of numbers", fn() { reduceGroupsTest( [ { Woody: [2, 3, 1, 4], Buzz: [5, 9], }, (sum, it) => sum + it, 0, ], { Woody: 10, Buzz: 14, }, ); },});
Deno.test({ name: "mapEntries() handles array of strings", fn() { reduceGroupsTest( [ { Woody: ["foo", "bar"], Buzz: ["foo", "bar", "baz"], }, (sum, it) => sum + it + " ", "", ], { Woody: "foo bar ", Buzz: "foo bar baz ", }, ); },});
Deno.test({ name: "mapEntries() handles mapper", fn() { reduceGroupsTest( [ { Woody: [{ val: 14 }, { val: 24 }], Buzz: [{ val: 1 }, { val: 2 }, { val: 3 }, { val: 4 }], }, (sum, it) => sum + it.val, 0, ], { Woody: 38, Buzz: 10, }, ); },});
Deno.test({ name: "mapEntries() handles initial value", fn() { reduceGroupsTest( [ { Woody: [], Buzz: [], }, (sum, it) => sum + it, 24, ], { Woody: 24, Buzz: 24, }, ); },});
Deno.test({ name: "mapEntries() handles empty input", fn() { reduceGroupsTest([{}, () => 0, 0], {}); },});
Version Info