ljf-common-utils
Version:
159 lines (157 loc) • 4.41 kB
JavaScript
// src/utils/text.ts
function convertNum(num, needUnit = true, locale = "en-US") {
if (needUnit) {
if (locale === "en-US") {
if (Math.abs(num) / 1e3 / 1e3 / 1e3 > 1) {
return (num / 1e3 / 1e3 / 1e3).toFixed(2) + " billion";
} else if (Math.abs(num) / 1e3 / 1e3 > 1) {
return (num / 1e3 / 1e3).toFixed(2) + " million";
} else if (Math.abs(num) / 1e3 > 1) {
return Math.round(num / 1e3) + " thousand";
} else {
return Math.round(num) + "";
}
} else {
if (Math.abs(num) / 1e4 / 1e4 > 1) {
return (num / 1e4 / 1e4).toFixed(2) + " \u4EBF";
} else if (Math.abs(num) / 1e4 > 1) {
return (num / 1e4).toFixed(2) + " \u4E07";
} else {
return num + "";
}
}
} else {
if (locale === "en-US") {
if (Math.abs(num) / 1e3 / 1e3 / 1e3 > 1) {
return (num / 1e3 / 1e3 / 1e3).toFixed(2);
} else if (Math.abs(num) / 1e3 / 1e3 > 1) {
return (num / 1e3 / 1e3).toFixed(2);
} else if (Math.abs(num) / 1e3 > 1) {
return Math.round(num / 1e3);
} else {
return Math.round(num);
}
} else {
if (Math.abs(num) / 1e4 / 1e4 > 1) {
return (num / 1e4 / 1e4).toFixed(2);
} else if (Math.abs(num) / 1e4 > 1) {
return (num / 1e4).toFixed(2);
} else {
return num;
}
}
}
}
function convertNumUnit(n, locale = "en-US") {
const num = Math.abs(n);
if (locale === "en-US") {
if (num / 1e3 / 1e3 / 1e3 > 1) {
return "billion";
} else if (num / 1e3 / 1e3 > 1) {
return "million";
} else if (num / 1e3 > 1) {
return "thousand";
} else {
return "";
}
} else {
if (num / 1e4 / 1e4 > 1) {
return "\u4EBF";
} else if (num / 1e4 > 1) {
return "\u4E07";
} else {
return "";
}
}
}
function convertStorageUnit(num) {
if (num / 1024 / 1024 / 1024 / 1024 > 1) {
return "TB";
} else if (num / 1024 / 1024 / 1024 > 1) {
return "GB";
} else if (num / 1024 / 1024 > 1) {
return "MB";
} else if (num / 1024 > 1) {
return "KB";
} else {
return "B";
}
}
function convertStorageNum(unit, num) {
switch (unit) {
case "KB":
return Math.round(num / 1024);
case "MB":
return (num / 1024 / 1024).toFixed(2);
case "GB":
return (num / 1024 / 1024 / 1024).toFixed(2);
case "TB":
return (num / 1024 / 1024 / 1024 / 1024).toFixed(2);
default:
return num;
}
}
function convertStorageSize(num) {
if (num / 1024 / 1024 / 1024 / 1024 > 1) {
return (num / 1024 / 1024 / 1024 / 1024).toFixed(2) + " TB";
} else if (num / 1024 / 1024 / 1024 > 1) {
return (num / 1024 / 1024 / 1024).toFixed(2) + " GB";
} else if (num / 1024 / 1024 > 1) {
return (num / 1024 / 1024).toFixed(2) + " MB";
} else if (num / 1024 > 1) {
return Math.round(num / 1024) + " KB";
} else {
return `${Number(num)} B`;
}
}
// src/utils/index.ts
var changeToStar = (str, len1, len2 = 0) => {
const strLen = str.length;
let stars = "";
for (let i = 0; i < strLen - len1 - len2; i++) {
stars += "*";
}
str = str.substring(0, len1) + stars + str.substring(strLen - len2);
return str;
};
function awaitTo(awaited, errorExt) {
const promise = Promise.resolve(awaited);
return promise.then((data) => [null, data]).catch((err) => {
if (errorExt) {
const parsedError = Object.assign({}, err, errorExt);
return [parsedError, undefined];
}
return [err, undefined];
});
}
var deepFind = (list, fn, childName = "children") => {
const res = [];
const traverse = function(node) {
node.forEach((item) => {
if (fn(item)) {
res.push(item);
} else {
item[childName] && traverse(item[childName]);
}
});
};
traverse(list);
return res;
};
var errorHandling = (fn, args) => {
let result;
try {
result = args ? fn(...args) : fn();
} catch (error) {
console.error(error);
}
return result;
};
var getFileExtension = (filename) => {
if (!filename) return "";
const match = filename.match(/\.([^.]+)$/);
return match ? match[1] : "";
};
export { awaitTo, changeToStar, convertNum, convertNumUnit, convertStorageNum, convertStorageSize, convertStorageUnit, deepFind, errorHandling, getFileExtension };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map