Skip to content

Commit 59bd7c8

Browse files
author
Joe Crick
committed
Refactor formatTimeSlot to addLeadingZeroIfLessThanOne. Add time convenience methods to Day. Refactor
Update docs
1 parent b2731f4 commit 59bd7c8

File tree

10 files changed

+263
-97
lines changed

10 files changed

+263
-97
lines changed

docs/index.html

Lines changed: 188 additions & 63 deletions
Large diffs are not rendered by default.

src/day.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import format from './format';
1+
import {addLeadingZeroIfLessThanTen, formatDate as format} from './format';
22
import {getJsDate} from './date';
33
import noOp from './no-op';
44
import getRangeOfDates from './date-utils/get_range_of_dates';
55
import addDays from './date-utils/adjust_days';
66
import getWeekNumber from './date-utils/get_week_number';
7+
import {getTwelveHourTime} from './hours';
78

89
/**
910
* @description CONSTRUCTOR: Returns a day object, which is a JS Date, a formatted string version of the date, and some convenience
@@ -26,6 +27,22 @@ export function getDay({date, getEvents = noOp, formatDate = format, toISOString
2627
get formattedDate() {
2728
return formatDate(this.date);
2829
},
30+
/**
31+
* @return {string}
32+
*/
33+
get twentyFourHourTime() {
34+
return this.date.toTimeString().split(' ')[0];
35+
},
36+
/**
37+
* @return {string}
38+
*/
39+
get twelveHourTime() {
40+
const date = this.date;
41+
const hours = addLeadingZeroIfLessThanTen(getTwelveHourTime(date.getHours()));
42+
const minutes = addLeadingZeroIfLessThanTen(date.getMinutes() );
43+
const seconds = addLeadingZeroIfLessThanTen(date.getSeconds());
44+
return `${hours}:${minutes}:${seconds}`;
45+
},
2946
/**
3047
* @desc Convenience method
3148
* @return {number}

src/event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import format from './format';
1+
import {formatDate} from './format';
22

33
/**
44
* @description Given a data set where events can be obtained as date properties off an object, return the set of events
@@ -13,6 +13,6 @@ import format from './format';
1313
*/
1414
export default function makeEventFinder(data) {
1515
return function getEvents(date) {
16-
return data[format(date)] || [];
16+
return data[formatDate(date)] || [];
1717
};
1818
}

