components.table.utils.tables.js Maven / Gradle / Ivy
import { isNumber } from 'lodash';
import Types from 'types/types';
import { isString } from 'utils/utils';
export function styleValueToNumber(value) {
if (isNumber(value))
return value
else if (isString(value)) {
const regex = /\d+(?:\.\d+)?(?=px)/i; // also match with % of word boundary: change 'px' to 'px|%|\b'
const match = value.match(regex)
return match ? Number(match[0]) : 0
}
console.log("Could not convert '%o' to number", value)
return 0
}
export function getMinWidth(columns) {
if (Array.isArray(columns)) {
return columns.reduce((partialSum,value) => partialSum + getMinWidth(value), 0)
} else if (Types.isObject(columns)) {
if (Object.hasOwn(columns, 'minWidth'))
return styleValueToNumber(columns.minWidth)
else if (Object.hasOwn(columns, 'width'))
return styleValueToNumber(columns.width)
else
return getMinWidth(Object.values(columns))
}
return 1
}