apphouse
Version:
Component library for React that uses observable state management and theme-able components.
65 lines (59 loc) • 1.63 kB
text/typescript
/**
* Detects if string has a valid font size unit in it
* @param value
* @returns true it encounters a valid font size unit, otherwise false
*/
export const hasFontSizeUnit = (value: string | number) => {
if (typeof value === 'string') {
if (value.indexOf('rem') >= 0) {
const newValue = value.replace('rem', '');
if (newValue) return true;
}
if (value.indexOf('px') >= 0) {
return true;
}
if (value.indexOf('pt') >= 0) {
return true;
}
if (value.indexOf('sp') >= 0) {
return true;
}
}
return false;
};
/**
* Checks if the value is a valid font size unit
* @param value the string or number value to check
* @returns boolean if true, the value is a valid font size unit
*/
export const isValidFontSizeUnit = (value: string | number): boolean => {
if (typeof value === 'string') {
if (hasFontSizeUnit(value)) {
if (value.indexOf('rem') >= 0) {
const newValue = value.replace('rem', '');
if (newValue) {
return !hasFontSizeUnit(newValue);
}
}
if (value.indexOf('px') >= 0) {
const newValue = value.replace('px', '');
if (newValue) {
return !hasFontSizeUnit(newValue);
}
}
if (value.indexOf('pt') >= 0) {
const newValue = value.replace('pt', '');
if (newValue) {
return !hasFontSizeUnit(newValue);
}
}
if (value.indexOf('sp') >= 0) {
const newValue = value.replace('sp', '');
if (newValue) {
return !hasFontSizeUnit(newValue);
}
}
}
}
return false;
};