Skip to content

Commit

Permalink
Add keys/values/entries iterators to concrete collections.
Browse files Browse the repository at this point in the history
This removes `iterator` and adds `keys`, `values`, and `entries` in it's place. ES6 default iterator are still available and map to the appropriate method per type.

Vector's iterators take an optional `sparse` which will cause them to skip unset indices.

This is a **breaking change** which renames the existing `keys`, `values`, `entries` and `fromEntries` methods to `keySeq`, `valueSeq`, `entrySeq` and `fromEntrySeq` to make room for the new behavior.
  • Loading branch information
leebyron committed Aug 25, 2014
1 parent dc79619 commit 0164a55
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 151 deletions.
20 changes: 10 additions & 10 deletions __tests__/ArraySequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,41 @@ describe('ArraySequence', () => {

expect(i.slice(6, null, /*maintainIndices*/true).reverse().reverse().toArray()).toEqual([,,,,,,3,,4,,5,,,,]);

expect(i.slice(6, null, /*maintainIndices*/true).reverse().reverse(true).entries().toArray()).toEqual(
expect(i.slice(6, null, /*maintainIndices*/true).reverse().reverse(true).entrySeq().toArray()).toEqual(
[[6,3], [4,4], [2,5]]
);

expect(i.slice(6, null, /*maintainIndices*/true).reverse(true).reverse().entries().toArray()).toEqual(
expect(i.slice(6, null, /*maintainIndices*/true).reverse(true).reverse().entrySeq().toArray()).toEqual(
[[6,3], [4,4], [2,5]]
);

expect(i.reverse(true).slice(6, null, /*maintainIndices*/true).reverse().entries().toArray()).toEqual(
expect(i.reverse(true).slice(6, null, /*maintainIndices*/true).reverse().entrySeq().toArray()).toEqual(
[[10,1], [8,2], [6,3]]
);

expect(i.reverse(true).slice(6, null, /*maintainIndices*/true).reverse(true).entries().toArray()).toEqual(
expect(i.reverse(true).slice(6, null, /*maintainIndices*/true).reverse(true).entrySeq().toArray()).toEqual(
[[2,1], [4,2], [6,3]]
);

var ii = i;
ii = ii.reverse();
expect(ii.toArray()).toEqual([,,5,,4,,3,,2,,1,,,,]);
expect(ii.entries().toArray()).toEqual([[2,5],[4,4],[6,3],[8,2],[10,1]]);
expect(ii.entrySeq().toArray()).toEqual([[2,5],[4,4],[6,3],[8,2],[10,1]]);
ii = ii.reverse(true);
expect(ii.toArray()).toEqual([,,5,,4,,3,,2,,1,,,,]);
expect(ii.entries().toArray()).toEqual([[10,1],[8,2],[6,3],[4,4],[2,5]]);
expect(ii.entrySeq().toArray()).toEqual([[10,1],[8,2],[6,3],[4,4],[2,5]]);
ii = ii.slice(6, null, true);
expect(ii.toArray()).toEqual([,,5,,4,,3,,,,,,,,]);
expect(ii.entries().toArray()).toEqual([[6,3],[4,4],[2,5]]);
expect(ii.entrySeq().toArray()).toEqual([[6,3],[4,4],[2,5]]);
ii = ii.reverse();
expect(ii.toArray()).toEqual([,,,,,,3,,4,,5,,,,]);
expect(ii.entries().toArray()).toEqual([[10,5],[8,4],[6,3]]);
expect(ii.entrySeq().toArray()).toEqual([[10,5],[8,4],[6,3]]);
ii = ii.reverse(true);
expect(ii.toArray()).toEqual([,,,,,,3,,4,,5,,,,]);
expect(ii.entries().toArray()).toEqual([[6,3],[8,4],[10,5]]);
expect(ii.entrySeq().toArray()).toEqual([[6,3],[8,4],[10,5]]);


expect(i.reverse().reverse(true).slice(6, null, /*maintainIndices*/true).reverse().reverse(true).entries().toArray()).toEqual(
expect(i.reverse().reverse(true).slice(6, null, /*maintainIndices*/true).reverse().reverse(true).entrySeq().toArray()).toEqual(
[[6,3],[8,4],[10,5]]
);

Expand Down
4 changes: 2 additions & 2 deletions __tests__/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Map', () => {

it('derives keys', () => {
var v = Map({a:1, b:2, c:3, d:4, e:5, f:6});
expect(v.keys().toArray()).toEqual(['a', 'b', 'c', 'd', 'e', 'f']);
expect(v.keySeq().toArray()).toEqual(['a', 'b', 'c', 'd', 'e', 'f']);
});

it('flips keys and values', () => {
Expand All @@ -187,7 +187,7 @@ describe('Map', () => {
it('can convert to a vector', () => {
var m = Map({a:1, b:2, c:3});
var v = m.toVector();
var k = m.keys().toVector();
var k = m.keySeq().toVector();
expect(v.length).toBe(3);
expect(k.length).toBe(3);
// Note: Map has undefined ordering, this Vector may not be the same
Expand Down
2 changes: 1 addition & 1 deletion __tests__/ObjectSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ObjectSequence', () => {

it('extracts keys', () => {
var i = Immutable.Sequence({'a': 'A', 'b': 'B', 'c': 'C'});
var k = i.keys().toArray();
var k = i.keySeq().toArray();
expect(k).toEqual(['a', 'b', 'c']);
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/OrderedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ describe('OrderedMap', () => {
it('respects order when merging', () => {
var m1 = OrderedMap({A: 'apple', B: 'banana', C: 'coconut'});
var m2 = OrderedMap({C: 'chocolate', B: 'butter', D: 'donut'});
expect(m1.merge(m2).entries().toArray()).toEqual(
expect(m1.merge(m2).entrySeq().toArray()).toEqual(
[['A','apple'],['B','butter'],['C','chocolate'],['D','donut']]
);
expect(m2.merge(m1).entries().toArray()).toEqual(
expect(m2.merge(m1).entrySeq().toArray()).toEqual(
[['C','coconut'],['B','banana'],['D','donut'],['A','apple']]
);
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ describe('Vector', () => {

check.it('iterates through all values', [gen.posInt], len => {
var v = Immutable.Range(0, len).toVector();
var iterator = v.iterator();
var valueIter = v.values();
for (var ii = 0; ii < len; ii++) {
expect(iterator.next().value).toBe(ii);
expect(valueIter.next().value).toBe(ii);
}
});

Expand Down
6 changes: 3 additions & 3 deletions __tests__/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ describe('concat', () => {
var a = Sequence({a:1,b:2,c:3});
expect(a.concat(a, a).toObject()).toEqual({a:1,b:2,c:3});
expect(a.concat(a, a).toArray()).toEqual([1,2,3,1,2,3,1,2,3]);
expect(a.concat(a, a).keys().toArray()).toEqual(['a','b','c','a','b','c','a','b','c']);
expect(a.concat(a, a).keySeq().toArray()).toEqual(['a','b','c','a','b','c','a','b','c']);
})

it('lazily reverses un-indexed sequences', () => {
var a = Sequence({a:1,b:2,c:3});
var b = Sequence({d:4,e:5,f:6});
expect(a.concat(b).reverse().keys().toArray()).toEqual(['f','e','d','c','b','a']);
expect(a.concat(b).reverse().keySeq().toArray()).toEqual(['f','e','d','c','b','a']);
})

it('lazily reverses indexed sequences', () => {
Expand All @@ -94,7 +94,7 @@ describe('concat', () => {
it('lazily reverses indexed sequences with unknown length, maintaining indicies', () => {
var a = Sequence([1,2,3]).filter(x=>true);
expect(a.concat(a, a).reverse(true).length).toBe(undefined);
expect(a.concat(a, a).reverse(true).entries().toArray()).toEqual(
expect(a.concat(a, a).reverse(true).entrySeq().toArray()).toEqual(
[[8,3],[7,2],[6,1],[5,3],[4,2],[3,1],[2,3],[1,2],[0,1]]
);
})
Expand Down
4 changes: 2 additions & 2 deletions __tests__/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ describe('sort', () => {
})

it('sorts a keyed sequence', () => {
expect(Sequence({z:1,y:2,x:3,c:3,b:2,a:1}).sort().entries().toArray())
expect(Sequence({z:1,y:2,x:3,c:3,b:2,a:1}).sort().entrySeq().toArray())
.toEqual([['z', 1],['a', 1],['y', 2],['b', 2],['x', 3],['c', 3]]);
})

it('sorts an OrderedMap', () => {
expect(OrderedMap({z:1,y:2,x:3,c:3,b:2,a:1}).sort().entries().toArray())
expect(OrderedMap({z:1,y:2,x:3,c:3,b:2,a:1}).sort().entrySeq().toArray())
.toEqual([['z', 1],['a', 1],['y', 2],['b', 2],['x', 3],['c', 3]]);
})

Expand Down
66 changes: 52 additions & 14 deletions dist/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,18 @@ declare module 'immutable' {
* Returns a new indexed sequence of the keys of this sequence,
* discarding values.
*/
keys(): IndexedSequence<K>;
keySeq(): IndexedSequence<K>;

/**
* Returns a new indexed sequence of the keys of this sequence,
* discarding keys.
*/
values(): IndexedSequence<V>;
valueSeq(): IndexedSequence<V>;

/**
* Returns a new indexed sequence of [key, value] tuples.
*/
entries(): IndexedSequence</*(K, V)*/Array<any>>;
entrySeq(): IndexedSequence</*(K, V)*/Array<any>>;

/**
* The `sideEffect` is executed for every entry in the sequence.
Expand Down Expand Up @@ -624,7 +624,7 @@ declare module 'immutable' {
* If this is a sequence of entries (key-value tuples), it will return a
* sequence of those entries.
*/
fromEntries(): Sequence<any, any>;
fromEntrySeq(): Sequence<any, any>;

/**
* Returns the first index at which a given value can be found in the
Expand Down Expand Up @@ -923,6 +923,21 @@ declare module 'immutable' {
*/
clear(): Map<K, V>;

/**
* An iterator of this Map's keys.
*/
keys(): Iterator<K>;

/**
* An iterator of this Map's values.
*/
values(): Iterator<V>;

/**
* An iterator of this Map's entries as [key, value] tuples.
*/
entries(): Iterator</*[K, V]*/Array<any>>;

/**
* When this cursor's (or any of its sub-cursors') `update` method is called,
* the resulting new data structure will be provided to the `onChange`
Expand Down Expand Up @@ -1201,7 +1216,7 @@ declare module 'immutable' {
function from<T>(array: Array<T>): Set<T>;

/**
* `Set.fromKeys()` creates a new immutable Set containing the keys from
* `Set.fromkeySeq()` creates a new immutable Set containing the keys from
* this Sequence or JavaScript Object.
*/
function fromKeys<T>(sequence: Sequence<T, any>): Set<T>;
Expand Down Expand Up @@ -1236,6 +1251,21 @@ declare module 'immutable' {
*/
clear(): Set<T>;

/**
* An iterator of this Set's values (Sets do not have keys).
*/
keys(): Iterator<T>;

/**
* An iterator of this Set's values.
*/
values(): Iterator<T>;

/**
* An iterator of this Sets's entries as [value, value] tuples.
*/
entries(): Iterator</*[T, T]*/Array<T>>;

/**
* Alias for `union`.
* @see `Map.prototype.merge`
Expand Down Expand Up @@ -1353,6 +1383,23 @@ declare module 'immutable' {
*/
clear(): Vector<T>;

/**
* An iterator of this Vector's keys.
*/
keys(sparse?: boolean): Iterator<number>;

/**
* An iterator of this Vector's values.
*/
values(sparse?: boolean): Iterator<T>;

/**
* An iterator of this Vector's entries as [key, value] tuples.
*
* `sparse` defaults to true for entries.
*/
entries(sparse?: boolean): Iterator</*[number, T]*/Array<any>>;

/**
* Returns a new Vector with the provided `values` appended, starting at this
* Vector's `length`.
Expand Down Expand Up @@ -1484,15 +1531,6 @@ declare module 'immutable' {
* @see `Map.prototype.asImmutable`
*/
asImmutable(): Vector<T>;

/**
* Allows `Vector` to be used in ES6 for-of expressions, returning an object
* that adheres to the `Iterator` interface: it has a `next()` method which
* returns the next value or (index, value) tuple when `sparse`.
*
* When no entries remain, returns `{ done: true }`
*/
iterator(sparse?: boolean): Iterator<any>
}


Expand Down

0 comments on commit 0164a55

Please sign in to comment.