From a7e74ee4d5db9f47f88c1c88352aa85b65df4a2a Mon Sep 17 00:00:00 2001 From: Justice Almanzar Date: Sat, 15 Jul 2023 22:50:21 +0000 Subject: [PATCH] classNameFactory: Allow (& ignore) all sorts of falsy values (#1427) --- src/api/Styles.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/Styles.ts b/src/api/Styles.ts index 6b189cab..6b0ac2cd 100644 --- a/src/api/Styles.ts +++ b/src/api/Styles.ts @@ -141,7 +141,7 @@ export const compileStyle = (style: Style) => { */ export const classNameToSelector = (name: string, prefix = "") => name.split(" ").map(n => `.${prefix}${n}`).join(""); -type ClassNameFactoryArg = string | string[] | Record; +type ClassNameFactoryArg = string | string[] | Record | false | null | undefined | 0 | ""; /** * @param prefix The prefix to add to each class, defaults to `""` * @returns A classname generator function @@ -154,9 +154,9 @@ type ClassNameFactoryArg = string | string[] | Record; export const classNameFactory = (prefix: string = "") => (...args: ClassNameFactoryArg[]) => { const classNames = new Set(); for (const arg of args) { - if (typeof arg === "string") classNames.add(arg); + if (arg && typeof arg === "string") classNames.add(arg); else if (Array.isArray(arg)) arg.forEach(name => classNames.add(name)); - else if (typeof arg === "object") Object.entries(arg).forEach(([name, value]) => value && classNames.add(name)); + else if (arg && typeof arg === "object") Object.entries(arg).forEach(([name, value]) => value && classNames.add(name)); } return Array.from(classNames, name => prefix + name).join(" "); };