Skip to main content

Case

.case() is a Gizmo method that allows you to write complex conditional logic in a way which is easier to read, write, debug and explain than long complex nested if() statements

This expression:

var pr = node.performanceRanking;
if (pr > 8) {
("A");
} else if (pr > 6) {
("B");
} else if (pr > 4) {
("C");
} else {
("D");
}

Can be written using .case() as

node.performanceRanking.case(is.gt(8), "A", is.gt(6), "B", is.gt(4), "C", "D")

Fallback option using caseโ€‹

Case operates as pairs. A final entry (unpaired) provides the fallback (else) case.

In the example above, "D" is the else case.

node.grade.case(is.blank, "Grade is missing")

image

  • A. If the "grade" property is blank the string "Grade is missing" is returned
  • B. If no else case is provided, the value is passed through in this example the grade is passed through unchanged

Using Case with Isโ€‹

.case() combined with is is powerful, and can be used to express complex cases very succinctly:

node.grade.case(
is.blank, "Grade is missing",
is.gt(node.p.grade), "Grade is higher than Managers!",
"Grade difference okay")

image

Using case over a collection of nodesโ€‹

.case() can map over a collection of nodes in this example testing all children of the selected node

node.c.grade.case(is.blank, "Grade is missing")

image

Using case to test multiple valuesโ€‹

Case can also be used in combination with match to test multiple properties at the same time.

node.case(
is.match({
department: "Finance",
location: "UK"}),
"UK Finance Team",
is.match({
department: "Executive",
grade: is.gt(3)}),
"Leadership Team")

If none of the conditions are met then the label of the node will be returned