Urara-Blog/node_modules/.pnpm-store/v3/files/99/f1a6c5dc215022ba630688761b334f4675bc2e4867835b331649c21a79b497f8ea7309bac4ff5945219ec31c963da6ecf21e484e83fd50f4a5f7c6a6b57583
2022-08-14 01:14:53 +08:00

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;