From afb26ef122e3517f1b6a32c14d7ffe92c0ae35a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 26 Jan 2023 19:27:54 +0100 Subject: [PATCH] Improve isIterable() UT MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 2 +- test/utils/UtilsTest.ts | 7 ++++++- ui/web/src/composables/Utils.ts | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 6557a588..f81acb1c 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -184,7 +184,7 @@ export default class Utils { return clone(object); } - public static isIterable>(obj: T): boolean { + public static isIterable(obj: T): boolean { return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; } diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index 21d5d137..987ba7fb 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -151,7 +151,12 @@ describe('Utils test suite', () => { expect(Utils.isIterable('test')).toBe(true); expect(Utils.isIterable(null)).toBe(false); expect(Utils.isIterable(undefined)).toBe(false); - expect(Utils.isIterable([])).toBe(true); + expect(Utils.isIterable([0, 1])).toBe(true); + expect(Utils.isIterable({ 1: 1 })).toBe(false); + expect(Utils.isIterable(new Map())).toBe(true); + expect(Utils.isIterable(new Set())).toBe(true); + expect(Utils.isIterable(new WeakMap())).toBe(false); + expect(Utils.isIterable(new WeakSet())).toBe(false); }); it('Verify isEmptyString()', () => { diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 92f687d8..8ce3b6ec 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -9,11 +9,11 @@ export default class Utils { return value as T; } - public static isIterable>(obj: T): boolean { + public static isIterable(obj: T): boolean { if (obj === null || obj === undefined) { return false; } - return typeof obj[Symbol.iterator] === 'function'; + return typeof (obj as any)[Symbol.iterator] === 'function'; } // public static ifNotIterableDo(obj: T, cb: () => void): void { -- 2.34.1