Transforms ES6 Array Comprehensions to ES5 compliant equivalent.
var squared = for x of 12345 if x > 2 x * x ;
var ;var ;varvar $__result = ;for var $__0 = $__getIterator12345 $__1; !$__1 = $__0nextdone;var x = $__1value;if x > 2$__resultpushx * x;return $__result;bindthis;
I'm glad to inform you that es6-comprehensions is now a part of es-next project.
For more information check out the current draft for ECMAScript 6.
Please notice that the syntax has changed and many resources is still using the old one.
$ npm install es6-comprehensions [--save]
Array comprehensions progressed to the Draft ECMAScript 6 Specification. It doesn't mean that there will be no changes or that array comprehensions will be included in the final ES6 Specification.
ES6 defines also iterators that can be used together with for-of loops that can be used in array comprehensions. This translator does not support iterators in
for-of loops. It translates
for-of loops to plain
for loops. Thus, it supports only plain JS arrays.
- Consider replacing plain
forEachmethod. It will result in more compact code,
- Consider migration to escodegen. Removed in order to follow up esnext's dependencies.
- Clone the repository.
- Do your changes.
Pull requests are highly appreciated.
- Switched to
- Added support for
for...ofloop. (Thanks @vslinko for PR.) Important! Generated code contains two more necessary functions.
- Binding current scope to the generated function expression. This allows one to use
thisin an array comprehension. An example can be found in a test file. (Thanks @vslinko for PR.)
- Using ast-util to generate safe temporary variables.
- Replaced ComprehensionExpression with CallExpression instead of ExpressionStatement.
- Changed API to conform to esnext's requirements.