Skip to content

Commit 67b4c78

Browse files
committed
Basic markdown parsing and bubble handling
1 parent f263c93 commit 67b4c78

File tree

9 files changed

+225
-50
lines changed

9 files changed

+225
-50
lines changed

.karma.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
browsers = [
2-
"Chrome"
2+
"Chrome",
33
// "PhantomJS"
44
];
55

src/bubble.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ export default class Bubble {
33
this.label = label;
44

55
this.date = {
6-
start: start,
7-
end: end
6+
start: Date.parse(start),
7+
end: Date.parse(end)
88
};
99
}
1010

src/bubble.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Bubble from '../src/bubble.js';
22

33
describe('Bubble', function () {
4-
const b = new Bubble(1, 2, "label");
4+
const b = new Bubble("2015-01-01T00:00:00Z", "2015-01-01", "label");
55

66
it('should have a start and end value', () => {
7-
expect(b.Start()).toEqual(1);
8-
expect(b.End()).toEqual(2);
7+
expect(b.Start()).toEqual(1420070400000);
8+
expect(b.End()).toEqual(1420070400000);
99
});
1010

1111
it("should have a label", () => {

src/list.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export default class List {
2+
constructor(sort) {
3+
this.storage = [];
4+
this.position = 0;
5+
this.sorter = sort
6+
}
7+
8+
sort() {
9+
if (!this.sorter) {
10+
return;
11+
}
12+
13+
this.storage.sort(this.sorter)
14+
}
15+
16+
Next() {
17+
this.position++;
18+
19+
return this.storage[this.position-1];
20+
}
21+
22+
Add() {
23+
for (var i = 0, m = arguments.length; i < m; i++) {
24+
this.storage.push(arguments[i])
25+
}
26+
27+
this.sort();
28+
}
29+
30+
Walk(func) {
31+
return this.storage.forEach(func);
32+
}
33+
34+
First() {
35+
return this.storage[0]
36+
}
37+
38+
Last() {
39+
return this.storage[this.Size()-1]
40+
}
41+
42+
Get() {
43+
return this.storage;
44+
}
45+
46+
Size() {
47+
return this.storage.length;
48+
}
49+
}

src/list.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import List from '../src/list.js';
2+
3+
describe('List', function () {
4+
it("should be initialized with length 0", () => {
5+
const l = new List();
6+
7+
expect(l.Size()).toEqual(0);
8+
});
9+
10+
it("should be able to add element and have size 1", () => {
11+
const l = new List();
12+
13+
l.Add(1);
14+
15+
expect(l.Size()).toEqual(1);
16+
});
17+
18+
it("should be able to add elements and have size 3", () => {
19+
const l = new List();
20+
21+
l.Add(1);
22+
l.Add(2);
23+
l.Add(3);
24+
25+
expect(l.Size()).toEqual(3);
26+
});
27+
28+
it("should be able to add multiple elements and have size 4", () => {
29+
const l = new List();
30+
31+
l.Add(0);
32+
l.Add(1, 2, 3);
33+
34+
expect(l.Size()).toEqual(4);
35+
});
36+
37+
it("should be able to add multiple elements and use Next() for access", () => {
38+
const l = new List();
39+
40+
l.Add(0);
41+
l.Add(1, 2, 3);
42+
43+
var cur = l.Next();
44+
expect(cur).toEqual(0);
45+
var cur = l.Next();
46+
expect(cur).toEqual(1);
47+
var cur = l.Next();
48+
expect(cur).toEqual(2);
49+
var cur = l.Next();
50+
expect(cur).toEqual(3);
51+
});
52+
53+
it("should be able to add multiple elements and use Next() for access with custom sorter", () => {
54+
const l = new List((a, b) => {
55+
return a < b ? 1 : -1;
56+
});
57+
58+
l.Add(0);
59+
l.Add(1, 2, 3);
60+
61+
var cur = l.Next();
62+
expect(cur).toEqual(3);
63+
var cur = l.Next();
64+
expect(cur).toEqual(2);
65+
var cur = l.Next();
66+
expect(cur).toEqual(1);
67+
var cur = l.Next();
68+
expect(cur).toEqual(0);
69+
});
70+
});

src/main.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1+
import Parser from "./parser.js"
2+
13
export default class Timesheet {
2-
constructor(name) {
3-
this.name = name
4+
constructor(html) {
5+
var p = new Parser()
6+
7+
this.list = p.parse(html)
8+
}
9+
10+
Start() {
11+
var start = null;
12+
13+
this.list.Walk((item) => {
14+
if (start === null || item.Start() < start) {
15+
start = item.Start();
16+
}
17+
});
18+
19+
return start;
20+
}
21+
22+
End() {
23+
var end = null;
24+
25+
this.list.Walk((item) => {
26+
if (end === null || item.End() > end) {
27+
end = item.End();
28+
}
29+
});
30+
31+
return end;
432
}
533
}

src/main.spec.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
import Timesheet from '../src/main.js';
22

3+
var toDOM = function(text) {
4+
var div = document.createElement('div');
5+
div.innerHTML = text;
6+
7+
return div.childNodes[1];
8+
}
9+
310
describe('Timesheet', function () {
4-
const foo = new Timesheet('custom');
11+
var input = toDOM(`
12+
<ul class="timesheet">
13+
<li class="timesheet-item">
14+
<span class="timesheet-item--date-start">2015-01-01T00:00:00Z</span>
15+
<span class="timesheet-item--label">example-0</span>
16+
<span class="timesheet-item--date-end">2015-01-12T00:00:00Z</span>
17+
</li>
18+
<li class="timesheet-item">
19+
<span class="timesheet-item--date-start">2014-01-01T00:00:00Z</span>
20+
<span class="timesheet-item--label">example-1</span>
21+
<span class="timesheet-item--date-end">2015-01-12T00:00:00Z</span>
22+
</li>
23+
<li class="timesheet-item">
24+
<span class="timesheet-item--date-start">2014-02-01</span>
25+
<span class="timesheet-item--label">example-1</span>
26+
<span class="timesheet-item--date-end">2014-05-01</span>
27+
</li>
28+
<li class="timesheet-item">
29+
<span class="timesheet-item--date-start">2014-12-01</span>
30+
<span class="timesheet-item--label">example-1</span>
31+
<span class="timesheet-item--date-end">2015-05-12</span>
32+
</li>
33+
</ul>
34+
`);
535

6-
it('name should be set to "custom"', () => {
7-
expect(foo.name).toEqual('custom');
36+
const t = new Timesheet(input);
37+
38+
it("should calculate the first element from list of bubbles", () => {
39+
expect(t.Start()).toEqual(1388534400000)
840
});
41+
42+
it("should calculate the last element from list of bubbles", () => {
43+
expect(t.End()).toEqual(1431388800000)
44+
})
945
});

src/parser.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
import Bubble from './bubble.js'
2+
import List from './list.js'
23

34
export default class Parser {
45
constructor() {
56

67
}
78

8-
parse(list) {
9-
if (!list.classList.contains('timesheet')) {
10-
return [];
9+
parse(html) {
10+
var list = new List();
11+
12+
if (!html.classList.contains('timesheet')) {
13+
return list;
1114
}
1215

13-
var items = list.querySelectorAll('.timesheet-item');
16+
var items = html.querySelectorAll('.timesheet-item');
1417
if (items.length === 0) {
15-
return [];
18+
return list;
1619
}
1720

18-
var data = []
21+
1922
for (var i = 0, m = items.length; i < m; i++) {
2023
var item = items[i];
2124

2225
var dateStart = item.querySelector('.timesheet-item--date-start')
2326
, dateEnd = item.querySelector('.timesheet-item--date-end')
2427
, label = item.querySelector('.timesheet-item--label');
2528

26-
data.push(new Bubble(
29+
if (dateStart === null || dateEnd === null || label === null) {
30+
continue;
31+
}
32+
33+
list.Add(new Bubble(
2734
dateStart ? dateStart.innerHTML : null,
2835
dateEnd ? dateEnd.innerHTML : null,
2936
label ? label.innerHTML : null
3037
))
3138
}
3239

33-
return data;
40+
return list;
3441
}
3542
}

src/parser.spec.js

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Parser', function () {
1616
</ul>
1717
`);
1818

19-
expect(p.parse(input)).toEqual([]);
19+
expect(p.parse(input).Size()).toEqual(0);
2020
});
2121

2222
it('should return an empty array for lists without timesheet class', () => {
@@ -26,7 +26,7 @@ describe('Parser', function () {
2626
</ul>
2727
`);
2828

29-
expect(p.parse(input)).toEqual([]);
29+
expect(p.parse(input).Size()).toEqual(0);
3030
});
3131

3232
it('should return an empty array for lists with timesheet class but without timesheet-item elements', () => {
@@ -36,67 +36,52 @@ describe('Parser', function () {
3636
</ul>
3737
`);
3838

39-
expect(p.parse(input)).toEqual([]);
39+
expect(p.parse(input).Size()).toEqual(0);
4040
});
4141

4242
it('should return an array for lists with timesheet class and timesheet-item elements', () => {
4343
var input = toDOM(`
4444
<ul class="timesheet">
4545
<li class="timesheet-item">
46-
<span class="timesheet-item--date-start">2015-01-01</span>
47-
<span class="timesheet-item--date-end">2015-01-12</span>
46+
<span class="timesheet-item--date-start">January 1, 2015</span>
47+
<span class="timesheet-item--date-end">January 12, 2015</span>
4848
<span class="timesheet-item--label">example</span>
4949
</li>
5050
</ul>
5151
`);
5252

53-
expect(p.parse(input).length).toEqual(1);
53+
expect(p.parse(input).Size()).toEqual(1);
5454
});
5555

5656
it('should return an array of bubbles for lists with timesheet class and timesheet-item elements', () => {
5757
var input = toDOM(`
5858
<ul class="timesheet">
5959
<li class="timesheet-item">
60-
<span class="timesheet-item--date-start">2015-01-01</span>
61-
<span class="timesheet-item--date-end">2015-01-12</span>
60+
<span class="timesheet-item--date-start">2015-01-01T00:00:00Z</span>
6261
<span class="timesheet-item--label">example-0</span>
62+
<span class="timesheet-item--date-end">2015-01-12</span>
6363
</li>
6464
<li class="timesheet-item">
65-
<span class="timesheet-item--date-end">2015-02-12</span>
6665
<span class="timesheet-item--label">example-1</span>
66+
<span class="timesheet-item--date-end">February 4, 2015</span>
6767
</li>
6868
<li class="timesheet-item">
69-
<span class="timesheet-item--date-start">2015-03-01</span>
69+
<span class="timesheet-item--date-start">March 1, 2015</span>
7070
<span class="timesheet-item--label">example-2</span>
7171
</li>
7272
<li class="timesheet-item">
73-
<span class="timesheet-item--date-start">2015-04-01</span>
74-
<span class="timesheet-item--date-end">2015-04-12</span>
73+
<span class="timesheet-item--date-end">April 12, 2015</span>
74+
<span class="timesheet-item--date-start">April 1, 2015</span>
7575
</li>
7676
</ul>
7777
`);
7878

7979
var output = p.parse(input);
80-
expect(output.length).toEqual(4);
80+
expect(output.Size()).toEqual(1);
8181

82-
var item = output.shift();
83-
expect(item.Start()).toEqual('2015-01-01')
84-
expect(item.End()).toEqual('2015-01-12')
82+
var item = output.Next();
83+
expect(item.Start()).toEqual(1420070400000)
84+
expect(item.End()).toEqual(1421020800000)
8585
expect(item.Label()).toEqual('example-0')
86-
87-
var item = output.shift();
88-
expect(item.Start()).toEqual(null)
89-
expect(item.End()).toEqual('2015-02-12')
90-
expect(item.Label()).toEqual("example-1")
91-
92-
var item = output.shift();
93-
expect(item.Start()).toEqual("2015-03-01")
94-
expect(item.End()).toEqual(null)
95-
expect(item.Label()).toEqual("example-2")
96-
97-
var item = output.shift();
98-
expect(item.Start()).toEqual("2015-04-01")
99-
expect(item.End()).toEqual("2015-04-12")
100-
expect(item.Label()).toEqual(null)
10186
});
10287
});

0 commit comments

Comments
 (0)