Skip to content

Commit

Permalink
feat: require node >=14, added period to regex (fixes #2), re2 is now…
Browse files Browse the repository at this point in the history
… a peer dependency (breaking), added tests, bump deps
  • Loading branch information
titanism committed Jun 28, 2022
1 parent 0ef7567 commit d7a1eac
Show file tree
Hide file tree
Showing 21 changed files with 159 additions and 10,054 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional']
};
2 changes: 1 addition & 1 deletion .dist.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"presets": [
["@babel/env", {
"targets": {
"browsers": [ "> 1%", "last 2 versions" ]
"browsers": [ "defaults, not ie 11" ]
}
}]
]
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.*.js
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
4 changes: 0 additions & 4 deletions .github/FUNDING.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on:
- push
- pull_request
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
node_version:
- 14
- 16
- 18
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
27 changes: 9 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
# OS #
###################
.DS_Store
*.log
.idea
Thumbs.db
tmp/
temp/


# Node.js #
###################
node_modules


# NYC #
###################
coverage
*.lcov
.nyc_output
locales/
package-lock.json
yarn.lock


# Files #
###################
*.log
Thumbs.db
tmp/
temp/
*.lcov
.env
lib
dist
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged && npm test
4 changes: 2 additions & 2 deletions .lib.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"presets": [
["@babel/env", {
"targets": {
"node": "10.12.0",
"browsers": [ "> 1%", "last 2 versions" ]
"node": "14",
"browsers": [ "defaults, not ie 11" ]
}
}]
],
Expand Down
4 changes: 1 addition & 3 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
"*.md,!test/**/*.md": [
filenames => filenames.map(filename => `remark ${filename} -qfo`)
],
"*.md": filenames => filenames.map(filename => `remark ${filename} -qfo`),
'package.json': 'fixpack',
'*.js': 'xo --fix'
};
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
singleQuote: true,
bracketSpacing: true,
trailingComma: 'none'
};
3 changes: 3 additions & 0 deletions .remarkrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ['preset-github']
};
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .xo-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
prettier: true,
space: true,
extends: ['xo-lass']
};
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# email-regex-safe

[![build status](https://img.shields.io/travis/com/niftylettuce/email-regex-safe.svg)](https://travis-ci.com/niftylettuce/email-regex-safe)
[![code coverage](https://img.shields.io/codecov/c/github/niftylettuce/email-regex-safe.svg)](https://codecov.io/gh/niftylettuce/email-regex-safe)
[![build status](https://github.com/spamscanner/email-regex-safe/actions/workflows/ci.yml/badge.svg)](https://github.com/spamscanner/email-regex-safe/actions/workflows/ci.yml)
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)
Expand All @@ -27,7 +26,7 @@

## Foreword

Previously I was using [email-regex][] through my work on [Spam Scanner][spam-scanner] and [Forward Email][forward-email]. However this package has [too many issues](https://github.com/sindresorhus/email-regex/issues/9) and [false positives](https://github.com/sindresorhus/email-regex/issues/2).
Previously we were using [email-regex][] through our work on [Spam Scanner][spam-scanner] and [Forward Email][forward-email]. However this package has [too many issues](https://github.com/sindresorhus/email-regex/issues/9) and [false positives](https://github.com/sindresorhus/email-regex/issues/2).

This package should hopefully more closely resemble real-world intended usage of an email regular expression, and also let you configure several [Options](#options). Please check out [Forward Email][forward-email] if this package helped you, and explore our source code on GitHub which shows how we use this package.

Expand All @@ -36,25 +35,19 @@ This package should hopefully more closely resemble real-world intended usage of

## Install

[npm][]:

```sh
npm install email-regex-safe
```
**NOTE:** As of v2.0.0 you must also install `re2` as a peer dependency.

[yarn][]:
[npm][]:

```sh
yarn add email-regex-safe
npm install email-regex-safe re2
```


## Usage

### Node

This package automatically includes [RE2][] for regex optimization and vulnerability protection. You will not have to manually wrap your URL regular expressions with `new RE2(emailRegex())` anymore through `email-regex-safe` (we do it automatically for you).

```js
const emailRegexSafe = require('email-regex-safe');

Expand Down Expand Up @@ -138,8 +131,6 @@ Since we cannot use regular expression's "negative lookbehinds" functionality (d

[npm]: https://www.npmjs.com/

[yarn]: https://yarnpkg.com/

[re2]: https://github.com/uhop/node-re2

[browserify]: https://github.com/browserify/browserify
Expand Down
75 changes: 38 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@
"Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)"
],
"dependencies": {
"ip-regex": "^4.3.0",
"re2": "^1.15.9",
"tlds": "^1.217.0"
"ip-regex": "4",
"tlds": "^1.231.0"
},
"devDependencies": {
"@babel/cli": "^7.12.16",
"@babel/core": "^7.12.16",
"@babel/preset-env": "^7.12.16",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"ava": "latest",
"@babel/cli": "^7.18.6",
"@babel/core": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^17.0.3",
"ava": "^4.3.0",
"babelify": "^10.0.0",
"browserify": "^17.0.0",
"codecov": "latest",
"cross-env": "latest",
"eslint": "latest",
"eslint-config-xo-lass": "latest",
"eslint-plugin-compat": "^3.9.0",
"cross-env": "^7.0.3",
"eslint": "^8.18.0",
"eslint-config-xo-lass": "^2.0.1",
"eslint-plugin-compat": "^4.0.2",
"eslint-plugin-node": "^11.1.0",
"fixpack": "latest",
"husky": "latest",
"fixpack": "^4.0.0",
"husky": "^8.0.1",
"jsdom": "15",
"lint-staged": "latest",
"nyc": "latest",
"remark-cli": "latest",
"remark-preset-github": "latest",
"tinyify": "^3.0.0",
"xo": "latest"
"lint-staged": "^13.0.3",
"nyc": "^15.1.0",
"re2": "^1.17.7",
"remark-cli": "^11.0.0",
"remark-preset-github": "^4.0.4",
"rimraf": "^3.0.2",
"tinyify": "^3.1.0",
"xo": "^0.50.0"
},
"engines": {
"node": ">= 10.12.0"
"node": ">=14"
},
"files": [
"lib",
Expand Down Expand Up @@ -105,6 +105,14 @@
],
"license": "MIT",
"main": "lib/index.js",
"peerDependencies": {
"re2": "^1.17.2"
},
"peerDependenciesMeta": {
"re2": {
"optional": true
}
},
"repository": {
"type": "git",
"url": "https://github.com/niftylettuce/email-regex-safe"
Expand All @@ -116,24 +124,17 @@
"build:clean": "rimraf lib dist",
"build:dist": "npm run browserify && npm run minify",
"build:lib": "babel --config-file ./.lib.babelrc src --out-dir lib",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"lint": "yarn run lint:js && yarn run lint:md && yarn run lint:lib && yarn run lint:dist",
"lint": "npm run lint:js && npm run lint:md && npm run lint:pkg && npm run lint:lib && npm run lint:dist",
"lint:dist": "eslint --no-inline-config -c .dist.eslintrc dist",
"lint:js": "xo",
"lint:js": "xo --fix",
"lint:lib": "eslint -c .lib.eslintrc lib",
"lint:md": "remark . -qfo",
"lint:pkg": "fixpack",
"minify": "cross-env NODE_ENV=production browserify src/index.js -o dist/email-regex-safe.min.js -s emailRegexSafe -g [ babelify --configFile ./.dist.babelrc ] -p tinyify",
"nyc": "cross-env NODE_ENV=test nyc ava",
"pretest": "yarn run build && yarn run lint",
"test": "cross-env NODE_ENV=test ava",
"test-coverage": "cross-env NODE_ENV=test nyc yarn run test"
"prepare": "husky install",
"pretest": "npm run build && npm run lint",
"test": "npm run test-coverage"
},
"unpkg": "dist/email-regex-safe.min.js",
"xo": {
"prettier": true,
"space": true,
"extends": [
"xo-lass"
]
}
"unpkg": "dist/email-regex-safe.min.js"
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ module.exports = (options) => {
// note that we are parsing for emails, not enforcing username match, so we allow +
'[^\\W_](?:[\\w\\.\\+]+)' // NOTE: we don't end with `[^\\W]` here since Gmail doesn't do this in webmail
: options.utf8
? "[^\\W_](?:[a-z\\d!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]+)"
: "[^\\W_](?:[a-z\\d!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]+)";
? "[^\\W_](?:[a-z\\d!#\\$%&'\\.\\*\\+\\-\\/=\\?\\^_`{\\|}~\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]+)"
: "[^\\W_](?:[a-z\\d!#\\$%&'\\.\\*\\+\\-\\/=\\?\\^_`{\\|}~]+)";

let regex = `(?:${emailUserPart}@(?:`;
if (options.localhost) regex += 'localhost|';
Expand Down
50 changes: 48 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const test = require('ava');
const emailRegexSafe = require('..');

const string = `
bob.foo-bar@test.com
__boop@beep.com
foo@foo.com
foo@f.com
Expand Down Expand Up @@ -33,8 +34,11 @@ foo@fe.com admin@2606:4700:4700::1111
fe@fe az@as test@1.2.3.4 foo@com.jpeg
foo@com.jpeg`;

test('parses matches', (t) => {
t.deepEqual(string.match(emailRegexSafe()), [
test('gmail matches', (t) => {
const match = string.match(emailRegexSafe());
t.log(match);
t.deepEqual(match, [
'bar@test.com',
'boop@beep.com',
'foo@foo.com',
'foo@f.com',
Expand Down Expand Up @@ -71,3 +75,45 @@ test('parses matches', (t) => {
'foo@com.jp'
]);
});

test('non-gmail matches', (t) => {
const match = string.match(emailRegexSafe({ gmail: false }));
t.log(match);
t.deepEqual(match, [
'bob.foo-bar@test.com',
'boop@beep.com',
'foo@foo.com',
'foo@f.com',
'some@sub.domain.jpg.co.uk.com.jp',
'boop@beep.com',
'bepp.test@boop.com',
'beep....foo@foo.com',
'beep..@foo.com',
'beep@bar.com',
'beep.boop.boop.@foo.com',
'beep@boop.com',
'foobar@gmail.com',
'foo@gmail.com',
'foo@gmail.com',
'test.com+@testtest.com',
'subscribe.example.com/subscribe.aspx?foo=zaaaa@example.io',
'beep=foo124123@example.nl',
'beep@test.co.uk',
'images/some_logo@2x.jp',
'images/foobar@2x.jp',
'beep.boop.net/foo.cfm?email=beep@example.ai',
'image001.png@01bazz23.mx',
'image002.png@03j570cf.ee',
'image000.png@03j570cfzaaaazz.ee',
'image005.png@03j570cf.es',
'example.@gmail.com',
'foo+test@gmail.com',
'text@example.com',
'foo@bar.com',
'baz@boop.com',
'foo@fe.com',
'test@1.2.3.4',
'foo@com.jp',
'foo@com.jp'
]);
});
Loading

0 comments on commit d7a1eac

Please sign in to comment.