refactor(forEach): change to for-of with iterable

rename: foreach -> for
rename: array -> iterable
update: DartParseTreeWriter
update: naive_infinite_scroll
update: todo
fix: tests in foreach_spec

Closes #919
This commit is contained in:
gdi2290
2015-03-13 16:22:01 -07:00
committed by Misko Hevery
parent f1fca5abb6
commit b61b8d60b7
14 changed files with 80 additions and 76 deletions

View File

@ -1,11 +1,11 @@
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
import {ArrayChanges} from 'angular2/src/change_detection/pipes/array_changes';
import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
import {NumberWrapper} from 'angular2/src/facade/lang';
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {TestIterable} from './iterable';
import {arrayChangesAsString} from './util';
import {iterableChangesAsString} from './util';
// todo(vicb): UnmodifiableListView / frozen object when implemented
export function main() {
@ -15,7 +15,7 @@ export function main() {
var l;
beforeEach(() => {
changes = new ArrayChanges();
changes = new IterableChanges();
});
afterEach(() => {
@ -23,30 +23,30 @@ export function main() {
});
it('should support list and iterables', () => {
expect(ArrayChanges.supportsObj([])).toBeTruthy();
expect(ArrayChanges.supportsObj(new TestIterable())).toBeTruthy();
expect(ArrayChanges.supportsObj(MapWrapper.create())).toBeFalsy();
expect(ArrayChanges.supportsObj(null)).toBeFalsy();
expect(IterableChanges.supportsObj([])).toBeTruthy();
expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
expect(IterableChanges.supportsObj(null)).toBeFalsy();
});
it('should support iterables', () => {
l = new TestIterable();
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: []
}));
l.list = [1];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]'],
additions: ['1[null->0]']
}));
l.list = [2, 1];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]'],
previous: ['1[0->1]'],
additions: ['2[null->0]'],
@ -57,20 +57,20 @@ export function main() {
it('should detect additions', () => {
l = [];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: []
}));
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'],
additions: ['a[null->0]']
}));
ListWrapper.push(l, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'],
previous: ['a'],
additions: ['b[null->1]']
@ -83,7 +83,7 @@ export function main() {
l = [1, 0];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['1[null->0]', '0[0->1]'],
previous: ['0[0->1]'],
additions: ['1[null->0]'],
@ -92,7 +92,7 @@ export function main() {
l = [2, 1, 0];
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
previous: ['1[0->1]', '0[1->2]'],
additions: ['2[null->0]'],
@ -108,7 +108,7 @@ export function main() {
ListWrapper.push(l, 2);
ListWrapper.push(l, 1);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['2[1->0]', '1[0->1]'],
previous: ['1[0->1]', '2[1->0]'],
moves: ['2[1->0]', '1[0->1]']
@ -122,7 +122,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
ListWrapper.insert(l, 0, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]']
@ -131,7 +131,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b', 'c[2->1]', 'a[1->2]'],
previous: ['b', 'a[1->2]', 'c[2->1]'],
moves: ['c[2->1]', 'a[1->2]']
@ -144,14 +144,14 @@ export function main() {
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a[null->0]'],
additions: ['a[null->0]']
}));
ListWrapper.push(l, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]'],
previous: ['a'],
additions: ['b[null->1]']
@ -160,7 +160,7 @@ export function main() {
ListWrapper.push(l, 'c');
ListWrapper.push(l, 'd');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
previous: ['a', 'b'],
additions: ['c[null->2]', 'd[null->3]']
@ -168,7 +168,7 @@ export function main() {
ListWrapper.removeAt(l, 2);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b', 'd[3->2]'],
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
moves: ['d[3->2]'],
@ -181,7 +181,7 @@ export function main() {
ListWrapper.push(l, 'b');
ListWrapper.push(l, 'a');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'],
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
additions: ['c[null->1]'],
@ -197,7 +197,7 @@ export function main() {
var oo = 'oo';
ListWrapper.set(l, 1, b + oo);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'boo'],
previous: ['a', 'boo']
}));
@ -207,7 +207,7 @@ export function main() {
l = [NumberWrapper.NaN];
changes.check(l);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: [NumberWrapper.NaN],
previous: [NumberWrapper.NaN]
}));
@ -219,7 +219,7 @@ export function main() {
ListWrapper.insert(l, 0, 'foo');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
previous: ['NaN[0->1]', 'NaN[1->2]'],
additions: ['foo[null->0]'],
@ -233,7 +233,7 @@ export function main() {
ListWrapper.removeAt(l, 1);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'c[2->1]'],
previous: ['a', 'b[1->null]', 'c[2->1]'],
moves: ['c[2->1]'],
@ -242,7 +242,7 @@ export function main() {
ListWrapper.insert(l, 1, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'b[null->1]', 'c[1->2]'],
previous: ['a', 'c[1->2]'],
additions: ['b[null->1]'],
@ -256,7 +256,7 @@ export function main() {
ListWrapper.removeAt(l, 0);
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['a', 'a', 'b[3->2]', 'b[4->3]'],
previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'],
moves: ['b[3->2]', 'b[4->3]'],
@ -270,7 +270,7 @@ export function main() {
ListWrapper.insert(l, 0, 'b');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'],
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
additions: ['b[null->4]'],
@ -287,7 +287,7 @@ export function main() {
ListWrapper.push(l, 'a');
ListWrapper.push(l, 'c');
changes.check(l);
expect(changes.toString()).toEqual(arrayChangesAsString({
expect(changes.toString()).toEqual(iterableChangesAsString({
collection: ['b[1->0]', 'a[0->1]', 'c'],
previous: ['a[0->1]', 'b[1->0]', 'c'],
moves: ['b[1->0]', 'a[0->1]']

View File

@ -1,6 +1,6 @@
import {isBlank} from 'angular2/src/facade/lang';
export function arrayChangesAsString({collection, previous, additions, moves, removals}) {
export function iterableChangesAsString({collection, previous, additions, moves, removals}) {
if (isBlank(collection)) collection = [];
if (isBlank(previous)) previous = [];
if (isBlank(additions)) additions = [];