2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-22 18:08:51 +00:00
the-guard-bot/utils/cmd/stringify.js
2020-02-19 08:11:32 +01:00

25 lines
563 B
JavaScript

'use strict';
const camelToSnake = s => s.replace(/[A-Z]/g, c => '_' + c.toLowerCase());
exports.stringify = ({ command = '', flags = {}, reason = '' }) => {
const flagS = Object.entries(flags)
.flatMap(([ key, value ]) => {
switch (value) {
case null:
case false:
case undefined: // eslint-disable-line no-undefined
return [];
case true:
return [ '-' + camelToSnake(key) ];
default:
return [ `-${camelToSnake(key)}=${value}` ];
}
}).join(' ');
return [
'/' + command,
flagS,
reason
].filter(Boolean).join(' ');
};