es7.comprehensions

ES7 Comprehensions

Usage

This is a stage 0 experimental transformer. Note that the second version enables all stage 0 transformers.

require("babel").transform("code", { optional: ["es7.comprehensions"] });
require("babel").transform("code", { stage: 0 });
$ babel --optional es7.comprehensions script.js
$ babel --stage 0 script.js

Explanation

Array and generator comprehensions provide simple declarative list processing similar as used in many functional programming patterns.

// Array comprehensions
var results = [
  for (c of customers)
    if (c.city == "Seattle")
      { name: c.name, age: c.age }
]

// Generator comprehensions
var results = (
  for (c of customers)
    if (c.city == "Seattle")
      { name: c.name, age: c.age }
)