deno.land / std@0.224.0 / collections / aggregate_groups_test.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { aggregateGroups } from "./aggregate_groups.ts";
function aggregateGroupsTest<T, A>( input: [ Record<string, ReadonlyArray<T>>, (current: T, key: string, first: boolean, accumulator?: A) => A, ], expected: Record<string, A>, message?: string,) { const actual = aggregateGroups(...input); assertEquals(actual, expected, message);}
Deno.test({ name: "aggregateGroups() handles no mutation", fn() { const input = { Woody: [2, 3, 1, 4], Buzz: [5, 9], };
aggregateGroups(input, () => 5);
assertEquals(input, { Woody: [2, 3, 1, 4], Buzz: [5, 9], }); },});
Deno.test({ name: "aggregateGroups() handles string building using all params", fn() { aggregateGroupsTest( [ { "Curry": ["spicy", "vegan"], "Omelette": ["creamy", "vegetarian"], }, (current, key, first, acc) => { if (first) { return `${key} is ${current}`; }
return `${acc} and ${current}`; }, ], { "Curry": "Curry is spicy and vegan", "Omelette": "Omelette is creamy and vegetarian", }, ); },});
Deno.test({ name: "aggregateGroups() handles sum ignoring non reduce params", fn() { aggregateGroupsTest( [ { Woody: [1, 2, 3], Buzz: [5, 6, 7], }, (current, _key, _first, acc) => current + (acc ?? 0), ], { Woody: 6, Buzz: 18, }, ); },});
Deno.test({ name: "aggregateGroups() handles empty input", fn() { aggregateGroupsTest([ {}, () => 1, ], {}); },});
Version Info