-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Email utilities. | ||
*/ | ||
|
||
import '#@initialize.ts'; | ||
|
||
import { $str } from '#index.ts'; | ||
|
||
/** | ||
* Gets email from an addr. | ||
* | ||
* - `username@hostname`. | ||
* - `"Name" <username@hostname>`. | ||
* | ||
* @param str String to consider. | ||
* | ||
* @returns Email from an addr; else empty string. | ||
*/ | ||
export const fromAddr = (str: string): string => { | ||
if (!str) return ''; | ||
if ($str.isEmail(str)) return str; | ||
|
||
const parts = str.split(/(?<=")\s(?=<)/u); | ||
if ( | ||
2 === parts.length && | ||
// | ||
parts[0].length >= 3 && // e.g., `"x"`. | ||
'"' === parts[0][0] && // Opening quote. | ||
'"' === parts[0][parts[0].length - 1] && // Closing quote. | ||
parts[0].length <= 255 + 2 && // 2 = quotes; i.e., `"..."`. | ||
// | ||
parts[1].length >= 3 && // e.g., `<x>`. | ||
'<' === parts[1][0] && // Opening bracket. | ||
'>' === parts[1][parts[1].length - 1] && // Closing bracket. | ||
$str.isEmail(parts[1].slice(1, -1)) // `<email>` validation. | ||
) { | ||
return parts[1].slice(1, -1).toLowerCase(); | ||
} | ||
return ''; // Not an addr. | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Test suite. | ||
*/ | ||
|
||
import { $email } from '#index.ts'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
describe('$email', async () => { | ||
test('.fromAddr()', async () => { | ||
expect($email.fromAddr('x@x')).toBe('x@x'); | ||
expect($email.fromAddr('x+x@x')).toBe('x+x@x'); | ||
|
||
expect($email.fromAddr('x@localhost')).toBe('x@localhost'); | ||
expect($email.fromAddr('x+x@localhost')).toBe('x+x@localhost'); | ||
|
||
expect($email.fromAddr('[email protected]')).toBe('[email protected]'); | ||
expect($email.fromAddr('[email protected]')).toBe('[email protected]'); | ||
|
||
expect($email.fromAddr('"X" <x@x>')).toBe('x@x'); | ||
expect($email.fromAddr('"X" <x+x@x>')).toBe('x+x@x'); | ||
|
||
expect($email.fromAddr('"X" <x@localhost>')).toBe('x@localhost'); | ||
expect($email.fromAddr('"X" <x+x@localhost>')).toBe('x+x@localhost'); | ||
|
||
expect($email.fromAddr('"X" <[email protected]>')).toBe('[email protected]'); | ||
expect($email.fromAddr('"X" <[email protected]>')).toBe('[email protected]'); | ||
|
||
expect($email.fromAddr('"X" <x@x>')).toBe('x@x'); | ||
expect($email.fromAddr('"X X" <x@x>')).toBe('x@x'); | ||
|
||
expect($email.fromAddr('x@x,x')).toBe(''); | ||
expect($email.fromAddr('x,x@x')).toBe(''); | ||
expect($email.fromAddr('<[email protected]>')).toBe(''); | ||
expect($email.fromAddr('x @hop.gdn')).toBe(''); | ||
|
||
expect($email.fromAddr('X x@x')).toBe(''); | ||
expect($email.fromAddr('"X" x@x')).toBe(''); | ||
expect($email.fromAddr('"" <x@x>')).toBe(''); | ||
expect($email.fromAddr('"X" <x@x>')).toBe(''); | ||
expect($email.fromAddr('"X" <x@>')).toBe(''); | ||
expect($email.fromAddr('"X" <@x>')).toBe(''); | ||
}); | ||
}); |