-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLineDestructor.js
More file actions
111 lines (87 loc) · 2.91 KB
/
LineDestructor.js
File metadata and controls
111 lines (87 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const moment = require('moment');
/**
* Inverse of builder pattern. We're destructing builded line.
*/
class LineDestructor {
constructor(line, date = moment(), projectsMap = {}) {
this.line = line;
this.date = date;
this.projectsMap = projectsMap;
}
async toObject(options = {}) {
this.modificated = this.line;
// TODO: handle missing PID
const pid = await this.desctructProjectId();
const tags = this.desctructTags();
const [start, duration] = await this.desctructStartAndDuration();
const description = this.desctructDescription();
const billable = this.destructProjectIsBillable(pid);
return {
...options,
description,
tags,
pid,
start,
duration,
billable,
};
}
desctructTags() {
const tagRegexp = /#\w+/g;
const matches = this.modificated.match(tagRegexp);
this.modificated = this.modificated.replace(tagRegexp, '');
return !matches ? [] : matches.map(tag => tag.slice(1));
}
async desctructProjectId() {
let projectId = this.desctuctProjectIdInLine();
if (!projectId) {
projectId = await this.desctructProjectIdByName();
}
return projectId;
}
desctuctProjectIdInLine() {
const projectIdRegexp = /\[[0-9]+\]/;
const matches = this.modificated.match(projectIdRegexp);
if (!matches) {
return undefined;
}
const projectId = matches.map(match => match.trim().slice(1, -1))[0];
let parsedProjectId = parseInt(projectId, 10);
if (Number.isNaN(parsedProjectId)) {
parsedProjectId = undefined;
} else {
this.modificated = this.modificated.replace(projectIdRegexp, '');
}
return parsedProjectId;
}
async desctructProjectIdByName() {
const tokens = Object.keys(this.projectsMap)
.map(name => `\\[${name}\\]`);
const regexp = new RegExp(tokens.join('|'));
const projectName = this.modificated
.match(regexp)
.map(match => match.trim().slice(1, -1))[0];
const projectId = this.projectsMap[projectName].id;
this.modificated = this.modificated.replace(regexp, '');
return projectId;
}
destructProjectIsBillable(id) {
const project = Object.values(this.projectsMap).find(p => p.id === id);
return project.isBillable;
}
desctructStartAndDuration() {
const hoursRegexp = /^\s*([0-9]{1,2}):([0-9]{2})\s*-\s*([0-9]{1,2}):([0-9]{2})[\s-]*/;
const [, startHour, startMinute, endHour, endMinute] = hoursRegexp.exec(this.modificated);
const startMoment = moment(this.date).hour(startHour).minute(startMinute);
const endMoment = moment(this.date).hour(endHour).minute(endMinute);
const duration = endMoment.diff(startMoment, 'seconds');
this.modificated = this.modificated.replace(hoursRegexp, '');
return [startMoment.toDate(), duration];
}
desctructDescription() {
return this.modificated
.replace(/\s+/g, ' ')
.trim();
}
}
module.exports = LineDestructor;