11import { trimQuotes , trimCommandValue , trimArray } from './cleanup' ;
22import * as Match from './match' ;
33
4- function doAction ( type : CR . OutputAction , isArray , actionValue , result , line , index ) : CR . Output {
4+ // TODO: change to use new Set ()
5+
6+ function doAction ( type : CR . OutputAction , isArray , actionValue , result , line , { chapter, page, task} ) : CR . Output {
57 // set to array
6- if ( result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] [ type ] === undefined ) {
7- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] [ type ] = [ ] ;
8+ if ( result . chapters [ chapter ] . pages [ page ] . tasks [ task ] [ type ] === undefined ) {
9+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] [ type ] = [ ] ;
810 }
911 if ( ! ! isArray ) {
1012 // array
1113 let values = trimArray ( actionValue ) ;
1214 values . forEach ( ( value ) => {
13- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] [ type ] . push ( value ) ;
15+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] [ type ] . push ( value ) ;
1416 } ) ;
1517 } else {
1618 // string
17- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] [ type ] . push ( actionValue ) ;
19+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] [ type ] . push ( actionValue ) ;
1820 }
1921 return result ;
2022}
2123
2224export function addToTasks ( result , line , index ) {
2325 let action : CR . TaskAction | string = Match . isAction ( line ) ; // 'action'|'test'|'hint'|'continue'
24- let task : CR . Task = result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] ;
26+ const { chapter, page, task} = index ;
27+ let currentTask : CR . Task = result . chapters [ chapter ] . pages [ page ] . tasks [ task ] ;
2528 let trimmedContent : string = line . slice ( action . length + 2 , line . length - 1 ) ; // content between brackets
2629 let actionValue : string = trimQuotes ( trimmedContent ) ;
2730 let isActionArray = Match . isArray ( trimQuotes ( actionValue ) ) ;
@@ -35,18 +38,18 @@ export function addToTasks(result, line, index) {
3538 case 'continue' :
3639 break ;
3740 case 'action' :
38- if ( task . actions === undefined ) {
39- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . actions = [ ] ;
41+ if ( currentTask . actions === undefined ) {
42+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] . actions = [ ] ;
4043 }
4144 if ( ! ! isActionArray ) {
4245 var arrayOfActions : string [ ] = JSON . parse ( isActionArray ) ;
4346 arrayOfActions . forEach ( function ( value ) {
4447 value = trimCommandValue ( trimQuotes ( value . trim ( ) ) ) ;
45- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . actions . push ( value ) ;
48+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] . actions . push ( value ) ;
4649 } ) ;
4750 } else {
4851 let value : string = trimCommandValue ( actionValue ) ;
49- result . chapters [ index . chapter ] . pages [ index . page ] . tasks [ index . task ] . actions . push ( value ) ;
52+ result . chapters [ chapter ] . pages [ page ] . tasks [ task ] . actions . push ( value ) ;
5053 }
5154 return result ;
5255
0 commit comments