Skip to content

Commit 489951e

Browse files
committed
rewrite action parser to use sets
1 parent 77d2a9c commit 489951e

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/build/parser/actions.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
import {trimQuotes, trimCommandValue, trimArray} from './cleanup';
22
import {isAction, isArray} from './match';
33

4-
// TODO: change to use new Set ()
5-
6-
function doAction(
7-
type: CR.OutputAction, isArray, actionValue, result, line,
8-
{page, task}
9-
): CR.Output {
4+
function doAction({
5+
type, isArray, actionValue, result,
6+
index: {page, task}
7+
}): CR.Output {
108
// set to array
119
if (result.pages[page].tasks[task][type] === undefined) {
1210
result.pages[page].tasks[task][type] = [];
1311
}
14-
let current = result.pages[page].tasks[task][type];
12+
let current = new Set(result.pages[page].tasks[task][type]);
1513
if (!!isArray) {
1614
// array
1715
let values = trimArray(actionValue);
18-
values.forEach((value) => {
19-
if (current.indexOf(value) === -1 && values.indexOf(value) === -1) {
20-
result.pages[page].tasks[task][type].push(value);
21-
}
16+
values.forEach((v) => {
17+
current.add(v);
2218
});
2319
} else {
2420
// string
25-
if (current.indexOf(actionValue) === -1) {
26-
result.pages[page].tasks[task][type].push(actionValue);
27-
}
21+
current.add(actionValue);
2822
}
23+
result.pages[page].tasks[task][type] = Array.from(current);
2924
return result;
3025
}
3126

32-
export function addToTasks(result, line, index) {
27+
export function addToTasks({ result, line, index }) {
3328
let action: CR.TaskAction | string = isAction(line); // 'action'|'test'|'hint'|'openConsole'
3429
const {page, task} = index;
3530
let currentTask: CR.Task = result.pages[page].tasks[task];
@@ -38,10 +33,22 @@ export function addToTasks(result, line, index) {
3833
let isActionArray = isArray(trimQuotes(actionValue));
3934
switch (action) {
4035
case 'test':
41-
result = doAction('tests', isActionArray, actionValue, result, line, index);
36+
result = doAction({
37+
type: 'tests',
38+
isArray: isActionArray,
39+
actionValue,
40+
result,
41+
index
42+
});
4243
break;
4344
case 'hint':
44-
result = doAction('hints', isActionArray, actionValue, result, line, index);
45+
result = doAction({
46+
type: 'hints',
47+
isArray: isActionArray,
48+
actionValue,
49+
result,
50+
index
51+
});
4552
break;
4653
case 'continue':
4754
break;
@@ -51,8 +58,8 @@ export function addToTasks(result, line, index) {
5158
}
5259
if (!!isActionArray) {
5360
var arrayOfActions: string[] = JSON.parse(isActionArray);
54-
arrayOfActions.forEach(function(value) {
55-
value = trimCommandValue(trimQuotes(value.trim()));
61+
arrayOfActions.forEach((v) => {
62+
let value = trimCommandValue(trimQuotes(v.trim()));
5663
result.pages[page].tasks[task].actions.push(value);
5764
});
5865
} else {

src/build/parser/task.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as Match from './match';
2-
import {page} from './page';
3-
import {addToTasks} from './actions';
4-
import {trimLeadingSpaces, bracketTracker, trimValue} from './cleanup';
5-
import {loadImport} from './import';
2+
import { page } from './page';
3+
import { addToTasks } from './actions';
4+
import { trimLeadingSpaces, bracketTracker, trimValue } from './cleanup';
5+
import { loadImport } from './import';
66

77
export function task({
88
dir, result, lines, index
@@ -52,7 +52,7 @@ export function task({
5252
} else {
5353
// multi-line action finished
5454
currentAction += line;
55-
result = addToTasks(result, currentAction, index);
55+
result = addToTasks({ result, line: currentAction, index });
5656
currentAction = null;
5757
bracketCount = 0;
5858
}
@@ -78,7 +78,7 @@ export function task({
7878
// TODO: bracket has completed bracket
7979
if (bracketCount === 0) {
8080
// if action complete, call addToTasks with merged lines
81-
result = addToTasks(result, currentAction, index);
81+
result = addToTasks({ result, line: currentAction, index });
8282
currentAction = null;
8383
}
8484
continue;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy