@jrc03c/email-validator
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -66,9 +66,39 @@ (() => { | ||
var uglyFetch = require_ugly_fetch(); | ||
function flatten(x) { | ||
let out = []; | ||
x.forEach((item) => { | ||
if (isArray(item)) { | ||
const children = flatten(item); | ||
out = out.concat(children); | ||
} else { | ||
out.push(item); | ||
} | ||
}); | ||
return out; | ||
} | ||
function isArray(x) { | ||
return x instanceof Array; | ||
} | ||
function isString(s) { | ||
return typeof s === "string"; | ||
} | ||
var EmailValidator = class { | ||
constructor() { | ||
this.topLevelDomainList = []; | ||
} | ||
_topLevelDomainList = []; | ||
get isReady() { | ||
return this.topLevelDomainList.length > 0; | ||
} | ||
get topLevelDomainList() { | ||
return this._topLevelDomainList; | ||
} | ||
set topLevelDomainList(value) { | ||
if (!isArray(value)) { | ||
throw new Error( | ||
"The new value for `topLevelDomainList` must be an array!" | ||
); | ||
} | ||
this._topLevelDomainList = flatten(value).filter((v) => isString(v)); | ||
} | ||
async load(url) { | ||
return await this.fetchTopLevelDomainList(url); | ||
} | ||
async fetchTopLevelDomainList(url) { | ||
@@ -93,3 +123,3 @@ if (!url) { | ||
} | ||
if (typeof email !== "string") { | ||
if (!isString(email)) { | ||
return false; | ||
@@ -96,0 +126,0 @@ } |
{ | ||
"name": "@jrc03c/email-validator", | ||
"author": "jrc03c", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "validates email addresses", | ||
@@ -6,0 +6,0 @@ "license": "ISC", |
@@ -1,2 +0,2 @@ | ||
# Install | ||
# Installation | ||
@@ -7,3 +7,3 @@ ```bash | ||
# Use | ||
# Usage | ||
@@ -13,3 +13,3 @@ In Node: | ||
```js | ||
const emailValidator = require("@jrc03c/email-validator") | ||
const EmailValidator = require("@jrc03c/email-validator") | ||
``` | ||
@@ -20,3 +20,3 @@ | ||
```html | ||
<script src="path/to/email-validator.js"></script> | ||
<script src="path/to/dist/email-validator.js"></script> | ||
``` | ||
@@ -27,5 +27,7 @@ | ||
```js | ||
emailValidator.fetchTopLevelDomainList().then(() => { | ||
const isValid = emailValidator.validate("someone@example.com") | ||
const validator = new EmailValidator() | ||
validator.load().then(() => { | ||
const isValid = validator.validate("someone@example.com") | ||
if (isValid) { | ||
@@ -39,6 +41,6 @@ // Yay! | ||
By default, the top-level domain list is fetched from [here](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). However, if you prefer, you can pass your own URL from which to fetch the list: | ||
By default, the top-level domain list is fetched from [IANA](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). However, if you prefer, you can pass your own URL from which to fetch the list: | ||
```js | ||
emailValidator.fetchTopLevelDomainList(myListUrl) | ||
validator.load(myListUrl) | ||
``` | ||
@@ -49,12 +51,49 @@ | ||
```js | ||
const emailValidator = require("@jrc03c/email-validator") | ||
const EmailValidator = require("@jrc03c/email-validator") | ||
const fetch = require("node-fetch") | ||
fetch(myListUrl, myFancyOptions).then(response => { | ||
const myFancyList = await response.json() | ||
// parse the response to retrieve your list, and then: | ||
emailValidator.topLevelDomainList = myFancyList | ||
const validator = new EmailValidator() | ||
validator.topLevelDomainList = myFancyList | ||
const isValid = emailValidator.validate("someone@example.com") | ||
const isValid = validator.validate("someone@example.com") | ||
// ... | ||
}) | ||
``` | ||
# API | ||
## EmailValidator | ||
There are no constructor arguments. | ||
### Properties | ||
#### `isReady` | ||
A read-only property that indicates whether or not a top-level domain list has been loaded into the validator. | ||
#### `topLevelDomainList` | ||
A list of top-level domains. (NOTE: I typically think of domains as something like "github.com". But "top-level domains" in this context refers to what I'd otherwise call the "extension" of the domain; i.e., the thing that comes at the end of the domain name, such as "com" in "github.com". So, this top-level domain list should be a list of all such domain name endings.) | ||
### Methods | ||
#### `load(url)` | ||
Returns a `Promise` that resolves once a list of top-level domains has been downloaded. Passing a URL into the method is optional. When passed, the top-level domain list will be fetched from the given URL rather than from [the default IANA URL](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). This method is identical to `fetchTopLevelDomainList`. | ||
#### `fetchTopLevelDomainList(url)` | ||
Returns a `Promise` that resolves once a list of top-level domains has been downloaded. Passing a URL into the method is optional. When passed, the top-level domain list will be fetched from the given URL rather than from [the default IANA URL](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). This method is identical to `load`. | ||
#### `isValid(email)` | ||
Returns a boolean indicating whether or not the given email address is valid. This method is identical to `validate`. | ||
#### `validate(email)` | ||
Returns a boolean indicating whether or not the given email address is valid. This method is identical to `isValid`. |
const uglyFetch = require("./ugly-fetch") | ||
function flatten(x) { | ||
let out = [] | ||
x.forEach(item => { | ||
if (isArray(item)) { | ||
const children = flatten(item) | ||
out = out.concat(children) | ||
} else { | ||
out.push(item) | ||
} | ||
}) | ||
return out | ||
} | ||
function isArray(x) { | ||
return x instanceof Array | ||
} | ||
function isString(s) { | ||
return typeof s === "string" | ||
} | ||
class EmailValidator { | ||
constructor() { | ||
this.topLevelDomainList = [] | ||
} | ||
_topLevelDomainList = [] | ||
@@ -12,2 +33,20 @@ get isReady() { | ||
get topLevelDomainList() { | ||
return this._topLevelDomainList | ||
} | ||
set topLevelDomainList(value) { | ||
if (!isArray(value)) { | ||
throw new Error( | ||
"The new value for `topLevelDomainList` must be an array!" | ||
) | ||
} | ||
this._topLevelDomainList = flatten(value).filter(v => isString(v)) | ||
} | ||
async load(url) { | ||
return await this.fetchTopLevelDomainList(url) | ||
} | ||
async fetchTopLevelDomainList(url) { | ||
@@ -40,3 +79,3 @@ if (!url) { | ||
if (typeof email !== "string") { | ||
if (!isString(email)) { | ||
return false | ||
@@ -43,0 +82,0 @@ } |
@@ -6,3 +6,5 @@ const EmailValidator = require(".") | ||
expect(validator.isReady).toBe(false) | ||
await validator.fetchTopLevelDomainList() | ||
validator.isReady = true | ||
expect(validator.isReady).toBe(false) | ||
await validator.load() | ||
@@ -34,5 +36,2 @@ const items = [ | ||
const selfReferencer = [2, 3, 4] | ||
selfReferencer.push(selfReferencer) | ||
const wrongs = [ | ||
@@ -62,3 +61,2 @@ 0, | ||
{ hello: "world" }, | ||
selfReferencer, | ||
] | ||
@@ -68,3 +66,11 @@ | ||
expect(validator.isValid(value)).toBe(false) | ||
const temp = new EmailValidator() | ||
if (value instanceof Array) { | ||
temp.topLevelDomainList = value | ||
expect(temp.isReady).toBe(false) | ||
} else { | ||
expect(() => (temp.topLevelDomainList = value)).toThrow() | ||
} | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22954
414
94
6