Coder Social home page Coder Social logo

es6-comprehensions's Introduction

es6-comprehensions

Compiles JavaScript array comprehensions (proposed in ES6) to ES5-compatible syntax. For instance:

var squared = [ for (x of [1,2,3,4,5]) if (x > 2) x * x ];

compiles to:

var $__getIterator = function(iterable) { ... };
var $__arrayIterator = function(array) { ... };

var squared = function() {
  var $__result = [];

  for (var $__0 = $__getIterator([1,2,3,4,5]), $__1; !($__1 = $__0.next()).done; ) {
    var x = $__1.value;

    if (x > 2) {
      $__result.push(x * x);
    }
  }

  return $__result;
}.bind(this)();

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.

Installation

$ npm install es6-comprehensions [--save]

Support

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.

Todo

  • Consider replacing plain for loop with forEach method. It will result in more compact code, Invalid as for..of support was added in version 0.3.0.
  • Consider migration to escodegen. Removed in order to follow up esnext's dependencies.

Development

  1. Clone the repository.
  2. Run npm install.
  3. Do your changes.

Pull requests are highly appreciated.

Changelog

v0.3.1

v0.3.0

  • Added support for for..of loop. (Thanks @vslinko for PR.) Important! Generated code contains two more necessary functions.

v0.2.3

  • Binding current scope to the generated function expression. This allows one to use this in an array comprehension. An example can be found in a test file. (Thanks @vslinko for PR.)

v0.2.2

  • Using ast-util to generate safe temporary variables.

v0.2.1

  • Replaced ComprehensionExpression with CallExpression instead of ExpressionStatement.

v0.2.0

  • Changed API to conform to esnext's requirements.

License

BSD

es6-comprehensions's People

Contributors

adambabik avatar eventualbuddha avatar vslinko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

vslinko

es6-comprehensions's Issues

Make compatible with esnext?

I'd love to use this project as part of esnext, but it looks like the API isn't the same. I wanted to check with you to see if you'd be willing to change the API of this project to the API used by the other projects used by esnext. That is, the main module should export two methods:

  • transform(object): object: takes an Esprima AST and returns a modified AST that replaces, in this project's case, ComprehensionExpression nodes with the equivalent ES5
  • compile(string): { code: string, map: object }: this method compiles the given source code by parsing it into an AST, passing the AST to transform, and using recast.print() to get both the code and the source map

You currently have a transform method, but it's closer to my compile method. And parse is similar to my transform method except that it takes source code as input instead of an AST.

Let me know if you'd be willing to discuss this API change!

Syntax is out of date

Draft spec has newer syntax, e.g.

[  for (x of [1,2,3,4,5]) square(x) ]

instead of the obsolete

[ square(x) for (x of [1,2,3,4,5]) ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.