Created
April 9, 2014 15:45
-
-
Save jwerle/10284742 to your computer and use it in GitHub Desktop.
Grep a function's source body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function beep () { | |
var b = 2; | |
var c = 10; | |
var foo = 'foo'; | |
var biz = foo; | |
var str = foo +' '+ biz; | |
var sum = b + c; | |
return [str, sum]; | |
} | |
var search = grep(beep, 'str'); | |
var numbered = search.map(function(l, i) { return i +' '+ l; }); | |
var str = numbered.join('\n'); | |
console.log(str); | |
// 0 var str = foo +' '+ biz; | |
// 1 return [str, sum]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function grep (fn, pattern) { | |
var re = null; | |
return ( | |
fn.toString() | |
.split('\n') | |
.filter((re = RegExp(pattern), re.test.bind(re))) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment