Do you clean your map/reduce functions before submission? How?

只愿长相守 提交于 2019-12-13 18:28:34

问题


In couchdb, one can define map and reduce functions in Javascript, and submit them via HTTP POST to the server to define views. Cool.

Apps specify a string representation of the respective functions.

I am building a JS app that connects to couchdb, using emacs, a text editor. This lets me define map and reduce functions like this:

var byname = function(doc) { 
   emit(doc.name,null);
};

This gives me emacs syntax highlighting, jslint checks, and so on.

At runtime, I can get the string representation of each one of a set of view functions defined like that, to send it to CouchDB, by doing this:

doc = { views: { empty:   { map:stringRep(empty) },
                 byname:  { map:stringRep(byname) },
                 invalid: { map:stringRep(invalid) }}};

// PUT that doc to couchdb here, to define the design_doc . 

But the string representations include newlines, carriage returns, and so on.

I could filter those out with a regex replacement, which works for the simple case. But there is also the more complex case of commented lines in the function. For example:

var byname = function(doc) { 
   // a comment here
   emit(doc.name,null);
};

In this case the stringRep will include the comment but if I eliminate the newlines and dummy whitespace, the comments remain. I could replace those too, but there may be other problems (not sure).

Is there a typical or recommended way that people sanitize their javascript functions before sending them to couchdb?


回答1:


No, there's nothing special. They are interpreted/compiled by regular JavaScript engine (SpiderMonkey) and don't need any preprocessing or cleaning up or whatever. Write them as you would any other script.

Most practices used on web have exactly same meaning or effect - you can minify everything you send if you somehow manage to send thousands of different functions to DB and want to save bandwidth; you can compile code from other language to JS, etc, etc.




回答2:


No.

We not only don't clean them, we have them in cofeescript in a design file which gets inserted by a cake task which auto-converts them to JS.




回答3:


This is what I ended up using:

CouchDB.stringRep = function(fn) {
    return fn.toString()
        .replace(/[\s\t]*\/\/.*$/gm, '') // comments
        .replace(/\n */gm, ' ')
        .replace(/\r */gm, ' ')
        .replace(/\{ +/gm, '{')
        .replace(/ +\}/gm, '}');
};

And then I upload the functions serialized this way, with this kind of HTTP message:

PUT https://foo.cloudant.com/fop/_design/baseViews HTTP/1.1
Accept: text/plain,application/json
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Content-Length: ...
Host: foo.cloudant.com

{"views":{"empty":{"map":"function(doc) {if ( ! doc.observation || doc.observation === '') {emit(doc.id, 1);}}"},...}} 


来源:https://stackoverflow.com/questions/11250534/do-you-clean-your-map-reduce-functions-before-submission-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!