From 5477199b6cc4164e5f9d8e2ced07918dc18b7acf Mon Sep 17 00:00:00 2001 From: Omar Aguinaga Date: Wed, 10 Oct 2018 16:57:07 +0200 Subject: [PATCH] Add is email --- package-lock.json | 2 +- src/is/types.js | 11 +++++++++++ test/is/types.test.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index f041fa1..81f3b4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "checkif.js", - "version": "0.0.1", + "version": "0.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/is/types.js b/src/is/types.js index 8d117e2..44bc566 100644 --- a/src/is/types.js +++ b/src/is/types.js @@ -125,6 +125,16 @@ export function isWindowObject(value) { return value != null && typeof value === 'object' && 'setInterval' in value; } +/** + * Returns whether a given value is an email + * @param {*} value the value to check + */ +export function isEmail(value) { + /* eslint-disable-next-line no-useless-escape */ + const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(String(value).toLowerCase()); +} + export default { object: isObject, array: isArray, @@ -142,4 +152,5 @@ export default { regexp: isRegexp, undefined: isUndefined, windowObject: isWindowObject, + email: isEmail, }; diff --git a/test/is/types.test.js b/test/is/types.test.js index 4ad9100..d5b0bc2 100644 --- a/test/is/types.test.js +++ b/test/is/types.test.js @@ -197,3 +197,15 @@ describe('isWindowObject', () => { }); testFalsyWithNullable(is.windowObject); }); + +describe('isEmail', () => { + test('returns true', () => { + expect(is.email('very.common@example.com')).toBeTruthy(); + expect(is.email('other.email-with-hyphen@example.com')).toBeTruthy(); + expect(is.email('user.name+tag+sorting@example.com')).toBeTruthy(); + }); + test('returns false', () => { + expect(is.email('john..doe@example.com')).toBeFalsy(); + }); + testFalsyWithNullable(is.email); +});