irctokens
Version:
RFC1459 and IRCv3 protocol tokeniser
46 lines • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.format = void 0;
const const_1 = require("./const");
function escapeTag(value) {
let processed = value;
for (let idx = 0; idx < const_1.TAG_ESCAPED.length; idx++) {
processed = processed.replace(const_1.TAG_UNESCAPED_REGEX[idx], const_1.TAG_ESCAPED[idx]);
}
return processed;
}
function format({ tags, source, command, params }) {
const outs = [];
if (tags) {
const tagsStr = [];
for (const [key, val] of Object.entries(tags).sort(([a], [b]) => a.localeCompare(b))) {
if (val)
tagsStr.push(`${key}=${escapeTag(val)}`);
else
tagsStr.push(key);
}
outs.push(`@${tagsStr.join(';')}`);
}
if (source) {
outs.push(`:${source}`);
}
outs.push(command);
if (params === null || params === void 0 ? void 0 : params.length) {
const paramCopy = [...params];
let last = paramCopy.pop();
for (const param of paramCopy) {
if (param.includes(' '))
throw new TypeError('non last params cannot have spaces');
else if (param.startsWith(':'))
throw new TypeError('non last params cannot start with colon');
}
outs.push(...paramCopy);
if (!last || last.includes(' ') || last.startsWith(':')) {
last = `:${last !== null && last !== void 0 ? last : ''}`;
}
outs.push(last);
}
return outs.join(' ');
}
exports.format = format;
//# sourceMappingURL=formatting.js.map