Skip to content

Instantly share code, notes, and snippets.

@jwerle
Created April 9, 2014 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwerle/10284742 to your computer and use it in GitHub Desktop.
Save jwerle/10284742 to your computer and use it in GitHub Desktop.
Grep a function's source body
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];
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