1
1
import { trimQuotes , trimCommandValue , trimArray } from './cleanup' ;
2
2
import { isAction , isArray } from './match' ;
3
3
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 {
10
8
// set to array
11
9
if ( result . pages [ page ] . tasks [ task ] [ type ] === undefined ) {
12
10
result . pages [ page ] . tasks [ task ] [ type ] = [ ] ;
13
11
}
14
- let current = result . pages [ page ] . tasks [ task ] [ type ] ;
12
+ let current = new Set ( result . pages [ page ] . tasks [ task ] [ type ] ) ;
15
13
if ( ! ! isArray ) {
16
14
// array
17
15
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 ) ;
22
18
} ) ;
23
19
} else {
24
20
// string
25
- if ( current . indexOf ( actionValue ) === - 1 ) {
26
- result . pages [ page ] . tasks [ task ] [ type ] . push ( actionValue ) ;
27
- }
21
+ current . add ( actionValue ) ;
28
22
}
23
+ result . pages [ page ] . tasks [ task ] [ type ] = Array . from ( current ) ;
29
24
return result ;
30
25
}
31
26
32
- export function addToTasks ( result , line , index ) {
27
+ export function addToTasks ( { result, line, index} ) {
33
28
let action :CR . TaskAction | string = isAction ( line ) ; // 'action'|'test'|'hint'|'openConsole'
34
29
const { page, task} = index ;
35
30
let currentTask :CR . Task = result . pages [ page ] . tasks [ task ] ;
@@ -38,10 +33,22 @@ export function addToTasks(result, line, index) {
38
33
let isActionArray = isArray ( trimQuotes ( actionValue ) ) ;
39
34
switch ( action ) {
40
35
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
+ } ) ;
42
43
break ;
43
44
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
+ } ) ;
45
52
break ;
46
53
case 'continue' :
47
54
break ;
@@ -51,8 +58,8 @@ export function addToTasks(result, line, index) {
51
58
}
52
59
if ( ! ! isActionArray ) {
53
60
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 ( ) ) ) ;
56
63
result . pages [ page ] . tasks [ task ] . actions . push ( value ) ;
57
64
} ) ;
58
65
} else {