@kotori-bot/core
Version:
Kotori Core
80 lines (79 loc) • 3 kB
JavaScript
/**
* @Package @kotori-bot/core
* @Version 1.7.2
* @Author Arimura Sena <me@hotaru.icu>
* @Copyright 2024-2025 Hotaru. All rights reserved.
* @License GPL-3.0
* @Link https://github.com/kotorijs/kotori
* @Date 2026/2/14 15:50:13
*/
;
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);
var factory_exports = {};
__export(factory_exports, {
formatFactory: () => formatFactory
});
module.exports = __toCommonJS(factory_exports);
var import_i18n = require("@kotori-bot/i18n");
var import_tools = require("@kotori-bot/tools");
var import_components = require("../components");
function formatFactory(i18n = new import_i18n.I18n()) {
function format(template, data) {
if (!Array.isArray(data)) {
for (const key of Object.keys(data)) {
if (data[key] === void 0 || data[key] === null) continue;
if (typeof data[key] !== "string") data[key] = String(data[key]);
data[key] = i18n.locale(data[key]);
}
return (0, import_tools.stringTemp)(i18n.locale(template), data);
}
const isPureString = (Array.isArray(data) ? data : Object.values(data)).every(
(value) => !(value instanceof import_components.MessageList || value instanceof import_components.MessageSingle)
);
const parts = i18n.locale(template).split(/(\{[0-9]+\})/);
const result = [];
let currentString = "";
for (const part of parts) {
if (part === void 0 || part === null) continue;
if (!part.match(/^\{[0-9]+\}$/)) {
currentString += part;
continue;
}
const index = Number.parseInt(part.slice(1, -1), 10);
const value = data[index];
if (value === void 0 || value === null) continue;
if (value instanceof import_components.MessageList || value instanceof import_components.MessageSingle) {
if (currentString) {
result.push(currentString);
currentString = "";
}
result.push(value);
} else {
currentString += String(value);
}
}
if (currentString) result.push(currentString);
return isPureString ? result.join("") : (0, import_components.Messages)(...result);
}
return format;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
formatFactory
});