Skip to content

Commit

Permalink
variable substitution filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Ronvel committed Sep 15, 2014
1 parent d66dc83 commit 74baced
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 28 deletions.
14 changes: 14 additions & 0 deletions format.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ TODOC: coupled formula using ${some-formula:some-index-formula} notation



## Filter

We can use builtin filter at substitution time, using the `${formula/filter}` syntax.

Valid filters:
* uppercase
* lowercase
* regexp: escape for regexp pattern
* shellarg: escape for a shell argument





# References

## Top level
Expand Down
53 changes: 40 additions & 13 deletions lib/spellcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,14 @@ spellcast.Book.prototype.castTransmute = function castTransmute( transmute , cas

spellcast.Book.prototype.variableSubstitution = function variableSubstitution( input )
{
var output , self = this ;
var i , filters , output , self = this ;

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

//console.log( 'arguments:' , arguments ) ;

var index , value , thirdParty , substitute = self.formula[ arguments[ 1 ] ] ;

if ( substitute === undefined ) { return '' ; }

Expand All @@ -1036,14 +1040,46 @@ spellcast.Book.prototype.variableSubstitution = function variableSubstitution( i
index = substitute.index ;
}

return substitute[ index ] ;
value = substitute[ index ] ;

// Filters
if ( arguments[ 4 ] )
{
filters = arguments[ 4 ].split( '/' ) ;
filters.shift() ;

for ( i = 0 ; i < filters.length ; i ++ )
{
//console.log( "filter #" + i + ":" , filters[ i ] ) ;

if ( spellcast.filter[ filters[ i ] ] )
{
value = spellcast.filter[ filters[ i ] ]( value ) ;
}
}
}

return value ;
} ) ;

return output ;
} ;



spellcast.filter = {
'uppercase': function uppercase( str ) { return str.toUpperCase() ; } ,
'lowercase': function lowercase( str ) { return str.toLowerCase() ; } ,
'regexp': function escapeRegExp( str ) {
return str.replace( /([.*+?^${}()|\[\]\/\\])/g , "\\$1" ) ;
} ,
'shellarg' : function escapeShellArg( str ) {
return '\'' + str.replace( /\'/g , "'\\''" ) + '\'' ;
}
} ;



spellcast.Book.prototype.argsVariableSubstitution = function argsVariableSubstitution( inArgs )
{
var key , args = {} ;
Expand All @@ -1059,15 +1095,6 @@ spellcast.Book.prototype.argsVariableSubstitution = function argsVariableSubstit



spellcast.filter = {} ;

spellcast.filter['regexp-escape'] = function regexpEscape( str )
{
return str.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' ) ;
} ;





/* Makefile builder */
Expand Down
8 changes: 8 additions & 0 deletions test/spellbook
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
formula
alert:bob
blah:blih
allcaps:FUUU
list:one,two,three
list-fr:un,deux,trois
version:0.1.2



Expand All @@ -20,6 +22,12 @@ formula
scroll
echo ${alert} ${blah}${blah} ${list}

.kawarimi-filter
scroll
echo ${version/regexp/shellarg}
echo ${alert/uppercase}
echo ${allcaps/lowercase}

.foreach
foreach list
scroll
Expand Down
45 changes: 30 additions & 15 deletions test/spellcast-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,60 +78,75 @@ function getFizzledLog( spell )



describe( "Formula" , function() {
describe( "Formula & variable substitution" , function() {

it( "should be parsed into list of string, with an additionnal property 'index' equals to 0" , function() {
var book = new spellcast.Book( fs.readFileSync( 'spellbook' ).toString() ) ;

expect( book.formula.alert ).to.be.eql( [ 'bob' ] ) ;
expect( book.formula.list ).to.be.eql( [ 'one' , 'two' , 'three' ] ) ;
} ) ;
} ) ;



describe( "'scroll' block" , function() {

it( "should echoing echo" , function( done ) {
it( "should substitute variable (aka formula) accordingly in 'scroll' block" , function( done ) {

cleanup( function() {

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

book.cast( 'echo' , function( error )
book.cast( 'kawarimi' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'echo' ) ).to.be( 'echo\n' ) ;
expect( getCastedLog( 'kawarimi' ) ).to.be( 'bob blihblih one\n' ) ;
done() ;
} ) ;
} ) ;
} ) ;

it( "should echoing delayed-echo after one second" , function( done ) {
it( "should substitute variable (aka formula) using filters" , function( done ) {

cleanup( function() {

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

book.cast( 'delayed-echo' , function( error )
book.cast( 'kawarimi-filter' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'delayed-echo' ) ).to.be( 'delayed-echo\n' ) ;
expect( getCastedLog( 'kawarimi-filter' ) ).to.be( '0\\.1\\.2\nBOB\nfuuu\n' ) ;
done() ;
} ) ;
} ) ;
} ) ;
} ) ;



describe( "'scroll' block" , function() {

it( "should substitute variable (aka formula) accordingly in 'scroll' block" , function( done ) {
it( "should echoing echo" , function( done ) {

cleanup( function() {

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

book.cast( 'kawarimi' , function( error )
book.cast( 'echo' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'kawarimi' ) ).to.be( 'bob blihblih one\n' ) ;
expect( getCastedLog( 'echo' ) ).to.be( 'echo\n' ) ;
done() ;
} ) ;
} ) ;
} ) ;

it( "should echoing delayed-echo after one second" , function( done ) {

cleanup( function() {

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

book.cast( 'delayed-echo' , function( error )
{
expect( error ).not.ok() ;
expect( getCastedLog( 'delayed-echo' ) ).to.be( 'delayed-echo\n' ) ;
done() ;
} ) ;
} ) ;
Expand Down

0 comments on commit 74baced

Please sign in to comment.