gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

use function to replace #48

Closed jean-alain-re closed 9 years ago

jean-alain-re commented 9 years ago

From this ticket, https://github.com/gkz/grasp/issues/22 , we can use a function to replace.

I try this and it doesn't work.

 var code = grasp.replace('equery' , 'require($str)' , function( $str ){
                return path.join( __dirname , $str ) + '.js'
            }, '... require("./a/b") ...')

I have this error.

Option 'replace': Invalid type for 'function ( $str ){
                return path.join( __dirname , $str ) + '.js'
            }' - expected type 'replacement::String'.

How can i use this options?

dy commented 9 years ago

+1. Same error. It seems to be a trivial but a very useful thing to do. I’m writing a browserify-transform graspify, and there are a bunch of use-cases where passing a function to replace comes handy. For example, if you need to make some calculations based on matched result, like eval simple maths 1000*60*60*24, Math.PI / 180 etc. Besides, having an API conforming with String.replace is a very good idea.

@gkz any thoughts?

dy commented 9 years ago

@gkz Seems you just have to bump package version.

gkz commented 9 years ago

grasp 0.3.0 released

dy commented 9 years ago

Great!