schat2-clive/src/utils/style.js

77 lines
1.5 KiB
JavaScript
Executable File

'use strict';
const config = require('config');
const styles = require('irc-colors');
function schatBold(text) {
return `**${text}**`;
}
function schatItalic(text) {
return `*${text}*`;
}
function schatColor(text, color) {
return `{${color}}(${text})`;
}
function schatCode(text) {
return `\`${text}\``;
}
function curate(fn) {
return (text) => {
if (typeof text !== 'undefined' && text !== null) {
return fn(text);
}
return '';
};
}
function bypass(text) {
return text;
}
function shieldMethods(methods) {
return Object.fromEntries(Object.entries(methods).map(([key, method]) => [key, curate(method)]));
}
const styleMethods = (() => {
if (config.platform === 'irc') {
return {
...styles,
forest: styles.green,
code: bypass,
};
}
if (config.platform === 'schat') {
return {
bold: schatBold,
italic: schatItalic,
code: schatCode,
...Object.fromEntries(Object.entries(config.schatColorAliases).map(([color, value]) => [color, (text) => schatColor(text, value)])),
};
}
return null;
})();
const expandedMethods = {
...styleMethods,
username: (value) => styleMethods.bold(styleMethods.cyan(value)),
time: (value) => styleMethods.bold(styleMethods.green(value)),
answer: (value) => styleMethods.bold(styleMethods.yellow(value)),
};
const handler = {
get(target, prop) {
return target[prop] || bypass;
},
};
const shieldedMethods = shieldMethods(expandedMethods);
module.exports = new Proxy(shieldedMethods, handler);