Skip to content

Latest commit

 

History

History
96 lines (76 loc) · 1.95 KB

branch.md

File metadata and controls

96 lines (76 loc) · 1.95 KB
layout api-command
language JavaScript
permalink api/javascript/branch/
command branch
io
r
any
related_commands
do
do/

Command syntax

{% apibody %} r.branch(test, true_action[, test2, test2_action, ...], false_action) → any test.branch(true_action[, test2, test2_action, ...], false_action) → any {% endapibody %}

Description

Perform a branching conditional equivalent to if-then-else.

The branch command takes 2n+1 arguments: pairs of conditional expressions and commands to be executed if the conditionals return any value but false or null (i.e., "truthy" values), with a final "else" command to be evaluated if all of the conditionals are false or null.

You may call branch infix style on the first test. (See the second example for an illustration.)

r.branch(test1, val1, test2, val2, elseval)

is the equivalent of the JavaScript statement

if (test1) {
    return val1;
} else if (test2) {
    return val2;
} else {
    return elseval;
}

Example: Test the value of x.

var x = 10;
r.branch(r.expr(x).gt(5), 'big', 'small').run(conn, callback);
// Result passed to callback
"big"

Example: As above, infix-style.

var x = 10;
r.expr(x).gt(5).branch('big', 'small').run(conn, callback);
// Result passed to callback
"big"

Example: Categorize heroes by victory counts.

r.table('marvel').map(
    r.branch(
        r.row('victories').gt(100),
        r.row('name').add(' is a superhero'),
        r.row('victories').gt(10),
        r.row('name').add(' is a hero'),
        r.row('name').add(' is very nice')
    )
).run(conn, callback);

If the documents in the table marvel are:

[
    { name: "Iron Man", victories: 214 },
    { name: "Jubilee", victories: 49 },
    { name: "Slava", victories: 5 }
]

The results will be:

[
    "Iron Man is a superhero",
    "Jubilee is a hero",
    "Slava is very nice"
]