From 66c005657ffcb2252e43a2aaf0538f61efa593e9 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 11 Jan 2023 09:58:04 -0500 Subject: [PATCH] Add some technical tests Signed-off-by: Sebastian Malton --- src/common/utils/__tests__/iter.test.ts | 28 ++++++++++++++++++++++++- src/common/utils/iter.ts | 12 +++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/common/utils/__tests__/iter.test.ts b/src/common/utils/__tests__/iter.test.ts index e41894e662..2489649d90 100644 --- a/src/common/utils/__tests__/iter.test.ts +++ b/src/common/utils/__tests__/iter.test.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { join, nth, reduce } from "../iter"; +import { join, nth, reduce, concat } from "../iter"; describe("iter", () => { describe("reduce", () => { @@ -39,4 +39,30 @@ describe("iter", () => { expect(nth(["a", "b"], 0)).toBe("a"); }); }); + + describe("concat", () => { + it("should yield undefined for empty args", () => { + const iter = concat(); + + expect(iter.next()).toEqual({ done: true }); + }); + + it("should yield undefined for only empty args", () => { + const iter = concat([].values(), [].values(), [].values(), [].values()); + + expect(iter.next()).toEqual({ done: true }); + }); + + it("should yield all of the first and then all of the second", () => { + const iter = concat([1, 2, 3].values(), [4, 5, 6].values()); + + expect(iter.next()).toEqual({ done: false, value: 1 }); + expect(iter.next()).toEqual({ done: false, value: 2 }); + expect(iter.next()).toEqual({ done: false, value: 3 }); + expect(iter.next()).toEqual({ done: false, value: 4 }); + expect(iter.next()).toEqual({ done: false, value: 5 }); + expect(iter.next()).toEqual({ done: false, value: 6 }); + expect(iter.next()).toEqual({ done: true }); + }); + }); }); diff --git a/src/common/utils/iter.ts b/src/common/utils/iter.ts index a1156e16cf..dc1c4621fd 100644 --- a/src/common/utils/iter.ts +++ b/src/common/utils/iter.ts @@ -239,12 +239,10 @@ export function every(src: Iterable, fn: (val: T) => any): boolean { return true; } -export function* concat(src1: IterableIterator, src2: IterableIterator): IterableIterator { - for (const val of src1) { - yield val; - } - - for (const val of src2) { - yield val; +export function* concat(...sources: IterableIterator[]): IterableIterator { + for (const source of sources) { + for (const val of source) { + yield val; + } } }