mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-07 01:39:13 +08:00
44 lines
1.1 KiB
Text
44 lines
1.1 KiB
Text
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
const unitsSplit = /(-?[0-9.]*[0-9]+[0-9.]*)/g;
|
|
const unitsTest = /^-?[0-9.]*[0-9]+[0-9.]*$/g;
|
|
function calculateSize(size, ratio, precision) {
|
|
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;
|
|
}
|
|
const oldParts = size.split(unitsSplit);
|
|
if (oldParts === null || !oldParts.length) {
|
|
return size;
|
|
}
|
|
const newParts = [];
|
|
let code = oldParts.shift();
|
|
let isNumber = unitsTest.test(code);
|
|
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);
|
|
}
|
|
code = oldParts.shift();
|
|
if (code === void 0) {
|
|
return newParts.join("");
|
|
}
|
|
isNumber = !isNumber;
|
|
}
|
|
}
|
|
|
|
exports.calculateSize = calculateSize;
|