@twurple/common
Version:
Common functions used by the `@twurple` library family.
66 lines (65 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCheermoteList = void 0;
const shared_utils_1 = require("@d-fischer/shared-utils");
const DataObject_1 = require("../DataObject");
const messagePartParser_1 = require("./messagePartParser");
/** @private */
class BaseCheermoteList extends DataObject_1.DataObject {
/**
* Parses all the cheermotes out of a message.
*
* @deprecated Use {@link parseChatMessage} instead.
*
* @param message The message.
* @param format The format to show the cheermotes in.
*/
parseMessage(message, format) {
const result = [];
const names = this.getPossibleNames();
const re = new RegExp('(?<=^|\\s)([a-z]+(?:\\d+[a-z]+)*)(\\d+)(?=\\s|$)', 'gi');
let match = null;
while ((match = re.exec(message))) {
const name = match[1].toLowerCase();
if (names.includes(name)) {
const amount = Number(match[2]);
result.push({
name,
amount,
position: (0, shared_utils_1.utf8Length)(message.slice(0, match.index)),
length: match[0].length,
displayInfo: this.getCheermoteDisplayInfo(name, amount, format)
});
}
}
return result;
}
/**
* Transforms all the cheermotes in a message and returns an array of all the message parts.
*
* @deprecated Use {@link parseChatMessage} instead.
*
* @param message The message.
* @param format The format to show the cheermotes in.
* @param transformer A function that transforms a message part into an arbitrary structure.
*/
transformCheerMessage(message, format, transformer) {
const result = [];
let currentPosition = 0;
for (const foundCheermote of (0, messagePartParser_1.findCheermotePositions)(message, this.getPossibleNames())) {
if (currentPosition < foundCheermote.position) {
result.push(message.substring(currentPosition, foundCheermote.position));
}
result.push(transformer({
...foundCheermote,
displayInfo: this.getCheermoteDisplayInfo(foundCheermote.name, foundCheermote.amount, format)
}));
currentPosition = foundCheermote.position + foundCheermote.length;
}
if (currentPosition < message.length) {
result.push(message.slice(currentPosition));
}
return result;
}
}
exports.BaseCheermoteList = BaseCheermoteList;