chipmunkbot3/util/chat/stringify/plain.js
Chipmunk 5f3560910b Refactor the chat system
not sure if it's better or worse now. also, i have tried to optimize color codes, but this optimization seems unreliable (i might fix or remove it in the future)
2024-03-17 23:52:58 -04:00

43 lines
982 B
JavaScript

const { normalize } = require('../utility')
function plainStringify (text, { lang = {} } = {}) {
text = normalize(text)
let string = ''
if (text.text != null) string += text.text
else if (text.translate != null) {
let format
if (Object.hasOwn(lang, text.translate)) format = lang[text.translate]
else if (text.fallback != null) format = text.fallback
else format = text.translate
const _with = text.with || []
let i = 0
string += format.replace(/%(?:(\d+)\$)?(s|%)/g, (g0, g1) => {
if (g0 === '%%') return '%'
const idx = g1 ? parseInt(g1) : i++
if (_with[idx]) {
return plainStringify(_with[idx], { lang })
}
return ''
})
}
else if (text.selector != null) string += text.selector
else if (text.keybind) {
// TODO
}
if (text.extra) {
for (const extra of text.extra) {
string += plainStringify(extra, { lang })
}
}
return string
}
module.exports = plainStringify