couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <paul.joseph.da...@gmail.com>
Subject Re: Am I understanding this correctly?
Date Sat, 03 Aug 2013 16:49:00 GMT
That's mostly right. The truth table you describe isn't quite right
for the value assigned to the key variable. Its album.title unless its
falsy in which case its album.name. Here falsy can mean more than the
strict "null" value you mention. The second bit is that the value
assigned to the variable named "value" is an object and not a tuple.

On Fri, Aug 2, 2013 at 8:55 PM, Yves S. Garret
<yoursurrogategod@gmail.com> wrote:
> This is the piece of Javascript code that I'm working with:
>
> function(doc) {
>   if('name' in doc && 'albums' in doc) {
>     doc.albums.forEach(function(album) {
>       var key = album.title || album.name;
>       var value = {by: doc.name, album: album};
>
>       emit(key, value);
>       //emit(key, key);
>     });
>   }
> }
>
> Now, this is my interpretation of how I view this code.
> - The function checks if there is a name and an albums field in the doc.
> - Then, using the doc, it accesses the list of albums and executes for each
> element in the list a
> method.
> - That method accepts one element -- by design -- and only one element.
>  That element is the
> individual value that's stored in the list.  In this case, the format of
> the list is assumed to be uniform and known in advance (but this is not
> guaranteed and would need extra checking to take into
> account).
> - In the function, the first variable is created by checking either
> album.title or album.name and
> selecting the one that is not null (if both are null, then you have a
> problem, but would assign to the
> key a null).
> - The value is created in a similar fashion by returning a tuple.
> - The results are outputted.
>
> Is my understanding of this correct?  Did I read the code correctly?

Mime
View raw message