@kamuridesu/whatframework
Version:
A simple WhatsApp Bot Framework on top of Baileys
51 lines (50 loc) • 1.45 kB
JavaScript
function parseTextWithQuotation(text) {
let words = [];
let inQuotes = false;
let currentWord = "";
for (let i = 0; i < text.length; i++) {
let char = text.charAt(i);
if (char === '"') {
inQuotes = !inQuotes;
}
else if (char === " " && !inQuotes) {
words.push(currentWord);
currentWord = "";
}
else {
currentWord += char;
}
}
words.push(currentWord);
return words;
}
function checkMentionsInText(text) {
const regex = /@[0-9]{11,13}/g;
const matches = text.match(regex);
if (matches) {
return matches.map((number) => number.replace("@", "") + "@s.whatsapp.net");
}
return [];
}
function checkJidInTextAndConvert(text) {
const regex = /[0-9]{11,13}@s\.whatsapp\.net/;
while (regex.test(text)) {
text = text.replace(regex, "@" + text.match(regex)[0].replace(/@s\.whatsapp\.net/, ""));
}
return {
text: text,
mentions: checkMentionsInText(text),
};
}
String.prototype.format = function (valuesMap) {
let s = this;
for (let key of Object.keys(valuesMap)) {
const value = `\$${key}`;
s = s.replaceAll(value, valuesMap[key]);
}
return s;
};
function stringFormat(text, valuesMap) {
return text.format(valuesMap);
}
export { parseTextWithQuotation, checkMentionsInText, checkJidInTextAndConvert, stringFormat };