Skip to content

Commit 5d58fae

Browse files
committed
handle array of actions, @onComplete
1 parent 5023f19 commit 5d58fae

File tree

7 files changed

+74
-26
lines changed

7 files changed

+74
-26
lines changed

lib/build/parser/actions.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ function doAction(type, isArray, actionValue, result, line, index) {
66
result.chapters[index.chapter].pages[index.page].tasks[index.task][type] = [];
77
}
88
if (!!isArray) {
9-
var valueList = actionValue.slice(1, -1).split(',');
10-
valueList.forEach(function (value) {
11-
value = cleanup_1.trimQuotes(value.trim(), true);
9+
var values = cleanup_1.trimArray(actionValue);
10+
values.forEach(function (value) {
1211
result.chapters[index.chapter].pages[index.page].tasks[index.task][type].push(value);
1312
});
1413
}

lib/build/parser/cleanup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function trimLineBreaks(text) {
1212
return text;
1313
}
1414
exports.trimLineBreaks = trimLineBreaks;
15+
function trimArray(text) {
16+
var array = text.slice(1, -1).match(/(".*?"|[^\s",][^",]+[^\s",])(?=\s*,|\s*$)/g) || [];
17+
return array.map(function (item) {
18+
return trimQuotes(trimLineBreaks(item), true);
19+
});
20+
}
21+
exports.trimArray = trimArray;
1522
var quotes = ['\'', '"'];
1623
function trimQuotes(text, quotesOnly) {
1724
if (!!text.match(/^[\r\n]/)) {

src/build/parser/actions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import {trimQuotes, trimCommandValue} from './cleanup';
1+
import {trimQuotes, trimCommandValue, trimArray} from './cleanup';
22
import * as Match from './match';
33

4-
function doAction(type: CR.OutputAction, isArray, actionValue, result, line, index) {
4+
function doAction(type: CR.OutputAction, isArray, actionValue, result, line, index): CR.Output {
55
// set to array
66
if (result.chapters[index.chapter].pages[index.page].tasks[index.task][type] === undefined) {
77
result.chapters[index.chapter].pages[index.page].tasks[index.task][type] = [];
88
}
99
if (!!isArray) {
10-
let valueList: string[] = actionValue.slice(1, -1).split(',');
11-
valueList.forEach((value) => {
12-
value = trimQuotes(value.trim(), true);
10+
// array
11+
let values = trimArray(actionValue);
12+
values.forEach((value) => {
1313
result.chapters[index.chapter].pages[index.page].tasks[index.task][type].push(value);
1414
});
1515
} else {
16+
// string
1617
result.chapters[index.chapter].pages[index.page].tasks[index.task][type].push(actionValue);
1718
}
1819
return result;

src/build/parser/cleanup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ export function bracketTracker(line: string): number {
44
return l - r;
55
}
66

7-
export function trimLineBreaks(text: string) {
7+
export function trimLineBreaks(text: string): string {
88
if (text.match(/^\s+|\s+$/)) {
99
text = text.replace(/^[\s\r\n]+|[\s\r\n]+$/g, '');
1010
}
1111
return text;
1212
}
1313

14+
export function trimArray(text: string): string[] {
15+
let array: string[] = text.slice(1, -1).match(/(".*?"|[^\s",][^",]+[^\s",])(?=\s*,|\s*$)/g) || [];
16+
return array.map(function(item) {
17+
return trimQuotes(trimLineBreaks(item), true);
18+
});
19+
}
20+
1421
let quotes = ['\'', '"'];
1522

1623
export function trimQuotes(text: string, quotesOnly?: boolean): string {

test/build-cleanup.spec.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
var expect = require('chai').expect;
2-
var trimQuotes = require('../lib/build/parser/cleanup').trimQuotes;
3-
var trimLineBreaks = require('../lib/build/parser/cleanup').trimLineBreaks;
2+
var cleanup = require('../lib/build/parser/cleanup');
3+
var trimQuotes = cleanup.trimQuotes;
4+
var trimLineBreaks = cleanup.trimLineBreaks;
5+
var trimArray = cleanup.trimArray;
6+
7+
describe('trimArray', function () {
8+
9+
it('should trim an array', function () {
10+
var test = "['hint 1', 'hint 2']";
11+
expect(trimArray(test)).to.deep.equal([
12+
'hint 1',
13+
'hint 2'
14+
]);
15+
});
16+
17+
it('should trim an array of string literals', function () {
18+
var test = "['`var a = 42;`', '`var b = 12;`']"
19+
expect(trimArray(test)).to.deep.equal([
20+
'`var a = 42;`',
21+
'`var b = 12;`'
22+
]);
23+
});
24+
});
425

526
describe('trimQuotes', function () {
627

test/build-onComplete.spec.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ describe('@onComplete', function() {
2323
return {
2424
chapters: [{
2525
pages: [{
26+
title: '',
27+
description: '',
2628
tasks: []
2729
}]
2830
}]
2931
};
3032
};
3133

34+
var indexTask = function() {
35+
return {
36+
chapter: 0,
37+
page: 0,
38+
task: -1
39+
};
40+
};
41+
3242
it('should add @onComplete string to the page', function() {
3343
var lines = ['### Page One', 'page description', 'more page description', '@onComplete("next page")'];
3444
var next = page(result(), lines, index());
@@ -42,7 +52,8 @@ describe('@onComplete', function() {
4252

4353
it('should handle multi-line codeblocks', function() {
4454
var lines = ['### Page One', 'page description', 'more page description',
45-
'@onComplete("next page', '```', 'var a = 42;', '```', '")'];
55+
'@onComplete("next page', '```', 'var a = 42;', '```', '")'
56+
];
4657
var next = page(result(), lines, index());
4758
var nextPage = next.chapters[0].pages[0];
4859
expect(nextPage).to.deep.equal({
@@ -52,22 +63,24 @@ describe('@onComplete', function() {
5263
});
5364
});
5465

55-
it('should handle string literals', function() {
56-
var lines = ['### Page One', 'page description', 'more page description',
57-
'@onComplete("next page', '`var a = 42;`', '")'];
58-
var next = page(result(), lines, index());
59-
var nextPage = next.chapters[0].pages[0];
60-
expect(nextPage).to.deep.equal({
61-
title: 'Page One',
62-
description: 'page description\nmore page description',
63-
onComplete: 'next page\n`var a = 42;`'
64-
});
66+
it('should handle string literals', function() {
67+
var lines = ['### Page One', 'page description', 'more page description',
68+
'@onComplete("next page', '`var a = 42;`', '")'
69+
];
70+
var next = page(result(), lines, index());
71+
var nextPage = next.chapters[0].pages[0];
72+
expect(nextPage).to.deep.equal({
73+
title: 'Page One',
74+
description: 'page description\nmore page description',
75+
onComplete: 'next page\n`var a = 42;`'
6576
});
77+
});
6678

67-
xit('shouldn\'t add @onComplete string to a task', function() {
68-
var lines = ['+ Task One', 'with more on the next', 'line', '@onComplete("next page")'];
69-
var next = task(resultTask(), lines, index());
79+
it('shouldn\'t add to tasks', function() {
80+
var lines = ['+ Task One', 'with more on the next', 'line'];
81+
var next = task(resultTask(), lines, indexTask());
7082
var nextTask = next.chapters[0].pages[0].tasks[0];
7183
expect(nextTask.description).to.equal('Task One\nwith more on the next\nline');
7284
});
85+
7386
});

test/hint.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ describe('@hint', function() {
103103
],
104104
description: 'Task One\n'
105105
});
106-
})
106+
});
107107

108108
}); // @hint

0 commit comments

Comments
 (0)