diff --git a/src/ageCalculator.js b/src/ageCalculator.js new file mode 100644 index 00000000..3796db16 --- /dev/null +++ b/src/ageCalculator.js @@ -0,0 +1,65 @@ +export default ageCalculator + +/** +* Original Source: https://stackoverflow.com/a/7091965 +* +* This method will calculate your today's age from +* your date of birth +* +* @param {String} dateString - The birthdate String in the form (dd/mm/yyyy) +* @return {String} - today's age String +*/ + +//is valid date format +function ageCalculator (dateString) { + if (!isDate(dateString)) + return "Not a valid date"; + var birthDate = parseDate(dateString); + var today = new Date(); + var age = today.getFullYear() - birthDate.getFullYear(); + var age_months = today.getMonth() - birthDate.getMonth(); + var age_days = today.getDate() - birthDate.getDate(); + if (age_months < 0 || (age_months == 0 && age_days < 0)) { + age = parseInt(age) - 1; + } + return age; +} + + +//convert the date string in the format of dd/mm/yyyy into a JS date object +function parseDate(dateString) { + var dateParts = dateString.split("/"); + return new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]); +} + +function isDate(txtDate) { + var currVal = txtDate; + if (currVal == '') + return true; + + //Declare Regex + var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; + var dtArray = currVal.match(rxDatePattern); // is format OK? + + if (dtArray == null) + return false; + + //Checks for dd/mm/yyyy format. + var dtDay = dtArray[1]; + var dtMonth = dtArray[3]; + var dtYear = dtArray[5]; + + if (dtMonth < 1 || dtMonth > 12) + return false; + else if (dtDay < 1 || dtDay > 31) + return false; + else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31) + return false; + else if (dtMonth == 2) { + var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0)); + if (dtDay > 29 || (dtDay == 29 && !isleap)) + return false; + } + + return true; +} diff --git a/src/array-average.js b/src/array-average.js index d32a2f0d..e10b530f 100644 --- a/src/array-average.js +++ b/src/array-average.js @@ -12,4 +12,4 @@ function average(array) { return total + current }) return sum / array.length -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 2fa84767..9e8f65e1 100644 --- a/src/index.js +++ b/src/index.js @@ -55,6 +55,7 @@ import timeDifference from './timeDifference' import isPrime from './is-prime' import swapElements from './swapElements' import reverse from './reverse' +import ageCalculator from './ageCalculator' export { isOdd, @@ -114,4 +115,5 @@ export { isPrime, swapElements, reverse, + ageCalculator } diff --git a/test/ageCalculator.test.js b/test/ageCalculator.test.js new file mode 100644 index 00000000..f0ec71dc --- /dev/null +++ b/test/ageCalculator.test.js @@ -0,0 +1,23 @@ +import test from 'ava' +import {ageCalculator} from '../src' + +test('Check for correct results', t => { + const dateString = "08/04/1997" + const expected_age = 20 + const actual_age = age_calculator(dateString) + t.deepEqual(expected_age,actual_age) +}) + +test('Invalid date to check validation', t => { + const dateString = "08/15/1997" + const expected_age = 20 + const actual_age = age_calculator(dateString) + t.deepEqual(expected_age,actual_age) +}) + +test('Invalid date to check validation pattern', t => { + const dateString = "08/1/199" + const expected_age = 20 + const actual_age = age_calculator(dateString) + t.deepEqual(expected_age,actual_age) +}) \ No newline at end of file