Skip to content

Commit b61b8d6

Browse files
PatrickJSmhevery
authored andcommitted
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 angular#919
1 parent f1fca5a commit b61b8d6

File tree

14 files changed

+80
-76
lines changed

14 files changed

+80
-76
lines changed

modules/angular2/change_detection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export * from './src/change_detection/pipes/pipe';
1818
import {ProtoChangeDetector, DynamicProtoChangeDetector, JitProtoChangeDetector}
1919
from './src/change_detection/proto_change_detector';
2020
import {PipeRegistry} from './src/change_detection/pipes/pipe_registry';
21-
import {ArrayChangesFactory} from './src/change_detection/pipes/array_changes';
21+
import {IterableChangesFactory} from './src/change_detection/pipes/iterable_changes';
2222
import {KeyValueChangesFactory} from './src/change_detection/pipes/keyvalue_changes';
2323
import {NullPipeFactory} from './src/change_detection/pipes/null_pipe';
2424

@@ -31,7 +31,7 @@ export class ChangeDetection {
3131

3232
export var defaultPipes = {
3333
"iterableDiff" : [
34-
new ArrayChangesFactory(),
34+
new IterableChangesFactory(),
3535
new NullPipeFactory()
3636
],
3737
"keyValDiff" : [

modules/angular2/directives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './src/directives/foreach';
1+
export * from './src/directives/for';
22
export * from './src/directives/if';
33
export * from './src/directives/non_bindable';
44
export * from './src/directives/switch';

modules/angular2/src/change_detection/pipes/array_changes.js renamed to modules/angular2/src/change_detection/pipes/iterable_changes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import {
1616

1717
import {NO_CHANGE, Pipe} from './pipe';
1818

19-
export class ArrayChangesFactory {
19+
export class IterableChangesFactory {
2020
supports(obj):boolean {
21-
return ArrayChanges.supportsObj(obj);
21+
return IterableChanges.supportsObj(obj);
2222
}
2323

2424
create():Pipe {
25-
return new ArrayChanges();
25+
return new IterableChanges();
2626
}
2727
}
2828

29-
export class ArrayChanges extends Pipe {
29+
export class IterableChanges extends Pipe {
3030
_collection;
3131
_length:int;
3232
_linkedRecords:_DuplicateMap;
@@ -66,7 +66,7 @@ export class ArrayChanges extends Pipe {
6666
}
6767

6868
supports(obj):boolean {
69-
return ArrayChanges.supportsObj(obj);
69+
return IterableChanges.supportsObj(obj);
7070
}
7171

7272
get collection() {

modules/angular2/src/directives/foreach.js renamed to modules/angular2/src/directives/for.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
55
import {ListWrapper} from 'angular2/src/facade/collection';
66

77
@Viewport({
8-
selector: '[foreach][in]',
8+
selector: '[for][of]',
99
bind: {
10-
'iterableChanges': 'in | iterableDiff'
10+
'iterableChanges': 'of | iterableDiff'
1111
}
1212
})
13-
export class Foreach {
13+
export class For {
1414
viewContainer: ViewContainer;
1515
constructor(viewContainer:ViewContainer) {
1616
super();
@@ -34,13 +34,13 @@ export class Foreach {
3434
(movedRecord) => ListWrapper.push(recordViewTuples, new RecordViewTuple(movedRecord, null))
3535
);
3636

37-
var insertTuples = Foreach.bulkRemove(recordViewTuples, this.viewContainer);
37+
var insertTuples = For.bulkRemove(recordViewTuples, this.viewContainer);
3838

3939
changes.forEachAddedItem(
4040
(addedRecord) => ListWrapper.push(insertTuples, new RecordViewTuple(addedRecord, null))
4141
);
4242

43-
Foreach.bulkInsert(insertTuples, this.viewContainer);
43+
For.bulkInsert(insertTuples, this.viewContainer);
4444

4545
for (var i = 0; i < insertTuples.length; i++) {
4646
this.perViewChange(insertTuples[i].view, insertTuples[i].record);

modules/angular2/test/change_detection/array_changes_spec.js renamed to modules/angular2/test/change_detection/iterable_changes_spec.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
2-
import {ArrayChanges} from 'angular2/src/change_detection/pipes/array_changes';
2+
import {IterableChanges} from 'angular2/src/change_detection/pipes/iterable_changes';
33

44
import {NumberWrapper} from 'angular2/src/facade/lang';
55
import {ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
66

77
import {TestIterable} from './iterable';
8-
import {arrayChangesAsString} from './util';
8+
import {iterableChangesAsString} from './util';
99

1010
// todo(vicb): UnmodifiableListView / frozen object when implemented
1111
export function main() {
@@ -15,38 +15,38 @@ export function main() {
1515
var l;
1616

1717
beforeEach(() => {
18-
changes = new ArrayChanges();
18+
changes = new IterableChanges();
1919
});
2020

2121
afterEach(() => {
2222
changes = null;
2323
});
2424

2525
it('should support list and iterables', () => {
26-
expect(ArrayChanges.supportsObj([])).toBeTruthy();
27-
expect(ArrayChanges.supportsObj(new TestIterable())).toBeTruthy();
28-
expect(ArrayChanges.supportsObj(MapWrapper.create())).toBeFalsy();
29-
expect(ArrayChanges.supportsObj(null)).toBeFalsy();
26+
expect(IterableChanges.supportsObj([])).toBeTruthy();
27+
expect(IterableChanges.supportsObj(new TestIterable())).toBeTruthy();
28+
expect(IterableChanges.supportsObj(MapWrapper.create())).toBeFalsy();
29+
expect(IterableChanges.supportsObj(null)).toBeFalsy();
3030
});
3131

3232
it('should support iterables', () => {
3333
l = new TestIterable();
3434

3535
changes.check(l);
36-
expect(changes.toString()).toEqual(arrayChangesAsString({
36+
expect(changes.toString()).toEqual(iterableChangesAsString({
3737
collection: []
3838
}));
3939

4040
l.list = [1];
4141
changes.check(l);
42-
expect(changes.toString()).toEqual(arrayChangesAsString({
42+
expect(changes.toString()).toEqual(iterableChangesAsString({
4343
collection: ['1[null->0]'],
4444
additions: ['1[null->0]']
4545
}));
4646

4747
l.list = [2, 1];
4848
changes.check(l);
49-
expect(changes.toString()).toEqual(arrayChangesAsString({
49+
expect(changes.toString()).toEqual(iterableChangesAsString({
5050
collection: ['2[null->0]', '1[0->1]'],
5151
previous: ['1[0->1]'],
5252
additions: ['2[null->0]'],
@@ -57,20 +57,20 @@ export function main() {
5757
it('should detect additions', () => {
5858
l = [];
5959
changes.check(l);
60-
expect(changes.toString()).toEqual(arrayChangesAsString({
60+
expect(changes.toString()).toEqual(iterableChangesAsString({
6161
collection: []
6262
}));
6363

6464
ListWrapper.push(l, 'a');
6565
changes.check(l);
66-
expect(changes.toString()).toEqual(arrayChangesAsString({
66+
expect(changes.toString()).toEqual(iterableChangesAsString({
6767
collection: ['a[null->0]'],
6868
additions: ['a[null->0]']
6969
}));
7070

7171
ListWrapper.push(l, 'b');
7272
changes.check(l);
73-
expect(changes.toString()).toEqual(arrayChangesAsString({
73+
expect(changes.toString()).toEqual(iterableChangesAsString({
7474
collection: ['a', 'b[null->1]'],
7575
previous: ['a'],
7676
additions: ['b[null->1]']
@@ -83,7 +83,7 @@ export function main() {
8383

8484
l = [1, 0];
8585
changes.check(l);
86-
expect(changes.toString()).toEqual(arrayChangesAsString({
86+
expect(changes.toString()).toEqual(iterableChangesAsString({
8787
collection: ['1[null->0]', '0[0->1]'],
8888
previous: ['0[0->1]'],
8989
additions: ['1[null->0]'],
@@ -92,7 +92,7 @@ export function main() {
9292

9393
l = [2, 1, 0];
9494
changes.check(l);
95-
expect(changes.toString()).toEqual(arrayChangesAsString({
95+
expect(changes.toString()).toEqual(iterableChangesAsString({
9696
collection: ['2[null->0]', '1[0->1]', '0[1->2]'],
9797
previous: ['1[0->1]', '0[1->2]'],
9898
additions: ['2[null->0]'],
@@ -108,7 +108,7 @@ export function main() {
108108
ListWrapper.push(l, 2);
109109
ListWrapper.push(l, 1);
110110
changes.check(l);
111-
expect(changes.toString()).toEqual(arrayChangesAsString({
111+
expect(changes.toString()).toEqual(iterableChangesAsString({
112112
collection: ['2[1->0]', '1[0->1]'],
113113
previous: ['1[0->1]', '2[1->0]'],
114114
moves: ['2[1->0]', '1[0->1]']
@@ -122,7 +122,7 @@ export function main() {
122122
ListWrapper.removeAt(l, 1);
123123
ListWrapper.insert(l, 0, 'b');
124124
changes.check(l);
125-
expect(changes.toString()).toEqual(arrayChangesAsString({
125+
expect(changes.toString()).toEqual(iterableChangesAsString({
126126
collection: ['b[1->0]', 'a[0->1]', 'c'],
127127
previous: ['a[0->1]', 'b[1->0]', 'c'],
128128
moves: ['b[1->0]', 'a[0->1]']
@@ -131,7 +131,7 @@ export function main() {
131131
ListWrapper.removeAt(l, 1);
132132
ListWrapper.push(l, 'a');
133133
changes.check(l);
134-
expect(changes.toString()).toEqual(arrayChangesAsString({
134+
expect(changes.toString()).toEqual(iterableChangesAsString({
135135
collection: ['b', 'c[2->1]', 'a[1->2]'],
136136
previous: ['b', 'a[1->2]', 'c[2->1]'],
137137
moves: ['c[2->1]', 'a[1->2]']
@@ -144,14 +144,14 @@ export function main() {
144144

145145
ListWrapper.push(l, 'a');
146146
changes.check(l);
147-
expect(changes.toString()).toEqual(arrayChangesAsString({
147+
expect(changes.toString()).toEqual(iterableChangesAsString({
148148
collection: ['a[null->0]'],
149149
additions: ['a[null->0]']
150150
}));
151151

152152
ListWrapper.push(l, 'b');
153153
changes.check(l);
154-
expect(changes.toString()).toEqual(arrayChangesAsString({
154+
expect(changes.toString()).toEqual(iterableChangesAsString({
155155
collection: ['a', 'b[null->1]'],
156156
previous: ['a'],
157157
additions: ['b[null->1]']
@@ -160,15 +160,15 @@ export function main() {
160160
ListWrapper.push(l, 'c');
161161
ListWrapper.push(l, 'd');
162162
changes.check(l);
163-
expect(changes.toString()).toEqual(arrayChangesAsString({
163+
expect(changes.toString()).toEqual(iterableChangesAsString({
164164
collection: ['a', 'b', 'c[null->2]', 'd[null->3]'],
165165
previous: ['a', 'b'],
166166
additions: ['c[null->2]', 'd[null->3]']
167167
}));
168168

169169
ListWrapper.removeAt(l, 2);
170170
changes.check(l);
171-
expect(changes.toString()).toEqual(arrayChangesAsString({
171+
expect(changes.toString()).toEqual(iterableChangesAsString({
172172
collection: ['a', 'b', 'd[3->2]'],
173173
previous: ['a', 'b', 'c[2->null]', 'd[3->2]'],
174174
moves: ['d[3->2]'],
@@ -181,7 +181,7 @@ export function main() {
181181
ListWrapper.push(l, 'b');
182182
ListWrapper.push(l, 'a');
183183
changes.check(l);
184-
expect(changes.toString()).toEqual(arrayChangesAsString({
184+
expect(changes.toString()).toEqual(iterableChangesAsString({
185185
collection: ['d[2->0]', 'c[null->1]', 'b[1->2]', 'a[0->3]'],
186186
previous: ['a[0->3]', 'b[1->2]', 'd[2->0]'],
187187
additions: ['c[null->1]'],
@@ -197,7 +197,7 @@ export function main() {
197197
var oo = 'oo';
198198
ListWrapper.set(l, 1, b + oo);
199199
changes.check(l);
200-
expect(changes.toString()).toEqual(arrayChangesAsString({
200+
expect(changes.toString()).toEqual(iterableChangesAsString({
201201
collection: ['a', 'boo'],
202202
previous: ['a', 'boo']
203203
}));
@@ -207,7 +207,7 @@ export function main() {
207207
l = [NumberWrapper.NaN];
208208
changes.check(l);
209209
changes.check(l);
210-
expect(changes.toString()).toEqual(arrayChangesAsString({
210+
expect(changes.toString()).toEqual(iterableChangesAsString({
211211
collection: [NumberWrapper.NaN],
212212
previous: [NumberWrapper.NaN]
213213
}));
@@ -219,7 +219,7 @@ export function main() {
219219

220220
ListWrapper.insert(l, 0, 'foo');
221221
changes.check(l);
222-
expect(changes.toString()).toEqual(arrayChangesAsString({
222+
expect(changes.toString()).toEqual(iterableChangesAsString({
223223
collection: ['foo[null->0]', 'NaN[0->1]', 'NaN[1->2]'],
224224
previous: ['NaN[0->1]', 'NaN[1->2]'],
225225
additions: ['foo[null->0]'],
@@ -233,7 +233,7 @@ export function main() {
233233

234234
ListWrapper.removeAt(l, 1);
235235
changes.check(l);
236-
expect(changes.toString()).toEqual(arrayChangesAsString({
236+
expect(changes.toString()).toEqual(iterableChangesAsString({
237237
collection: ['a', 'c[2->1]'],
238238
previous: ['a', 'b[1->null]', 'c[2->1]'],
239239
moves: ['c[2->1]'],
@@ -242,7 +242,7 @@ export function main() {
242242

243243
ListWrapper.insert(l, 1, 'b');
244244
changes.check(l);
245-
expect(changes.toString()).toEqual(arrayChangesAsString({
245+
expect(changes.toString()).toEqual(iterableChangesAsString({
246246
collection: ['a', 'b[null->1]', 'c[1->2]'],
247247
previous: ['a', 'c[1->2]'],
248248
additions: ['b[null->1]'],
@@ -256,7 +256,7 @@ export function main() {
256256

257257
ListWrapper.removeAt(l, 0);
258258
changes.check(l);
259-
expect(changes.toString()).toEqual(arrayChangesAsString({
259+
expect(changes.toString()).toEqual(iterableChangesAsString({
260260
collection: ['a', 'a', 'b[3->2]', 'b[4->3]'],
261261
previous: ['a', 'a', 'a[2->null]', 'b[3->2]', 'b[4->3]'],
262262
moves: ['b[3->2]', 'b[4->3]'],
@@ -270,7 +270,7 @@ export function main() {
270270

271271
ListWrapper.insert(l, 0, 'b');
272272
changes.check(l);
273-
expect(changes.toString()).toEqual(arrayChangesAsString({
273+
expect(changes.toString()).toEqual(iterableChangesAsString({
274274
collection: ['b[2->0]', 'a[0->1]', 'a[1->2]', 'b', 'b[null->4]'],
275275
previous: ['a[0->1]', 'a[1->2]', 'b[2->0]', 'b'],
276276
additions: ['b[null->4]'],
@@ -287,7 +287,7 @@ export function main() {
287287
ListWrapper.push(l, 'a');
288288
ListWrapper.push(l, 'c');
289289
changes.check(l);
290-
expect(changes.toString()).toEqual(arrayChangesAsString({
290+
expect(changes.toString()).toEqual(iterableChangesAsString({
291291
collection: ['b[1->0]', 'a[0->1]', 'c'],
292292
previous: ['a[0->1]', 'b[1->0]', 'c'],
293293
moves: ['b[1->0]', 'a[0->1]']

modules/angular2/test/change_detection/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {isBlank} from 'angular2/src/facade/lang';
22

3-
export function arrayChangesAsString({collection, previous, additions, moves, removals}) {
3+
export function iterableChangesAsString({collection, previous, additions, moves, removals}) {
44
if (isBlank(collection)) collection = [];
55
if (isBlank(previous)) previous = [];
66
if (isBlank(additions)) additions = [];

0 commit comments

Comments
 (0)