mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
d102a10235
* refactor: import from `@std/assert` * update
43 lines
1004 B
TypeScript
43 lines
1004 B
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Applies the given selector to all elements in the given collection and
|
|
* calculates the sum of the results.
|
|
*
|
|
* @typeParam T The type of the array elements.
|
|
*
|
|
* @param array The array to calculate the sum of.
|
|
* @param selector The selector function to get the value to sum.
|
|
*
|
|
* @returns The sum of all elements in the collection.
|
|
*
|
|
* @example Basic usage
|
|
* ```ts
|
|
* import { sumOf } from "@std/collections/sum-of";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* const people = [
|
|
* { name: "Anna", age: 34 },
|
|
* { name: "Kim", age: 42 },
|
|
* { name: "John", age: 23 },
|
|
* ];
|
|
*
|
|
* const totalAge = sumOf(people, (person) => person.age);
|
|
*
|
|
* assertEquals(totalAge, 99);
|
|
* ```
|
|
*/
|
|
export function sumOf<T>(
|
|
array: Iterable<T>,
|
|
selector: (el: T) => number,
|
|
): number {
|
|
let sum = 0;
|
|
|
|
for (const i of array) {
|
|
sum += selector(i);
|
|
}
|
|
|
|
return sum;
|
|
}
|