Skip to content

Commit

Permalink
Variable coupling
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Ronvel committed Sep 14, 2014
1 parent 9fcee3d commit 9ee72eb
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ ls line: two
one more time: two
one more time: three
end: one
one - un
two - deux
three - trois
onE
two
thrEE
Expand Down Expand Up @@ -155,6 +158,22 @@ cleanup( function() {
} ) ;
```

should iterate through a variable as a list using coupled variable substitution.

```js
cleanup( function() {

var book = new spellcast.Book( fs.readFileSync( 'spellbook' ).toString() ) ;

book.cast( 'foreach-coupled' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'foreach-coupled' ) ).to.be( 'one - un\ntwo - deux\nthree - trois\n' ) ;
done() ;
} ) ;
} ) ;
```

<a name="transmute-block"></a>
# 'transmute' block
should execute a regular expression to a variable as a list.
Expand Down
19 changes: 19 additions & 0 deletions bdd-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ls line: two
one more time: two
one more time: three
end: one
one - un
two - deux
three - trois
onE
two
thrEE
Expand Down Expand Up @@ -137,6 +140,22 @@ cleanup( function() {
} ) ;
```

should iterate through a variable as a list using coupled variable substitution.

```js
cleanup( function() {

var book = new spellcast.Book( fs.readFileSync( 'spellbook' ).toString() ) ;

book.cast( 'foreach-coupled' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'foreach-coupled' ) ).to.be( 'one - un\ntwo - deux\nthree - trois\n' ) ;
done() ;
} ) ;
} ) ;
```

<a name="transmute-block"></a>
# 'transmute' block
should execute a regular expression to a variable as a list.
Expand Down
1 change: 1 addition & 0 deletions format.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ When a variable is encountered, it is substituted by its string value.
List of blocks that supports formula:
* *scroll*'s command, when: at execution of each command
* *scroll*'s *write-formula* argument
* *scroll*'s *only-index* argument

TODOC: list formula

Expand Down
68 changes: 62 additions & 6 deletions lib/spellcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ spellcast.Book.prototype.castSummon = function castSummon( summon , castExecutio

spellcast.Book.prototype.castScroll = function castScroll( scroll , castExecution , callback )
{
var plan , splitter , self = this ;
var plan , self = this ;

plan = async
.map( scroll.shellCommands , this.execShellCommand.bind( this , castExecution , scroll.args ) )
Expand All @@ -844,17 +844,48 @@ spellcast.Book.prototype.castScroll = function castScroll( scroll , castExecutio
else { scroll.args['write-formula'] = this.variableSubstitution( scroll.args['write-formula'] ) ; }
}

// Perform variable substitution on only-index's argument
if ( scroll.args['only-index'] && typeof scroll.args['only-index'] === 'string' )
{
scroll.args['only-index'] = this.variableSubstitution( scroll.args['only-index'] ) ;
}

// Let's exec the plan and process the final outcome!

plan.exec( function( error , outputMap ) {

var splitter , splitted , formula ;

if ( error ) { callback( error ) ; return ; }

if ( scroll.args['write-formula'] )
{
formula = scroll.args['write-formula'] ;
splitter = scroll.args.splitter || '\n' ;
self.formula[ scroll.args['write-formula'] ] = outputMap.join( '' ).trim().split( splitter ) ;
Object.defineProperty( self.formula[ scroll.args['write-formula'] ] , 'index' , { value: 0 , writable: true } ) ;
splitted = outputMap.join( '' ).trim().split( splitter ) ;

if ( scroll.args['only-index'] )
{
if ( ! self.formula[ formula ] )
{
self.formula[ formula ] = [] ;
Object.defineProperty( self.formula[ formula ] , 'index' , { value: 0 , writable: true } ) ;
}

if ( typeof scroll.args['only-index'] === 'string' )
{
self.formula[ formula ][ parseInt( scroll.args['only-index'] ) ] = splitted[ 0 ] ;
}
else
{
self.formula[ formula ][ self.formula[ formula ].index ] = splitted[ 0 ] ;
}
}
else
{
self.formula[ formula ] = splitted ;
Object.defineProperty( self.formula[ formula ] , 'index' , { value: 0 , writable: true } ) ;
}

/*
console.log( "outputMap:" , outputMap[0] ) ;
Expand Down Expand Up @@ -974,10 +1005,35 @@ spellcast.Book.prototype.variableSubstitution = function variableSubstitution( i
{
var output , self = this ;

output = input.replace( /\$\{([^\s\}]+)\}/g , function() {
var substitute = self.formula[ arguments[ 1 ] ] ;
output = input.replace( /\$\{([0-9a-zA-Z_-]+)(:([0-9a-zA-Z_-]+))?\}/g , function() {
var index , thirdParty , substitute = self.formula[ arguments[ 1 ] ] ;

if ( substitute === undefined ) { return '' ; }
return substitute[ substitute.index ] ;

if ( arguments[ 3 ] )
{
// Find a specific index

if ( arguments[ 3 ].match( /^[0-9]+$/ ) )
{
// This is a fixed numeric index
index = parseInt( arguments[ 3 ] ) ;
}
else
{
// Get the numeric index of a third party formula
thirdParty = self.formula[ arguments[ 3 ] ] ;
if ( thirdParty === undefined ) { return '' ; }
index = thirdParty.index ;
}
}
else
{
// Normal substitution mode: replace by the variable current index
index = substitute.index ;
}

return substitute[ index ] ;
} ) ;

return output ;
Expand Down
6 changes: 6 additions & 0 deletions test/spellbook
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ formula
alert:bob
blah:blih
list:one,two,three
list-fr:un,deux,trois



Expand All @@ -27,6 +28,11 @@ formula
scroll
echo end: ${list}

.foreach-coupled
foreach list
scroll
echo ${list} - ${list-fr:list}

.transmute
transmute list
/e/g
Expand Down
15 changes: 15 additions & 0 deletions test/spellcast-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ describe( "'foreach' block" , function() {
} ) ;
} ) ;
} ) ;

it( "should iterate through a variable as a list using coupled variable substitution" , function( done ) {

cleanup( function() {

var book = new spellcast.Book( fs.readFileSync( 'spellbook' ).toString() ) ;

book.cast( 'foreach-coupled' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'foreach-coupled' ) ).to.be( 'one - un\ntwo - deux\nthree - trois\n' ) ;
done() ;
} ) ;
} ) ;
} ) ;
} ) ;


Expand Down

0 comments on commit 9ee72eb

Please sign in to comment.