From c2b59a12a0cd43b4aff509aaf568e802a54573fd Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:30:18 +0300 Subject: [PATCH 01/13] add sumof tests --- collections/sum_of.ts | 26 ++++++++++++++++++++++++++ collections/sum_of_test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 collections/sum_of.ts create mode 100644 collections/sum_of_test.ts diff --git a/collections/sum_of.ts b/collections/sum_of.ts new file mode 100644 index 000000000000..886d425804bf --- /dev/null +++ b/collections/sum_of.ts @@ -0,0 +1,26 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +import { Selector } from "./types.ts"; + +/** + * Applies the given selector to all elements in the given collection and calculates the sum of the results + * + * Example: + * + * ```typescript + * const people = [ + * { name: 'Anna', age: 34 }, + * { name: 'Kim', age: 42 }, + * { name: 'John', age: 23 }, + * ] + * const totalAge = sumOf(people, it => it.age) + * + * console.assert(totalAge, 99) + * ``` + */ +export function sumOf( + array: Array, + selector: Selector +): number { + return 0; +} \ No newline at end of file diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts new file mode 100644 index 000000000000..8f09ab3a3dfc --- /dev/null +++ b/collections/sum_of_test.ts @@ -0,0 +1,32 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +import { assertEquals } from "../testing/asserts.ts"; +import { sumOf } from "./sum_of.ts"; + +Deno.test("[collections/sumOf] On object properties", () => { + const object = [ + { name: "Rock Lee", age: 34 }, + { name: "John", age: 42 }, + { name: "Chun Li", age: 23 }, + ] + + const actual = sumOf(object, i => i.age); + + assertEquals(actual, 99); +}) + +Deno.test("[collections/sumOf] Add 2 to each num", () => { + const array = [1, 2, 3]; + + const actual = sumOf(array, i => i + 2); + + assertEquals(actual, 13); +}) + +Deno.test("[collections/sumOf] Do nothing", () => { + const array = [1, 2, 3]; + + const actual = sumOf(array, i => i); + + assertEquals(actual, 6); +}) \ No newline at end of file From 08ea47984718a3850183432285910a4bc8c4c35b Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:32:26 +0300 Subject: [PATCH 02/13] add sumof, fix test --- collections/sum_of.ts | 9 ++++++++- collections/sum_of_test.ts | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/collections/sum_of.ts b/collections/sum_of.ts index 886d425804bf..287ccc0270d2 100644 --- a/collections/sum_of.ts +++ b/collections/sum_of.ts @@ -22,5 +22,12 @@ export function sumOf( array: Array, selector: Selector ): number { - return 0; + const selected = array.map(selector); + let sum = 0; + + for (let i of selected) { + sum += i; + } + + return sum; } \ No newline at end of file diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index 8f09ab3a3dfc..0a3bb7d12cf5 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -20,10 +20,10 @@ Deno.test("[collections/sumOf] Add 2 to each num", () => { const actual = sumOf(array, i => i + 2); - assertEquals(actual, 13); + assertEquals(actual, 12); }) -Deno.test("[collections/sumOf] Do nothing", () => { +Deno.test("[collections/sumOf] Regular sum", () => { const array = [1, 2, 3]; const actual = sumOf(array, i => i); From 8c37da407ab064e6f36dfbaf1797ce0ceca684b1 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:33:13 +0300 Subject: [PATCH 03/13] some other test stuff --- collections/sum_of_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index 0a3bb7d12cf5..d3a0b8d15c88 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -5,9 +5,9 @@ import { sumOf } from "./sum_of.ts"; Deno.test("[collections/sumOf] On object properties", () => { const object = [ - { name: "Rock Lee", age: 34 }, + { name: "Kyle", age: 34 }, { name: "John", age: 42 }, - { name: "Chun Li", age: 23 }, + { name: "Anna", age: 23 }, ] const actual = sumOf(object, i => i.age); From 4c1ec8e26521f95dc8e5b3f18571dc0dad42bcac Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:35:42 +0300 Subject: [PATCH 04/13] lint, fmt --- collections/sum_of.ts | 20 ++++++++++---------- collections/sum_of_test.ts | 34 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/collections/sum_of.ts b/collections/sum_of.ts index 287ccc0270d2..5d9b743d7179 100644 --- a/collections/sum_of.ts +++ b/collections/sum_of.ts @@ -19,15 +19,15 @@ import { Selector } from "./types.ts"; * ``` */ export function sumOf( - array: Array, - selector: Selector + array: Array, + selector: Selector, ): number { - const selected = array.map(selector); - let sum = 0; - - for (let i of selected) { - sum += i; - } + const selected = array.map(selector); + let sum = 0; - return sum; -} \ No newline at end of file + for (const i of selected) { + sum += i; + } + + return sum; +} diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index d3a0b8d15c88..c85a669c1ad8 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -4,29 +4,29 @@ import { assertEquals } from "../testing/asserts.ts"; import { sumOf } from "./sum_of.ts"; Deno.test("[collections/sumOf] On object properties", () => { - const object = [ - { name: "Kyle", age: 34 }, - { name: "John", age: 42 }, - { name: "Anna", age: 23 }, - ] + const object = [ + { name: "Kyle", age: 34 }, + { name: "John", age: 42 }, + { name: "Anna", age: 23 }, + ]; - const actual = sumOf(object, i => i.age); + const actual = sumOf(object, (i) => i.age); - assertEquals(actual, 99); -}) + assertEquals(actual, 99); +}); Deno.test("[collections/sumOf] Add 2 to each num", () => { - const array = [1, 2, 3]; + const array = [1, 2, 3]; - const actual = sumOf(array, i => i + 2); + const actual = sumOf(array, (i) => i + 2); - assertEquals(actual, 12); -}) + assertEquals(actual, 12); +}); Deno.test("[collections/sumOf] Regular sum", () => { - const array = [1, 2, 3]; - - const actual = sumOf(array, i => i); + const array = [1, 2, 3]; - assertEquals(actual, 6); -}) \ No newline at end of file + const actual = sumOf(array, (i) => i); + + assertEquals(actual, 6); +}); From fcde4157542f2b699067e0befa039ddb83947238 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:37:22 +0300 Subject: [PATCH 05/13] fix example code hopefully --- collections/sum_of.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/collections/sum_of.ts b/collections/sum_of.ts index 5d9b743d7179..c921ebb12f1b 100644 --- a/collections/sum_of.ts +++ b/collections/sum_of.ts @@ -8,14 +8,16 @@ import { Selector } from "./types.ts"; * Example: * * ```typescript + * import { sumOf } from "./sum_of.ts" + * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, * { name: 'John', age: 23 }, * ] - * const totalAge = sumOf(people, it => it.age) + * const totalAge = sumOf(people, i => i.age) * - * console.assert(totalAge, 99) + * console.assert(totalAge === 99) * ``` */ export function sumOf( From 434ca790b0cecd77ba2382f7a01b98061ca35451 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:38:37 +0300 Subject: [PATCH 06/13] even more fmt --- collections/sum_of.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collections/sum_of.ts b/collections/sum_of.ts index c921ebb12f1b..5b30255b1d9a 100644 --- a/collections/sum_of.ts +++ b/collections/sum_of.ts @@ -9,7 +9,7 @@ import { Selector } from "./types.ts"; * * ```typescript * import { sumOf } from "./sum_of.ts" - * + * * const people = [ * { name: 'Anna', age: 34 }, * { name: 'Kim', age: 42 }, From 06d7f6aadb76cc069d9abcf02a76c1611f25d726 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 15:42:27 +0300 Subject: [PATCH 07/13] export sumof from mod.ts --- collections/mod.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/collections/mod.ts b/collections/mod.ts index e049bdad2649..553dc5f1b58d 100644 --- a/collections/mod.ts +++ b/collections/mod.ts @@ -14,3 +14,4 @@ export * from "./map_keys.ts"; export * from "./map_values.ts"; export * from "./partition.ts"; export * from "./permutations.ts"; +export * from "./sum_of.ts"; From 6514b0d8e9254c41d87fd2d03615a76bbdd88fc1 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 16:20:39 +0300 Subject: [PATCH 08/13] add more sumof tests --- collections/sum_of_test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index c85a669c1ad8..86b145a412d8 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -30,3 +30,35 @@ Deno.test("[collections/sumOf] Regular sum", () => { assertEquals(actual, 6); }); + +Deno.test("[collections/sumOf] Negatives with regular sum", () => { + const array = [-1, -2, -3]; + + const actual = sumOf(array, (i) => i); + + assertEquals(actual, -6); +}); + +Deno.test("[collections/sumOf] Mixed negatives and positives with regular sum", () => { + const array = [-1, 2, 3, -5]; + + const actual = sumOf(array, (i) => i); + + assertEquals(actual, -1); +}); + +Deno.test("[collections/sumBy] Selector turns nums into negatives", () => { + const array = [1, 3, 5, 3]; + + const actual = sumOf(array, (i) => i - 6); + + assertEquals(actual, -12); +}); + +Deno.test("[collections/sumBy] Selector turns nums into zeros", () => { + const array = [3, 3, 3, 3]; + + const actual = sumOf(array, (i) => i - 3); + + assertEquals(actual, 0); +}); From 8ff42a6cf19bed160a7af90369124e869d2ae9eb Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 16:22:17 +0300 Subject: [PATCH 09/13] add more object tests --- collections/sum_of_test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index 86b145a412d8..aee7fb51a18b 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -62,3 +62,27 @@ Deno.test("[collections/sumBy] Selector turns nums into zeros", () => { assertEquals(actual, 0); }); + +Deno.test("[collections/sumOf] On negative object properties", () => { + const object = [ + { name: "Kyle", age: -34 }, + { name: "John", age: -42 }, + { name: "Anna", age: -23 }, + ]; + + const actual = sumOf(object, (i) => i.age); + + assertEquals(actual, -99); +}); + +Deno.test("[collections/sumOf] On mixed object properties", () => { + const object = [ + { name: "Kyle", age: -34 }, + { name: "John", age: 42 }, + { name: "Anna", age: -23 }, + ]; + + const actual = sumOf(object, (i) => i.age); + + assertEquals(actual, -15); +}); \ No newline at end of file From 93c46d3fd6b49a3dc786c9c23a62a8d9b8cbc028 Mon Sep 17 00:00:00 2001 From: Grian Date: Sat, 7 Aug 2021 16:22:51 +0300 Subject: [PATCH 10/13] fmt --- collections/sum_of_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index aee7fb51a18b..7facda62af13 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -85,4 +85,4 @@ Deno.test("[collections/sumOf] On mixed object properties", () => { const actual = sumOf(object, (i) => i.age); assertEquals(actual, -15); -}); \ No newline at end of file +}); From a3f412eb28dd10532db381ea0cd3ba58e764fa03 Mon Sep 17 00:00:00 2001 From: Grian Date: Sun, 8 Aug 2021 05:17:24 +0300 Subject: [PATCH 11/13] leon review --- collections/sum_of.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/collections/sum_of.ts b/collections/sum_of.ts index 5b30255b1d9a..bf9ed29698f4 100644 --- a/collections/sum_of.ts +++ b/collections/sum_of.ts @@ -1,14 +1,13 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { Selector } from "./types.ts"; - /** * Applies the given selector to all elements in the given collection and calculates the sum of the results * * Example: * - * ```typescript + * ```ts * import { sumOf } from "./sum_of.ts" + * import { assertEquals } from "../testing/asserts.ts" * * const people = [ * { name: 'Anna', age: 34 }, @@ -17,18 +16,17 @@ import { Selector } from "./types.ts"; * ] * const totalAge = sumOf(people, i => i.age) * - * console.assert(totalAge === 99) + * assertEquals(totalAge, 99) * ``` */ export function sumOf( array: Array, - selector: Selector, + selector: (el: T) => number, ): number { - const selected = array.map(selector); let sum = 0; - for (const i of selected) { - sum += i; + for (const i of array) { + sum += selector(i); } return sum; From 9c5395ebda19f2fb49e161281c3a7bd7a8b8aa79 Mon Sep 17 00:00:00 2001 From: Grian Date: Sun, 8 Aug 2021 05:33:52 +0300 Subject: [PATCH 12/13] add extra tests --- collections/sum_of_test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index 7facda62af13..1e6ed3d09d4f 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -86,3 +86,35 @@ Deno.test("[collections/sumOf] On mixed object properties", () => { assertEquals(actual, -15); }); + +Deno.test("[collections/sumOf] No mutation", () => { + const array = [1, 2, 3, 4]; + + sumOf(array, (i) => i + 2); + + assertEquals(array, [1, 2, 3, 4]) +}) + +Deno.test("[collections/sumOf] Empty array results in 0", () => { + const array: number[] = []; + + const actual = sumOf(array, (i) => i + 2); + + assertEquals(actual, 0); +}) + +Deno.test("[collections/sumOf] NaN and Infinity", () => { + const array = [1, 2, Number.POSITIVE_INFINITY, 3, 4, Number.NEGATIVE_INFINITY, 5, 6, Number.NaN, 7, 8]; + + const actual = sumOf(array, (i) => i); + + assertEquals(actual, NaN); +}) + +Deno.test("[collections/sumOf] Infinity", () => { + const array = [1, 2, Infinity, 3, 4, 5, 6, 7, 8]; + + const actual = sumOf(array, (i) => i); + + assertEquals(actual, Infinity); +}) \ No newline at end of file From e8e1d05c10267827b584a057b55728439c71361a Mon Sep 17 00:00:00 2001 From: Grian Date: Sun, 8 Aug 2021 05:37:10 +0300 Subject: [PATCH 13/13] test fmt --- collections/sum_of_test.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/collections/sum_of_test.ts b/collections/sum_of_test.ts index 1e6ed3d09d4f..c4af0312d01c 100644 --- a/collections/sum_of_test.ts +++ b/collections/sum_of_test.ts @@ -91,30 +91,42 @@ Deno.test("[collections/sumOf] No mutation", () => { const array = [1, 2, 3, 4]; sumOf(array, (i) => i + 2); - - assertEquals(array, [1, 2, 3, 4]) -}) + + assertEquals(array, [1, 2, 3, 4]); +}); Deno.test("[collections/sumOf] Empty array results in 0", () => { const array: number[] = []; - + const actual = sumOf(array, (i) => i + 2); assertEquals(actual, 0); -}) +}); Deno.test("[collections/sumOf] NaN and Infinity", () => { - const array = [1, 2, Number.POSITIVE_INFINITY, 3, 4, Number.NEGATIVE_INFINITY, 5, 6, Number.NaN, 7, 8]; + const array = [ + 1, + 2, + Number.POSITIVE_INFINITY, + 3, + 4, + Number.NEGATIVE_INFINITY, + 5, + 6, + Number.NaN, + 7, + 8, + ]; const actual = sumOf(array, (i) => i); assertEquals(actual, NaN); -}) +}); Deno.test("[collections/sumOf] Infinity", () => { const array = [1, 2, Infinity, 3, 4, 5, 6, 7, 8]; const actual = sumOf(array, (i) => i); - assertEquals(actual, Infinity); -}) \ No newline at end of file + assertEquals(actual, Infinity); +});