Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Guf-Hub committed Nov 19, 2023
1 parent a19d31d commit 44ef9eb
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions bot/2 Validate functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ function extractTextFromMessage(message) {
return message?.text;
}

/**
* Checks if the given message is not valid.
*
* @param {string} message - The message to be checked.
* @return {boolean} Returns true if the message is not valid, false otherwise.
*/
function isNoValide(message) {
if (
/([A-Za-zА-Яа-я]+[0-9]+|[0-9]+[A-Za-zА-Яа-я]+|^\d{3,}|[A-Za-zА-Яа-я])/gm.test(
Expand All @@ -163,6 +169,12 @@ function isNoValide(message) {
else return false;
}

/**
* Checks if the given message is a valid date.
*
* @param {string} message - The message to be checked.
* @return {boolean} - Returns true if the message is a valid date, otherwise returns false.
*/
function isValideDate(message) {
if (
!!/^\d{2}\.\d{2}\.\d{4}$/g.exec(
Expand All @@ -173,46 +185,76 @@ function isValideDate(message) {
else return false;
}

// function isBadWord(message) {
// const text = message?.text
// ? message.text
// : message?.caption
// ? message.caption
// : null;
// if (text) {
// const words = text
// .toLowerCase()
// .replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, " ")
// .split(" ")
// .filter((e) => e);
// for (const word of words)
// if (badWordsArray.includes(word)) return true;
// else return false;
// } else return false;
// }

/**
* Checks if a given message contains any bad words.
*
* @param {object} message - The message object that contains the text or caption.
* @returns {boolean} True if the message contains any bad words, false otherwise.
*/
function isBadWord(message) {
const text = message?.text
? message.text
: message?.caption
? message.caption
: null;
const text = message?.text || message?.caption || null;
if (text) {
const words = text
.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, " ")
.split(" ")
.filter((e) => e);
for (const word of words)
if (badWordsArray.includes(word)) return true;
else return false;
} else return false;
.filter(Boolean);
return words.some((word) => badWordsArray.includes(word));
}
return false;
}

/**
* Replaces bad words in a message with asterisks.
*
* @param {string} message - The message to be processed.
* @return {string|boolean} The processed message with bad words replaced by asterisks, or `false` if no message was provided.
*/
function replaceBadWords(message) {
// const replacer = (word) => {
// for (let i = 0; i < badWordsArray.length; i++) {
// const badWord = badWordsArray[i];
// if (word.includes(badWord)) {
// return replacer(word.replace(badWord, "*".repeat(badWord.length)));
// }
// }
// return word;
// };

const replacer = (word) => {
for (let i = 0; i < badWordsArray.length; i++) {
const badWord = badWordsArray[i];
for (const badWord of badWordsArray) {
if (word.includes(badWord)) {
return replacer(word.replace(badWord, "*".repeat(badWord.length)));
}
}
return word;
};

const text = message?.text
? message.text
: message?.caption
? message.caption
: null;
const text = message?.text || message?.caption || null;
if (text) {
return text
.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, " ")
.split(" ")
.filter((e) => e)
.filter(Boolean)
.map(replacer)
.join(" ");
} else return false;
Expand Down

0 comments on commit 44ef9eb

Please sign in to comment.