oneday-core
Advanced tools
Comparing version 1.0.53 to 1.0.54
@@ -40,2 +40,3 @@ 'use strict'; | ||
module.exports.isValidEmail = util.isValidEmail; | ||
module.exports.isValidPhoneNumber = util.isValidPhoneNumber; | ||
module.exports.addIfPresent = util.addIfPresent; | ||
@@ -42,0 +43,0 @@ module.exports.aggregateByProperty = util.aggregateByProperty; |
{ | ||
"name": "oneday-core", | ||
"version": "1.0.53", | ||
"version": "1.0.54", | ||
"description": "Basic AWS util functions to ease development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
41
test.js
@@ -35,2 +35,43 @@ var assert = require('chai').assert; | ||
describe('isValidEmail', function () { | ||
it('should return true when given param is a valid email', function () { | ||
assert.isTrue(index.isValidEmail('abc@abc.com')); | ||
assert.isTrue(index.isValidEmail('123@123.com')); | ||
assert.isTrue(index.isValidEmail('a1c@a1c.com')); | ||
assert.isTrue(index.isValidEmail('a-c@a-c.com')); | ||
assert.isTrue(index.isValidEmail('a.c@abc.com')); | ||
assert.isTrue(index.isValidEmail('a_c@a_c.com')); | ||
assert.isTrue(index.isValidEmail('a+c@abc.com')); | ||
assert.isTrue(index.isValidEmail('abc@a.bc.com')); | ||
}); | ||
it('should return false when given param is not a valid email', function () { | ||
assert.isFalse(index.isValidEmail(null)); | ||
assert.isFalse(index.isValidEmail(undefined)); | ||
assert.isFalse(index.isValidEmail('foo')); | ||
assert.isFalse(index.isValidEmail('123')); | ||
assert.isFalse(index.isValidEmail('@abc.com')); | ||
assert.isFalse(index.isValidEmail('abc@.com')); | ||
assert.isFalse(index.isValidEmail('@.com')); | ||
assert.isFalse(index.isValidEmail('a@b.')); | ||
assert.isFalse(index.isValidEmail('a@b.1')); | ||
}); | ||
}); | ||
describe('isValidPhoneNumber', function () { | ||
it('should return true when given param is a valid email', function () { | ||
assert.isTrue(index.isValidPhoneNumber('1234567890')); | ||
assert.isTrue(index.isValidPhoneNumber('+1 234 567 8901')); | ||
assert.isTrue(index.isValidPhoneNumber('+123 4567890123')); | ||
assert.isTrue(index.isValidPhoneNumber('+123 (456)789-0123')); | ||
}); | ||
it('should return false when given param is not a valid email', function () { | ||
assert.isFalse(index.isValidPhoneNumber(null)); | ||
assert.isFalse(index.isValidPhoneNumber(undefined)); | ||
assert.isFalse(index.isValidPhoneNumber('foo')); | ||
assert.isFalse(index.isValidPhoneNumber('123')); | ||
assert.isFalse(index.isValidPhoneNumber('12345678901234')); | ||
assert.isFalse(index.isValidPhoneNumber('1234a567890')); | ||
}); | ||
}); | ||
describe('get', function () { | ||
@@ -37,0 +78,0 @@ const obj = { |
18
util.js
@@ -12,2 +12,3 @@ module.exports.getDynamoDbArn = getDynamoDbArn; | ||
module.exports.isValidEmail = isValidEmail; | ||
module.exports.isValidPhoneNumber = isValidPhoneNumber; | ||
module.exports.addIfPresent = addIfPresent; | ||
@@ -150,6 +151,19 @@ module.exports.aggregateByProperty = aggregateByProperty; | ||
function isValidEmail(email) { | ||
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(email).toLowerCase()); | ||
const re1 = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; // Email Address 1 | ||
const p = new RegExp(re1, ["i"]); | ||
const m = p.exec(email); | ||
return m != null; | ||
} | ||
function isValidPhoneNumber(phone) { | ||
if(phone === null || phone === undefined || phone.match(/[a-z]/i)){ | ||
return false; | ||
} | ||
phone = phone.toString().replace(/\D/g, '').trim(); | ||
if (phone.length < 10 || phone.length > 13) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
function aggregateByProperty(obj, prop) { | ||
@@ -156,0 +170,0 @@ let result = {}; |
33845
874