Skip to content

Commit

Permalink
Atualizando getonlyemail
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Aug 2, 2023
1 parent 0f41c68 commit 56ce446
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ validator.FUNCTION_NAME.errorMsg = 'ErrorMsg' // You can customize errors
*/
```

Documentation: https://gabriel-logan.github.io/multiform-validator

```javascript

const { validateEmail } = require('multiform-validator');
Expand Down
24 changes: 12 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ <h1 class="navbar-brand nav-link myTitle">Multiform validator</h1>
<div class="row">
<div class="col-12">
<h1>Welcome to the multiform-validator website</h1>
<p>
<a href="https://badge.fury.io/js/multiform-validator.svg" target="_blank">
<img src="https://badge.fury.io/js/multiform-validator.svg" alt="npm version">
</a>
<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT">
</a>
<a href="https://npm-stat.com/charts.html?package=multiform-validator" target="_blank">
<img src="https://img.shields.io/npm/dm/multiform-validator.svg?style=flat-square" alt="npm downloads">
</a>
</p>
<p>
Using my library, you can convert many lines of code into very few lines and as a result you have a cleaner, stronger and safer code
</p>
Expand Down Expand Up @@ -94,6 +83,17 @@ <h1>Change color mode</h1>
<section>
<h1>Hello (:</h1>
<p>by downloading my library, you will have several tools and functions that will do several form validations</p>
<p>
<a href="https://badge.fury.io/js/multiform-validator.svg" target="_blank">
<img src="https://badge.fury.io/js/multiform-validator.svg" alt="npm version">
</a>
<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT">
</a>
<a href="https://npm-stat.com/charts.html?package=multiform-validator" target="_blank">
<img src="https://img.shields.io/npm/dm/multiform-validator.svg?style=flat-square" alt="npm downloads">
</a>
</p>
<p>follow the <a href="./srcPage/subPages/documentation/index.html">documentation</a> for more information</p>
<div>
<a href="https://github.com/gabriel-logan/multiform-validator" target="_blank" rel="noopener noreferrer">Go to github page</a>
Expand Down Expand Up @@ -132,4 +132,4 @@ <h1>Hello (:</h1>
<script src="./srcPage/js/header/index.js"></script>
</body>

</html>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiform-validator",
"version": "1.0.18",
"version": "1.0.19",
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
"main": "index.js",
"scripts": {
Expand Down
54 changes: 52 additions & 2 deletions src/getOnlyEmail.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,71 @@
const CleanAfterDefaultDomain = ['.br', '.io', '.pt', '.us', '.org', '.com', ];
/**
* @param {string} text
* @param {boolean} [multiple] optional
* @param {boolean|string[]} [cleanDomain=false]
* @param {boolean} [repeatEmail=false]
* @example getOnlyEmail("Entre em contato com a equipe:</br> joao@empresa.com, maria@empresa.com, contato@empresa.com", true);
* // Output: ["joao@empresa.com", "maria@empresa.com", "contato@empresa.com"]
*
* @example getOnlyEmail("Vaga na asdlaod </br> Mande seu email para fiawn@rdwah.com</br>Sim aqui mesmo");
* // Output: "fiawn@rdwah.com"
*
* @example getOnlyEmail("Vaga na asdlaod </br> Mande seu email para fiawn@rdwah.comSim aqui asdasd@gmail.commesmo", true, true);
* // Output: [ 'fiawn@rdwah.com', 'asdasd@gmail.com' ]
*
* @description This function extracts emails from a given text. If multiple is set to true,
* it returns an array with all emails found. Otherwise, it returns only the first email found as a string.
*
* @description Default domainsList is ['.br', '.io', '.pt', '.us', '.org', '.com', ]; you can set true to use this or pass your own list,
* OBS: Your list gonna replace the default
* IMPORTANT: List order matters
*
* The third parameter solves the following, if the email is formatted as follows, foo@bar.comAAAA, it will clean the email returning only foo@bar.com, all characters that come after the one in the list will be removed and the email comes out clean
*
* if you need to skip some param use null
*
* @description the fourth parameter, if true, will allow the list to return repeated emails, otherwise the default does not return repeated emails
*
* @returns {string | string[]} An email string if multiple is false, or an array of email strings if multiple is true.
*/
function getOnlyEmail(text, multiple = false) {
function getOnlyEmail(text, multiple = false, cleanDomain = false, repeatEmail = false) {
const emailPattern = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
const matches = text.match(emailPattern);

if (!matches) return 'No email found';

if (cleanDomain) {
let domainsToClean;
if (Array.isArray(cleanDomain)) {
domainsToClean = cleanDomain;
} else {
domainsToClean = CleanAfterDefaultDomain;
}

const cleanedEmails = matches.map((email) => {
for (const domain of domainsToClean) {
const index = email.lastIndexOf(domain);
if (index !== -1) {
return email.substring(0, index + domain.length);
}
}
return email;
});

if (!repeatEmail) {
const uniqueEmails = [...new Set(cleanedEmails)];
return multiple ? uniqueEmails : uniqueEmails[0];
}

return multiple ? cleanedEmails : cleanedEmails[0];
}

if (!repeatEmail) {
const uniqueEmails = [...new Set(matches)];
return multiple ? uniqueEmails : uniqueEmails[0];
}

return multiple ? matches : matches[0];
}
module.exports = getOnlyEmail;

module.exports = getOnlyEmail;
19 changes: 17 additions & 2 deletions srcPage/subPages/functions/getOnlyEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ <h2 class="subtitle">Parameters</h2>
<li><code>text</code> (string) - The input text from which emails will be extracted.</li>
<li><code>multiple</code> (optional boolean) - If set to <code>true</code>, the function will return an array with all the email addresses found. If set to <code>false</code> or not provided, it will return only the first email address
found as a string.</li>
<li><code>cleanDomain</code> (optional boolean | string[]) - Either a boolean value indicating whether to clean after default domains or an array of custom domains to clean after. Default is <code>false</code> which means it won't
clean the email addresses after default domains ['.br', '.io', '.pt', '.us', '.org', '.com', ];. If set to <code>true</code>, it will clean after default domains. If set to an array of strings, it will clean after the specified
custom domains.</li>
<li><code>repeatEmail</code> (optional boolean) - If set to <code>false</code>, the function will remove duplicate emails from the output list. If set to <code>true</code> or not provided, it will keep the duplicate emails in the output
list.
</li>
</ul>

<h2 class="subtitle">Examples</h2>
Expand All @@ -160,7 +166,16 @@ <h2 class="subtitle">Examples</h2>
// Example 2 - Extracting the first email from the text
const result2 = getOnlyEmail("Vaga na asdlaod </br> Mande seu email para fiawn@rdwah.com</br>Sim aqui mesmo");
console.log(result2);
// Output: "fiawn@rdwah.com"</pre>
// Output: "fiawn@rdwah.com"

const result3 = getOnlyEmail("Vaga na asdlaod </br> Mande seu email para fiawn@rdwah.comSim aqui asdasd@gmail.commesmo", true, true);
console.log(result3);
// Output: [ 'fiawn@rdwah.com', 'asdasd@gmail.com' ]

const result4 = getOnlyEmail("Vaga na asdlaod </br> Mande seu email para fiawn@rdwah.comSim aqui asdasd@gmail.commesmo asdasd asd as fiawn@rdwah.com", true, true, true);
console.log(result4);
// Output: [ 'fiawn@rdwah.com', 'asdasd@gmail.com', 'fiawn@rdwah.com' ]
</pre>

<h2 class="subtitle">Notes</h2>
<p>
Expand Down Expand Up @@ -188,4 +203,4 @@ <h2 class="subtitle">Notes</h2>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script>
</body>

</html>
</html>

0 comments on commit 56ce446

Please sign in to comment.