cqcode
Version:
CQCode Helper for NodeJS.
34 lines (33 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
/**
* @param { string } text 即将被解析的CQCode字符串,以[开始,以]结束。
* @return { CQCode } 返回CQCode对象。
*/
const parse = (text) => {
if (text.substring(0, 1) !== '[' || text.substring(text.length - 1) !== ']') {
throw new Error('Not a valid CQCode string.');
}
const splitArr = text.substring(1, text.length - 1).split(',');
const cqCodeTypeArr = (splitArr[0] || '').split(':');
if (cqCodeTypeArr.length !== 2) {
throw new Error('CQ:{type} field invalid.');
}
if (cqCodeTypeArr[0] !== 'CQ') {
throw new Error('Could not find CQ:{type} field.');
}
const cqCodeType = cqCodeTypeArr[1].toLowerCase();
const cqCodeData = {};
for (let i = 1; i < splitArr.length; i++) {
const cqCodeDataArr = splitArr[i].split('=');
if (cqCodeDataArr.length > 1) {
cqCodeData[cqCodeDataArr[0]] = cqCodeDataArr[1];
}
}
return {
type: cqCodeType,
data: cqCodeData,
};
};
exports.parse = parse;