implement array comprehension
This commit is contained in:
@@ -45,9 +45,9 @@
|
||||
| [Computed property names](FEATURES.md#computed-property-names) | ✓ | |
|
||||
| [Constants](FEATURES.md#constants) | ✓ | |
|
||||
| [Binary and Octal Literals](FEATURES.md#binary-and-octal-literals) | ✓ | |
|
||||
| [Iterators](FEATURES.md#iterators) | | ✓ |
|
||||
| [Iterators](FEATURES.md#iterators) | ✓ | ✓ |
|
||||
| [Array comprehension](FEATURES.md#array-comprehension) | ✓ | |
|
||||
| [Destructuring assignment](FEATURES.md#destructuring-assignment) | | |
|
||||
| [Array comprehension](FEATURES.md#array-comprehension) | | |
|
||||
| [Generators](FEATURES.md#generators) | | |
|
||||
|
||||
## Installation
|
||||
@@ -157,6 +157,10 @@ browserify()
|
||||
|
||||
Cannot subclass built-ins such as `Date`, `Array`, `DOM` etc.
|
||||
|
||||
### Generator comprehension
|
||||
|
||||
Not supported.
|
||||
|
||||
## Comparison to Traceur
|
||||
|
||||
### Performance
|
||||
|
||||
5
lib/6to5/templates/array-comprehension-container.js
Normal file
5
lib/6to5/templates/array-comprehension-container.js
Normal file
@@ -0,0 +1,5 @@
|
||||
(function () {
|
||||
var KEY = [];
|
||||
|
||||
return KEY;
|
||||
})();
|
||||
3
lib/6to5/templates/array-comprehension-for-each.js
Normal file
3
lib/6to5/templates/array-comprehension-for-each.js
Normal file
@@ -0,0 +1,3 @@
|
||||
OBJECT.forEach(function (KEY) {
|
||||
|
||||
});
|
||||
1
lib/6to5/templates/array-push.js
Normal file
1
lib/6to5/templates/array-push.js
Normal file
@@ -0,0 +1 @@
|
||||
KEY.push(STATEMENT);
|
||||
3
lib/6to5/templates/if.js
Normal file
3
lib/6to5/templates/if.js
Normal file
@@ -0,0 +1,3 @@
|
||||
if (STATEMENT) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
var util = require("../util");
|
||||
|
||||
exports.ComprehensionExpression = function (node, parent, opts, generateUid) {
|
||||
var uid = generateUid("arr");
|
||||
|
||||
var container = util.template("array-comprehension-container", {
|
||||
KEY: uid
|
||||
});
|
||||
|
||||
var block = container.callee.body;
|
||||
var body = block.body;
|
||||
|
||||
var returnStatement = body.pop();
|
||||
|
||||
var build = function () {
|
||||
var self = node.blocks.shift();
|
||||
if (!self) return;
|
||||
|
||||
if (!self.of) {
|
||||
throw util.errorWithNode(self, "for-in array comprehension is not supported");
|
||||
}
|
||||
|
||||
var child = build();
|
||||
if (!child) {
|
||||
// last item
|
||||
|
||||
child = util.template("array-push", {
|
||||
KEY: uid,
|
||||
STATEMENT: node.body
|
||||
}, true);
|
||||
|
||||
if (node.filter) {
|
||||
var filter = util.template("if", {
|
||||
STATEMENT: node.filter
|
||||
});
|
||||
filter.consequent.body = [child];
|
||||
child = filter;
|
||||
}
|
||||
}
|
||||
|
||||
var container2 = util.template("array-comprehension-for-each", {
|
||||
OBJECT: self.right,
|
||||
KEY: self.left,
|
||||
}, true);
|
||||
|
||||
container2.expression.arguments[0].body.body = [child];
|
||||
|
||||
return container2;
|
||||
};
|
||||
|
||||
body.push(build());
|
||||
body.push(returnStatement);
|
||||
|
||||
return container;
|
||||
};
|
||||
|
||||
1
test/fixtures/array-comprehension/if/actual.js
vendored
Normal file
1
test/fixtures/array-comprehension/if/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var seattlers = [for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }];
|
||||
12
test/fixtures/array-comprehension/if/expected.js
vendored
Normal file
12
test/fixtures/array-comprehension/if/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
var seattlers = function () {
|
||||
var _arr = [];
|
||||
customers.forEach(function (c) {
|
||||
if (c.city == "Seattle") {
|
||||
_arr.push({
|
||||
name: c.name,
|
||||
age: c.age
|
||||
});
|
||||
}
|
||||
});
|
||||
return _arr;
|
||||
}();
|
||||
1
test/fixtures/array-comprehension/multiple/actual.js
vendored
Normal file
1
test/fixtures/array-comprehension/multiple/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var arr = [for (x of "abcdefgh".split("")) for (y of "12345678".split("")) (x + y)];
|
||||
11
test/fixtures/array-comprehension/multiple/expected.js
vendored
Normal file
11
test/fixtures/array-comprehension/multiple/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var arr = (function () {
|
||||
var _arr = [];
|
||||
|
||||
"abcdefgh".split("").forEach(function (x) {
|
||||
"12345678".split("").forEach(function (y) {
|
||||
_arr.push(x + y);
|
||||
});
|
||||
});
|
||||
|
||||
return _arr;
|
||||
})();
|
||||
1
test/fixtures/array-comprehension/no-in/actual.js
vendored
Normal file
1
test/fixtures/array-comprehension/no-in/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var arr = [for (i in [1, 2, 3]) i * i];
|
||||
3
test/fixtures/array-comprehension/no-in/options.json
vendored
Normal file
3
test/fixtures/array-comprehension/no-in/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "for-in array comprehension is not supported"
|
||||
}
|
||||
1
test/fixtures/array-comprehension/single/actual.js
vendored
Normal file
1
test/fixtures/array-comprehension/single/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var arr = [for (i of [1, 2, 3]) i * i];
|
||||
9
test/fixtures/array-comprehension/single/expected.js
vendored
Normal file
9
test/fixtures/array-comprehension/single/expected.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
var arr = (function () {
|
||||
var _arr = [];
|
||||
|
||||
[1, 2, 3].forEach(function (i) {
|
||||
_arr.push(i * i);
|
||||
});
|
||||
|
||||
return _arr;
|
||||
})();
|
||||
Reference in New Issue
Block a user