Skip to content

Commit e3b9f13

Browse files
committed
Refactor filterEmails method to handle regex option as an array and not crash without errors if not defined.
1 parent a402781 commit e3b9f13

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,18 @@ class MiabClient {
287287
* @param {String} options.subject - The email subject.
288288
* @returns {ParsedMail[]}
289289
*/
290-
filterEmails(emails, options) {
291-
const { regex, from, subject } = options;
290+
filterEmails(emails, options = {}) {
291+
let { regex = [], from = '', subject = '' } = options;
292+
293+
if (!Array.isArray(regex)) {
294+
console.error('The regex option must be an array. Trying to experimentally convert it to an array.');
295+
296+
if (this.#isRegex(regex) || typeof regex === 'string') {
297+
regex = [regex];
298+
} else {
299+
throw new Error('The regex option must be an array and either a valid string or regex. Failed to experimentally convert it to an array.');
300+
}
301+
}
292302

293303
return emails.filter(email => {
294304
// Check if the email matches the regex patterns.

0 commit comments

Comments
 (0)