* refactor: eliminate eslint errors as many as I can * refactor: fix more eslint errors in the record module * LINT: fix @typescript-eslint/unbound-method * LINT: fix all eslint errors in source code * LINT: fix as many eslint warnings as possible Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
export function parseCSSText(cssText: string): Record<string, string> {
|
|
const res: Record<string, string> = {};
|
|
const listDelimiter = /;(?![^(]*\))/g;
|
|
const propertyDelimiter = /:(.+)/;
|
|
const comment = /\/\*.*?\*\//g;
|
|
cssText
|
|
.replace(comment, '')
|
|
.split(listDelimiter)
|
|
.forEach(function (item) {
|
|
if (item) {
|
|
const tmp = item.split(propertyDelimiter);
|
|
tmp.length > 1 && (res[camelize(tmp[0].trim())] = tmp[1].trim());
|
|
}
|
|
});
|
|
return res;
|
|
}
|
|
|
|
export function toCSSText(style: Record<string, string>): string {
|
|
const properties = [];
|
|
for (const name in style) {
|
|
const value = style[name];
|
|
if (typeof value !== 'string') continue;
|
|
const normalizedName = hyphenate(name);
|
|
properties.push(`${normalizedName}: ${value};`);
|
|
}
|
|
return properties.join(' ');
|
|
}
|
|
|
|
/**
|
|
* Camelize a hyphen-delimited string.
|
|
*/
|
|
const camelizeRE = /-([a-z])/g;
|
|
const CUSTOM_PROPERTY_REGEX = /^--[a-zA-Z0-9-]+$/;
|
|
export const camelize = (str: string): string => {
|
|
if (CUSTOM_PROPERTY_REGEX.test(str)) return str;
|
|
return str.replace(camelizeRE, (_, c: string) => (c ? c.toUpperCase() : ''));
|
|
};
|
|
|
|
/**
|
|
* Hyphenate a camelCase string.
|
|
*/
|
|
const hyphenateRE = /\B([A-Z])/g;
|
|
export const hyphenate = (str: string): string => {
|
|
return str.replace(hyphenateRE, '-$1').toLowerCase();
|
|
};
|