src/format.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
/**
2+
* @desc Formats a number with a leading zero if it is below 10
3+
* @param number
4+
* @return {string}
5+
*/
6+
export function addLeadingZeroIfLessThanTen (number) {
7+
return number < 10 ? `0${number}` : number;
8+
}
9+
110
/**
211
* @description Formats a date in International format
312
* @export
413
* @param {Date} date A JS Date object.
514
* @returns {string} A formatted date.
615
*/
7-
export default function formatDate(date) {
8-
// TODO: Prettify
9-
const _date = date.getDate();
10-
const day = _date <= 9 ? `0${_date}` : _date;
11-
let month = date.getMonth() + 1;
12-
month = month <= 9 ? `0${month}` : month;
16+
export function formatDate(date) {
17+
const day = addLeadingZeroIfLessThanTen(date.getDate());
18+
const month = addLeadingZeroIfLessThanTen(date.getMonth() + 1);
1319
return `${day}/${month}/${date.getFullYear()}`;
1420
}

src/hours.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import {addLeadingZeroIfLessThanTen} from './format';
2+
13
/**
24
* @desc Returns 24 time slots, from 12:00 AM - 12:00 PM
35
* @returns {[string]} An array of time slots corresponding to 12-hour time.
46
*/
57
export function getTwelveHourTimeSlots() {
68
const timeSlots = ['12:00 AM'];
79
for(let slot = 1; slot < 24; slot++) {
8-
timeSlots.push(`${formatTimeSlot(getTwelveHourTime(slot))}:00 ${getTimePeriod(slot)}`);
10+
timeSlots.push(`${addLeadingZeroIfLessThanTen(getTwelveHourTime(slot))}:00 ${getTimePeriod(slot)}`);
911
}
1012
return timeSlots;
1113
}
@@ -17,7 +19,7 @@ export function getTwelveHourTimeSlots() {
1719
export function getTwentyFourHourTimeSlots() {
1820
const timeSlots = [];
1921
for(let slot=0; slot < 24; slot++) {
20-
timeSlots.push(`${formatTimeSlot(slot)}:00`);
22+
timeSlots.push(`${addLeadingZeroIfLessThanTen(slot)}:00`);
2123
}
2224
return timeSlots;
2325
}
@@ -41,15 +43,6 @@ export function getTimePeriod(slot) {
4143
return slot < 12 && slot > 0 ? 'AM' : 'PM';
4244
}
4345

44-
/**
45-
* @desc Formats a number with a leading zero, if it is below 10
46-
* @param {number} slot A number
47-
* @returns {string} If the number is below 10, it will be padded with a leading 0.
48-
*/
49-
export function formatTimeSlot(slot) {
50-
return slot < 10 ? `0${slot}` : slot;
51-
}
52-
5346
/**
5447
* @desc Converts 24 hour time to 12 hour time.
5548
* @param {string} time A valid 24-hour time value 'hh:mm'.
@@ -59,5 +52,5 @@ export function formatTimeSlot(slot) {
5952
export function twentyFourToTwelveHourTime(time) {
6053
const slot = time.substring(0,2) >> 0;
6154
const timePeriod = getTimePeriod(slot);
62-
return `${formatTimeSlot(getTwelveHourTime(slot))}:00 ${timePeriod}`;
55+
return `${addLeadingZeroIfLessThanTen(getTwelveHourTime(slot))}:00 ${timePeriod}`;
6356
}

test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import './test/day.test';
22
import './test/week.test';
33
import './test/month.test';
44
import './test/integration.test';
5-
import './test/hour.test';
5+
import './test/hour.test';
6+
import './test/format.test';

test/day.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'tape';
22
import {getDay, getNDays} from '../src/day';
33

4-
const TEST_DAY = '12.28.2016';
4+
const TEST_DAY = '12.28.2016 14:24:05';
55

66
test('Calendar Model: Day', nest => {
77
nest.test('Returns a Day Object when Asked for a Day Provided a String Date', assert => {
@@ -24,11 +24,31 @@ test('Calendar Model: Day', nest => {
2424
assert.ok(day.weekOfYear === 52, 'should be 52');
2525
assert.end();
2626
});
27+
nest.test('Returns a 24-hour time', assert => {
28+
const day = getDay({date: TEST_DAY});
29+
assert.ok(day.twentyFourHourTime === '14:24:05', 'should be 14:24:05');
30+
assert.end();
31+
});
32+
nest.test('Returns a 12-hour time', assert => {
33+
const day = getDay({date: TEST_DAY});
34+
assert.ok(day.twelveHourTime === '02:24:05', 'should be 02:24:05');
35+
assert.end();
36+
});
2737
nest.test('Returns a one-based month number', assert => {
2838
const day = getDay({date: TEST_DAY});
2939
assert.ok(day.month === 12, 'should be 12');
3040
assert.end();
3141
});
42+
nest.test('Returns a formatted date', assert => {
43+
const day = getDay({date: TEST_DAY});
44+
assert.ok(day.formattedDate === '28/12/2016', 'should be 28/12/2016');
45+
assert.end();
46+
});
47+
nest.test('Returns an ISO formatted date', assert => {
48+
const day = getDay({date: TEST_DAY});
49+
assert.ok(day.month === 12, 'should be 12');
50+
assert.end();
51+
});
3252
nest.test('Returns a set of days, given a start date and a count', assert => {
3353
const daySet = getNDays({startDate: TEST_DAY, numOfDays: 4});
3454
assert.ok(daySet.length === 5, 'should be 5 days');

test/format.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'tape';
2+
import {addLeadingZeroIfLessThanTen} from '../src/format';
3+
4+
test('Calendar Model: Format', nest => {
5+
nest.test('Should format a time value under 10 with a leading zero', assert => {
6+
assert.ok(addLeadingZeroIfLessThanTen(9) === '09', 'should have a leading zero');
7+
assert.end();
8+
});
9+
});

test/hour.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import test from 'tape';
22
import * as hours from '../src/hours';
33

44
test('Calendar Model: Hours', nest => {
5-
nest.test('Should format a time value under 10 with a leading zero', assert => {
6-
assert.ok(hours.formatTimeSlot(9) === '09', 'should have a leading zero');
7-
assert.end();
8-
});
95
nest.test('Should return the correct value for a twelve hour clock', assert => {
106
assert.ok(hours.getTwelveHourTime(14) === 2, 'should be 2');
117
assert.end();

test/integration.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import makeEventFinder from '../src/event';
55
const TEST_DATE = '03/01/2017';
66

77
test('Calendar Model: Events', nest => {
8-
nest.test('Returns a set of days with populated events', assert => {
9-
const getEvents = makeEventFinder({ '01/03/2017': ['one', 'two', 'three'] });
10-
const week = getNWeeks({ startDate: TEST_DATE, getEvents, numOfWeeks: 0 });
11-
assert.comment('events: ' + week[3].events);
12-
assert.ok(week[3].events, 'should not be empty');
13-
assert.end();
14-
});
8+
nest.test('Returns a set of days with populated events', assert => {
9+
const getEvents = makeEventFinder({'01/03/2017': ['one', 'two', 'three']});
10+
const week = getNWeeks({startDate: TEST_DATE, getEvents, numOfWeeks: 0});
11+
assert.ok(week[3].events, 'should not be empty');
12+
assert.end();
13+
});
1514
});

0 commit comments

Comments
 (0)