2020-04-28 09:47:35 +00:00
|
|
|
/**
|
|
|
|
* Regular expressions for calculating dimensions
|
|
|
|
*/
|
|
|
|
const unitsSplit = /(-?[0-9.]*[0-9]+[0-9.]*)/g;
|
|
|
|
const unitsTest = /^-?[0-9.]*[0-9]+[0-9.]*$/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate second dimension when only 1 dimension is set
|
|
|
|
*/
|
2020-12-22 12:49:02 +00:00
|
|
|
export function calculateSize(
|
2020-04-28 09:47:35 +00:00
|
|
|
size: string | number,
|
|
|
|
ratio: number,
|
|
|
|
precision?: number
|
|
|
|
): string | number {
|
|
|
|
if (ratio === 1) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
precision = precision === void 0 ? 100 : precision;
|
|
|
|
if (typeof size === 'number') {
|
|
|
|
return Math.ceil(size * ratio * precision) / precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof size !== 'string') {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split code into sets of strings and numbers
|
|
|
|
const oldParts = size.split(unitsSplit);
|
|
|
|
if (oldParts === null || !oldParts.length) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newParts = [];
|
|
|
|
let code = oldParts.shift() as string;
|
|
|
|
let isNumber = unitsTest.test(code as string);
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
while (true) {
|
|
|
|
if (isNumber) {
|
|
|
|
const num = parseFloat(code);
|
|
|
|
if (isNaN(num)) {
|
|
|
|
newParts.push(code);
|
|
|
|
} else {
|
|
|
|
newParts.push(Math.ceil(num * ratio * precision) / precision);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newParts.push(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// next
|
|
|
|
code = oldParts.shift() as string;
|
|
|
|
if (code === void 0) {
|
|
|
|
return newParts.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
isNumber = !isNumber;
|
|
|
|
}
|
|
|
|
}
|