From: Jérôme Benoit Date: Tue, 25 Oct 2022 08:54:03 +0000 (+0200) Subject: Implement an optimized (20x) version of isEmptyObject() X-Git-Tag: v1.1.86~17 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=87f82a940257f8d6814cf6af47fa8f1b66573eea;p=e-mobility-charging-stations-simulator.git Implement an optimized (20x) version of isEmptyObject() Also fix isEmptyString() corner case issues Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index d396cfa9..1d422af2 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -172,7 +172,7 @@ export default class Utils { } public static isEmptyString(value: unknown): boolean { - return Utils.isString(value) && (value as string).length === 0; + return Utils.isString(value) && (value as string).trim().length === 0; } public static isUndefined(value: unknown): boolean { @@ -195,7 +195,15 @@ export default class Utils { } public static isEmptyObject(obj: object): boolean { - return !Object.keys(obj).length; + if (obj.constructor !== Object) { + return false; + } + // Iterates over the keys of an object, if + // any exist, return false. + for (const _ in obj) { + return false; + } + return true; } public static insertAt = (str: string, subStr: string, pos: number): string =>