Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

WIP: Cannot get coverage quite there #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/ageCalculator.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/array-average.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ function average(array) {
return total + current
})
return sum / array.length
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -114,4 +115,5 @@ export {
isPrime,
swapElements,
reverse,
ageCalculator
}
23 changes: 23 additions & 0 deletions test/ageCalculator.test.js
Original file line number Diff line number Diff line change
@@ -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)
})