Skip to content

Commit

Permalink
Project update. [p][robotic]
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswrks committed Dec 29, 2024
1 parent f892274 commit 2d8e52e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .env.vault
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ DOTENV_VAULT_MAIN="7GW98NfL4hM4N6HbfeDXCKreVxKvcVPupFRnZ8XJ+pomaQ=="
DOTENV_VAULT_MAIN_VERSION=1

# dev
DOTENV_VAULT_DEV="SxOgzzm+LPdZ0f+Whjf9+cK90exZYPcYGm5fxxadBmIgLNn7e8/n1iyhbbD8hb2j5IjzEOvISIJjL896CcdJnISWiGm4HZpLVXZhSy8Q5c4O63TP47Hi64bjYJ4GhuA7CSl3K5LheGPv4PRlEJMzFw1s4RJxMQNP22Yb8UxSODoZULAqiB8HfAchhlLe3goPHoHigtJHZINx/cncLhy3+Qs6nL3FC5eR+ToRYX8gj7O97geSc8WYb5AGMBW2KyO8NQ=="
DOTENV_VAULT_DEV_VERSION=559
DOTENV_VAULT_DEV="TwxDnS+ApGYMAvvlyy1XL2ZHSF81VkHMf8Yj6YCMH4B3Qpg7jEOPFdVZt/I//Z4cRvmDYrr3+CZp15fx39KmzieZ/hgnf52hL3moq56IMuJQXlRej/pZFo4eV18akkxpltdVVa6qNb/qN8n5uH3dGrEwHZq8CC+zDDc4aMAEqrnqjRKOM04Y4NOCpuE5I+mqpkz+PuhmmknxgIU70eTa29La9LvaRPMTuAW6QXG5lH8izAGHHI0yylR3TtBzcZGz6A=="
DOTENV_VAULT_DEV_VERSION=561

# ci
DOTENV_VAULT_CI="oUO9zm2WJcH5NJR5u//O13wjujSyz9gYzQYOXKSO+HXmDs7v5OBiZVQBCWTPbnBHtLpYqVlcQCvgPCNoCBgNJabqkOBiyzwmFyy1TK5O3VxlnA3so/9nSr1pmMvyuyIzL3XcvRlyQzHyCpF2eCfzRlTenRWi8cPfwWKoMIWTpg=="
DOTENV_VAULT_CI_VERSION=559
DOTENV_VAULT_CI="m73XqNmab2TEtFaq24HgWnbf01VOBtGEgpQLY8GQLzvjhDU+6UctelFDZ2OncOj33NKYJkgtVBx3frHi37rDiTjnmwDu/FD12PFxaRxG+AcJriAXDCHtlYJ5eTUtMgP8EdmRDe1NaTDYQiycRIaSMlGry7bJB5ZQyuVHvkTC7g=="
DOTENV_VAULT_CI_VERSION=561

# stage
DOTENV_VAULT_STAGE="aRP8su2YV4jZu3w1HZ/SLaots0IwJDFw75TCpvXEFeNp7tw="
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "1.0.966",
"version": "1.0.967",
"license": "GPL-3.0-or-later",
"name": "@clevercanyon/utilities",
"description": "Utilities for JavaScript apps running in any environment.",
Expand Down Expand Up @@ -75,6 +75,11 @@
"import": "./dist/dom.js",
"default": "./dist/dom.js"
},
"./email": {
"types": "./dist/types/email.d.ts",
"import": "./dist/email.js",
"default": "./dist/email.js"
},
"./env": {
"types": "./dist/types/env.d.ts",
"import": "./dist/env.js",
Expand Down
40 changes: 40 additions & 0 deletions src/email.ts
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.
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * as $color from '#color.ts';
export * as $cookie from '#cookie.ts';
export * as $crypto from '#crypto.ts';
export * as $dom from '#dom.ts';
export * as $email from '#email.ts';
export * as $env from '#env.ts';
export * as $error from '#error.ts';
export * as $fn from '#fn.ts';
Expand Down
20 changes: 2 additions & 18 deletions src/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import '#@initialize.ts';

import { $dom, $env, $is, $obj, $to } from '#index.ts';
import { $dom, $email, $env, $is, $obj, $to } from '#index.ts';
import ipRegex from 'ip-regex';

const ipV4MaxLength = 15,
Expand Down Expand Up @@ -999,23 +999,7 @@ export const isEmail = (str: string): boolean => {
* @returns True if string is an addr.
*/
export const isAddr = (str: string): boolean => {
if (!str) return false;
if (isEmail(str)) return true;

const parts = str.split(/(?<=")\s(?=<)/u);
return (
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.
isEmail(parts[1].slice(1, -1)) // `<email>` validation.
);
return $email.fromAddr(str) ? true : false;
};

/* ---
Expand Down
43 changes: 43 additions & 0 deletions src/tests/email/index.ts
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('');
});
});

0 comments on commit 2d8e52e

Please sign in to comment.