You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2013/07/08 17:59:57 UTC

[04/21] git commit: updated refs/heads/1846-dev-server-improvements to 5da4bbf

Use Array#forEach instead of for .. in

Also improved the wording in a few places.


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/e204f0d0
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/e204f0d0
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/e204f0d0

Branch: refs/heads/1846-dev-server-improvements
Commit: e204f0d0dbe9ae6385bcc225218731105fd5ff5e
Parents: ba3adb1
Author: Michael Jackson <mj...@gmail.com>
Authored: Sat Jun 29 12:47:05 2013 -0700
Committer: Michael Jackson <mj...@gmail.com>
Committed: Sat Jun 29 12:47:05 2013 -0700

----------------------------------------------------------------------
 share/doc/src/ddocs.rst | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/e204f0d0/share/doc/src/ddocs.rst
----------------------------------------------------------------------
diff --git a/share/doc/src/ddocs.rst b/share/doc/src/ddocs.rst
index 368f49e..ada5b0d 100644
--- a/share/doc/src/ddocs.rst
+++ b/share/doc/src/ddocs.rst
@@ -48,25 +48,23 @@ Map functions
 
    :param doc: Processed document object.
 
-Map functions should take a single argument as document object and optionally
-emits paired values also known as `key` and `value`. Since JavaScript
-doesn't support generators and ``yield`` statement it is emulated via :func:`emit`:
+Map functions accept a single document as the argument and (optionally) :func:`emit`
+key/value pairs that are stored in a view.
 
 .. code-block:: javascript
 
-    function(doc){
-      if (!doc.tags || !isArray(doc.tags) || !doc.type || doc.type != 'post'){
-        return;
-      }
-      for (var idx in doc.tags){
-        emit(doc.tags[idx].toLower(), 1);
+    function (doc) {
+      if (doc.type === 'post' && doc.tags && Array.isArray(doc.tags)) {
+        doc.tags.forEach(function (tag) {
+          emit(tag.toLowerCase(), 1);
+        });
       }
     }
 
-In this example the map function produces documents view by tag if they
-has `tags` attribute as array and `type` attribute with "post" value. Note that
-:func:`emit` function could be called  multiple times, so the same document
-will be available by several `keys`.
+In this example a key/value pair is emitted for each value in the `tags` array
+of a document with a `type` of "post". Note that :func:`emit` may be called many
+times for a single document, so the same document may be available by several
+different keys.
 
 Also keep in mind that each document is *sealed* to prevent situation when one
 map function changes document state and the other one received a modified
@@ -77,7 +75,7 @@ each document is processed by group of map functions from all views of
 related design document. This means that if you trigger index update for one
 view in ddoc, all others will get updated too.
 
-Since `1.1.0` release `map` function supports 
+Since `1.1.0` release `map` function supports
 :ref:`CommonJS <commonjs>` modules and access to :func:`require` function.
 
 .. _reducefun:
@@ -128,7 +126,7 @@ JavaScript below:
 
     // could be replaced by _count
     function(keys, values, rereduce){
-      if (rereduce){
+      if (rereduce) {
         return sum(values);
       } else {
         return values.length;
@@ -143,11 +141,13 @@ JavaScript below:
         'max': Math.max.apply(null, values),
         'count': values.length,
         'sumsqr': (function(){
-          _sumsqr = 0;
-          for(var idx in values){
-            _sumsqr += values[idx] * values[idx];
-          }
-          return _sumsqr;
+          var sumsqr = 0;
+
+          values.forEach(function (value) {
+            sumsqr += value * value;
+          });
+
+          return sumsqr;
         })(),
       }
     }
@@ -208,7 +208,7 @@ Basic example of show function could be:
 .. code-block:: javascript
 
     function(doc, req){
-      if (doc){
+      if (doc) {
         return "Hello from " + doc._id + "!";
       } else {
         return "Hello, world!";
@@ -712,7 +712,7 @@ permissions:
 
 .. note::
 
-   The ``return`` statement used only for function, it has no impact on 
+   The ``return`` statement used only for function, it has no impact on
    the validation process.
 
 .. seealso::