@yookue/ts-lang-utils
Version:
Common lang utilities for typescript
115 lines (113 loc) • 4.17 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/util/StringUtils/formatPercent.ts
var formatPercent_exports = {};
__export(formatPercent_exports, {
formatPercent: () => formatPercent
});
module.exports = __toCommonJS(formatPercent_exports);
var import_minLength = require("../ArrayUtils/minLength");
var import_isPlain = require("../ObjectUtils/isPlain");
var import_toString = require("../ObjectUtils/toString");
function formatPercent(text, ...params) {
if (!text || text.length <= 2 || !params || !params.length) {
return text;
}
const matches = text.match(/%[bcdfjosxX]/g) || [];
const count = (0, import_minLength.minLength)(matches, params);
if (count === 0) {
return text;
}
let result = text;
for (let i = 0; i < count; i++) {
const param = params[i];
const pattern = matches[i].substring(1);
switch (pattern) {
case "b":
case "c":
case "d":
case "o":
case "x":
case "X":
try {
let value = void 0;
if (typeof param === "string") {
value = Number.parseInt(param);
} else if (param instanceof String) {
value = Number.parseInt(param.toString());
} else if (typeof param === "number") {
value = param;
}
if (value) {
if (pattern === "b") {
result = result.replace(`%${pattern}`, value.toString(2));
} else if (pattern === "c") {
result = result.replace(`%${pattern}`, String.fromCharCode(value));
} else if (pattern === "d") {
result = result.replace(`%${pattern}`, value.toString(10));
} else if (pattern === "o") {
result = result.replace(`%${pattern}`, "0" + value.toString(8));
} else if (pattern === "x") {
result = result.replace(`%${pattern}`, "0x" + value.toString(16));
} else if (pattern === "X") {
result = result.replace(`%${pattern}`, "0x" + value.toString(16).toUpperCase());
}
}
} catch {
throw new TypeError(`Invalid parameter type of '${param}', index ${i}`);
}
break;
case "f":
try {
let value = void 0;
if (typeof param === "string") {
value = Number.parseFloat(param);
} else if (param instanceof String) {
value = Number.parseFloat(param.toString());
} else if (typeof param === "number") {
value = param;
}
if (value) {
result = result.replace(`%${pattern}`, "0x" + value.toString());
}
} catch {
throw new TypeError(`Invalid parameter type of '${param}', index ${i}`);
}
break;
case "j":
if (param === void 0 || param === null) {
result = result.replace(`%${pattern}`, "");
break;
} else if ((0, import_isPlain.isPlain)(param)) {
result = result.replace(`%${pattern}`, JSON.stringify(param));
break;
}
throw new TypeError(`Invalid parameter type of '${param}', index ${i}`);
case "s":
result = result.replace(`%${pattern}`, (0, import_toString.toString)(param, ""));
break;
default:
break;
}
}
return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
formatPercent
});