Skip to content

Instantly share code, notes, and snippets.

@xhinking
Last active July 15, 2016 17:33
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 xhinking/a933410848b7f2d986a570c3426dd48a to your computer and use it in GitHub Desktop.
Save xhinking/a933410848b7f2d986a570c3426dd48a to your computer and use it in GitHub Desktop.
function lambda( l ) {
var fn = l.match(/\((.*)\)\s*=>\s*(.*)/) ;
var p = [] ;
var b = "" ;
if ( fn.length > 0 ) fn.shift() ;
if ( fn.length > 0 ) b = fn.pop() ;
if ( fn.length > 0 ) p = fn.pop()
.replace(/^\s*|\s(?=\s)|\s*$|,/g, '').split(' ') ;
// prepend a return if not already there.
fn = ( ( ! /\s*return\s+/.test( b ) ) ? "return " : "" ) + b ;
p.push( fn ) ;
try {
return Function.apply( {}, p ) ;
}
catch(e) {
return null ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment