Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert function source code to a string in NodeJS/JavaScript

I am creating JavaScript functions for map/reduce operations on database server side. To have syntax highlighting and errors check I make it like this inside my text editor:

var map = function (doc, meta) {
  emit([doc.type, doc.created], doc);
};

Then I need to somehow turn it into a string variable so that to add it as a view to Couchbase looking like this:

var map = "function (doc, meta) { emit([doc.type, doc.created], doc); };"

Is that possible to convert the source code to a string somehow?

like image 955
Sergei Basharov Avatar asked Sep 06 '13 18:09

Sergei Basharov


1 Answers

Function.toString is your friend.

Try:

map.toString();
like image 69
Joon Avatar answered Nov 11 '22 19:11

Joon