-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (51 loc) · 1.77 KB
/
index.js
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
import { timeParse, timeFormat } from "d3-time-format";
const parseDate = timeParse("%B %d, %Y");
const formatDate = timeFormat("%m/%d/%Y");
const contains = (line, symbol) => ~line.indexOf(symbol);
const parsePoundage = (line) => +line.match(/[0-9]*lb/)[0].replace("lb", "");
export const parse = (rawText) => {
const lines = rawText.split("\n").map((line) => line.trim());
let exercise;
let date;
const rows = [];
lines.forEach((line) => {
if (contains(line, "@")) {
let reps, poundage, multiplier, notes;
const [beforeAt, afterAt] = line.split("@").map((str) => str.trim());
const beforeAtLowerCase = beforeAt.toLowerCase();
if (contains(beforeAtLowerCase, "x")) {
const [beforeX, afterX] = beforeAtLowerCase.split("x");
multiplier = +beforeX;
reps = +afterX;
} else {
multiplier = 1;
reps = +beforeAt;
}
const numbersAfterAt = afterAt.match(/[0-9]*/);
poundage = +numbersAfterAt[0];
const row = { exercise, reps, poundage };
const hasTextAfterNumbers = numbersAfterAt[0].length < afterAt.length;
if (hasTextAfterNumbers) {
const textAfterNumbers = afterAt
.substring(numbersAfterAt[0].length)
.trim();
row[exercise ? "notes" : "exercise"] = textAfterNumbers;
}
if (date) {
row.date = date;
}
for (let i = 0; i < multiplier; i++) {
rows.push(row);
}
} else if (contains(line, "Workout")) {
date = formatDate(parseDate(line.replace("Workout", "").trim()));
} else if (contains(line, "lb")) {
rows.push({ exercise, notes: line, poundage: parsePoundage(line) });
} else if (line === "") {
exercise = null;
} else {
exercise = line;
}
});
return rows;
};