Skip to content

Instantly share code, notes, and snippets.

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 anaran/56cd4c1cbd5adfd4934b to your computer and use it in GitHub Desktop.
Save anaran/56cd4c1cbd5adfd4934b to your computer and use it in GitHub Desktop.
Devtools Snippet to Stringify Couchdb View Functions
// Open window with stringified version of couchdb view functions.
function map(doc) {
if(doc.name && doc.age) {
emit(doc.name, doc.age);
}
}
function reduce(keys, values) {
var sum = 0;
values.forEach(function(element) {
sum = sum + element;
});
return sum;
}
function validate_doc_update(newDoc, oldDoc, userCtx) {
// äöüß
if (oldDoc.activity == oldDoc.activity
&& oldDoc.clockin_ms == oldDoc.clockin_ms
&& oldDoc.clockout_ms == oldDoc.clockout_ms) {
throw ({
forbidden: 'entry exists in database!'
});
}
}
var funcs = {
map: map.toSource(),
reduce: reduce.toSource(),
validate_doc_update: validate_doc_update.toSource()
};
// Make sure user gets a new tab in Unicode encoding!
// application/json does not seem to enforce that in nightly firefox as of
// 36.0a1 (2014-11-20)
window.open('data:application/json; charset=utf-8, '
+ window.encodeURIComponent(JSON.stringify(funcs, null, 2)));
{
"map": "function map(doc) {\n if(doc.name && doc.age) {\n emit(doc.name, doc.age);\n }\n}",
"reduce": "function reduce(keys, values) {\n var sum = 0;\n values.forEach(function(element) {\n sum = sum + element;\n });\n return sum;\n}",
"validate_doc_update": "function validate_doc_update(newDoc, oldDoc, userCtx) {\n // äöüß\n if (oldDoc.activity == oldDoc.activity\n && oldDoc.clockin_ms == oldDoc.clockin_ms\n && oldDoc.clockout_ms == oldDoc.clockout_ms) {\n throw ({\n forbidden: 'entry exists in database!'\n });\n }\n}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment