From: Jérôme Benoit Date: Thu, 26 Jan 2023 18:27:54 +0000 (+0100) Subject: Improve isIterable() UT X-Git-Tag: v1.1.92~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=afb26ef122e3517f1b6a32c14d7ffe92c0ae35a0;p=e-mobility-charging-stations-simulator.git Improve isIterable() UT Signed-off-by: Jérôme Benoit --- 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 {