From 87f82a940257f8d6814cf6af47fa8f1b66573eea Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Oct 2022 10:54:03 +0200 Subject: [PATCH] Implement an optimized (20x) version of isEmptyObject() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also fix isEmptyString() corner case issues Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 => -- 2.34.1