Skip to content

Commit 3b3f7e7

Browse files
committed
refactor
1 parent 898d8aa commit 3b3f7e7

4 files changed

Lines changed: 57 additions & 46 deletions

File tree

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class TimeCard {
7979
}
8080

8181
return new Promise(async resolve => {
82-
await self.getTimeCardData();
82+
await self.processTimeCardData();
8383

8484
if (self.clockoutIsPending) {
8585
console.log(self.errors.clockOutIsPending);
@@ -117,7 +117,7 @@ export default class TimeCard {
117117
}
118118

119119
return new Promise(async resolve => {
120-
await self.getTimeCardData();
120+
await self.processTimeCardData();
121121

122122
if (self.clockoutIsPending) {
123123
const date = new Date().toString();
@@ -151,7 +151,7 @@ export default class TimeCard {
151151
const self = this;
152152

153153
return new Promise(async resolve => {
154-
await self.getTimeCardData();
154+
await self.processTimeCardData();
155155

156156
let output = self.prettyPrintHeader();
157157
self.timecardData.shifts.forEach(item => {

lib/utils.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs';
33
import duration from 'pendel';
44
import sort from 'lodash.sortby';
55
import happyAsJson from 'happy-json';
6-
import {errors} from './messages.js';
6+
// import {errors} from './messages.js';
77

88
// help node 0.10 with Promises
99
require('native-promise-only');
@@ -13,42 +13,40 @@ require('native-promise-only');
1313
*
1414
* @returns {*} Promise
1515
*/
16-
export function getTimeCardData() {
16+
export function processTimeCardData() {
1717
const self = this;
1818

1919
return new Promise((resolve, reject) => {
20-
fs.readFile(self.filepath, 'utf8', (err, data) => {
21-
if (err) {
22-
reject(err);
23-
} else {
24-
self.timecardData = JSON.parse(data);
25-
self.name = self.timecardData.name;
26-
const shifts = self.timecardData.shifts;
20+
self.getTimeCard().then(data => {
21+
self.timecardData = JSON.parse(data);
22+
self.name = self.timecardData.name;
23+
const shifts = self.timecardData.shifts;
2724

28-
// reset to zero each time the timecard data is read
29-
self.totalSeconds = 0;
25+
// reset to zero each time the timecard data is read
26+
self.totalSeconds = 0;
3027

31-
if (shifts.length > 0) {
32-
self.shifts = sort(shifts, shift => {
33-
return shift.id;
34-
});
28+
if (shifts.length > 0) {
29+
self.shifts = sort(shifts, shift => {
30+
return shift.id;
31+
});
3532

36-
const lastShift = self.shifts[self.shifts.length - 1];
37-
if (lastShift.hasOwnProperty('startTime') && lastShift.hasOwnProperty('endTime') === false) {
38-
self.clockoutIsPending = true;
39-
self.pendingClockoutIndex = lastShift.id;
40-
}
41-
self.shifts.map(item => {
42-
if (item.hasOwnProperty('startTime') && item.hasOwnProperty('endTime')) {
43-
self.totalSeconds += duration(item.startTime, item.endTime).totalSeconds;
44-
}
45-
});
46-
resolve(self.shifts);
47-
} else {
48-
self.shifts = [];
49-
resolve(self.timecardData);
33+
const lastShift = self.shifts[self.shifts.length - 1];
34+
if (lastShift.hasOwnProperty('startTime') && lastShift.hasOwnProperty('endTime') === false) {
35+
self.clockoutIsPending = true;
36+
self.pendingClockoutIndex = lastShift.id;
5037
}
38+
self.shifts.map(item => {
39+
if (item.hasOwnProperty('startTime') && item.hasOwnProperty('endTime')) {
40+
self.totalSeconds += duration(item.startTime, item.endTime).totalSeconds;
41+
}
42+
});
43+
resolve(self.shifts);
44+
} else {
45+
self.shifts = [];
46+
resolve(self.timecardData);
5147
}
48+
}).catch(err => {
49+
reject(err);
5250
});
5351
});
5452
}
@@ -71,6 +69,25 @@ export function writeTimeCard(data) {
7169
});
7270
}
7371

72+
/**
73+
* Read the .timecard.json file from disk and return its contents
74+
*
75+
* @returns {*} Promise
76+
*/
77+
export function getTimeCard() {
78+
const self = this;
79+
80+
return new Promise((resolve, reject) => {
81+
fs.readFile(self.filepath, 'utf8', (err, data) => {
82+
if (err) {
83+
reject(err);
84+
} else {
85+
resolve(data);
86+
}
87+
});
88+
});
89+
}
90+
7491
/**
7592
* The blank .timecard.json value.
7693
*/
@@ -82,15 +99,9 @@ export function blankTimecard(name) {
8299
* Check if .timecard.json is in a valid json format.
83100
*/
84101
export function checkForValidTimecard() {
85-
try {
86-
var timecardData = require('../.timecard.json');
87-
88-
if (happyAsJson(timecardData) === false) {
89-
throw new Error('Invalid timecard data.');
90-
}
91-
} catch (err) {
92-
console.log(errors.invalidTimeCardJSON);
93-
console.log(' ' + err);
94-
process.exit(1);
95-
}
102+
this.getTimeCard().then(data => {
103+
return happyAsJson(data);
104+
}).catch(() => {
105+
return false;
106+
});
96107
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "timecard",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "Keep track of your project development time",
55
"main": "timecard.js",
66
"scripts": {

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ test.serial('records total seconds', async t => {
8787
t.true(timecard.totalSeconds >= 3);
8888
});
8989

90-
test.serial('utils.getTimeCardData', async t => {
90+
test.serial('utils.processTimeCardData', async t => {
9191
await timecard.create();
9292
await timecard.clockin();
93-
timecard.getTimeCardData().then(data => {
93+
timecard.processTimeCardData().then(data => {
9494
t.true(data);
9595
t.is(typeof data, 'object');
9696
t.is(data[0].id, 0);

0 commit comments

Comments
 (0)