UNPKG

hd-utils

Version:

A handy utils for modern JS developers

29 lines (28 loc) 923 B
import camelCaseToKebab from '../string/camelCaseToKebab'; import isObject from '../validation/isObject'; /** * @description will convert any css style object into string. * @example cssPropertiesToString({backgroundColor:"blue"}) // background-color:blue; */ export default function cssPropertiesToString(sx) { if (!sx) return ''; const convertNestedObjectToString = (nestedSx) => { return Object.entries(nestedSx) .map(([nestedKey, nestedValue]) => { return `${camelCaseToKebab(nestedKey)}: ${nestedValue};`; }) .join(' '); }; return Object.entries(sx) .map(([key, value]) => { const cssKey = camelCaseToKebab(key); if (isObject(value)) { return `${cssKey} {${convertNestedObjectToString(value)}}`; } else { return `${cssKey}: ${value};`; } }) .join(' '); }