schat2-clive/src/utils/style.js

69 lines
1.2 KiB
JavaScript

'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 (text) {
return fn(text);
}
return '';
};
}
function bypass(text) {
return text;
}
function shieldMethods(methods) {
return Object.fromEntries(Object.entries(methods).map(([key, method]) => [key, curate(method)]));
}
module.exports = (() => {
if (config.platform === 'irc') {
return shieldMethods({
...styles,
code: bypass,
});
}
if (config.platform === 'schat') {
const methods = {
bold: schatBold,
italic: schatItalic,
code: schatCode,
...Object.fromEntries(Object.entries(config.schatColors).map(([color, value]) => [color, (text) => schatColor(text, value)])),
};
const handler = {
get(target, prop) {
return target[prop] || bypass;
},
};
const shieldedMethods = shieldMethods(methods);
return new Proxy(shieldedMethods, handler);
}
return null;
})();