From a7037f8c6131e0891a7c575647a9025f93190d1d Mon Sep 17 00:00:00 2001 From: Panu Horsmalahti Date: Tue, 31 Aug 2021 13:56:49 +0300 Subject: [PATCH] Add test to iter reduce Signed-off-by: Panu Horsmalahti --- src/common/utils/__tests__/iter.test.ts | 34 +++++++++++++++++++++++++ src/common/utils/iter.ts | 1 + 2 files changed, 35 insertions(+) create mode 100644 src/common/utils/__tests__/iter.test.ts diff --git a/src/common/utils/__tests__/iter.test.ts b/src/common/utils/__tests__/iter.test.ts new file mode 100644 index 0000000000..fd4677df0d --- /dev/null +++ b/src/common/utils/__tests__/iter.test.ts @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +import { reduce } from "../iter"; + +describe("iter", () => { + describe("reduce", () => { + it("can reduce a value", () => { + expect(reduce([1, 2, 3], (acc: number[], current: number) => [current, ...acc], [0])).toEqual([3, 2, 1, 0]); + }); + + it("can reduce an empty iterable", () => { + expect(reduce([], (acc: number[], current: number) => [acc[0] + current], [])).toEqual([]); + }); + }); +}); diff --git a/src/common/utils/iter.ts b/src/common/utils/iter.ts index 2a91b6afd9..65b0937726 100644 --- a/src/common/utils/iter.ts +++ b/src/common/utils/iter.ts @@ -160,6 +160,7 @@ export function find(src: Iterable, match: (i: T) => any): T | undefined { /** * Iterate over `src` calling `reducer` with the previous produced value and the current * yielded value until `src` is exausted. Then return the final value. + * @param src The value to iterate over * @param reducer A function for producing the next item from an accumilation and the current item * @param initial The initial value for the iteration */