Skip to content

BREAKING: KeyedCollection.toArray() returns array of tuples. #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/ObjectSeq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('ObjectSequence', () => {
it('is reversable', () => {
let i = Seq({a: 'A', b: 'B', c: 'C'});
let k = i.reverse().toArray();
expect(k).toEqual(['C', 'B', 'A']);
expect(k).toEqual([['c', 'C'], ['b', 'B'], ['a', 'A']]);
});

it('can double reversable', () => {
it('is double reversable', () => {
let i = Seq({a: 'A', b: 'B', c: 'C'});
let k = i.reverse().reverse().toArray();
expect(k).toEqual(['A', 'B', 'C']);
expect(k).toEqual([['a', 'A'], ['b', 'B'], ['c', 'C']]);
});

it('can be iterated', () => {
Expand Down
12 changes: 6 additions & 6 deletions __tests__/OrderedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('OrderedMap', () => {
expect(m.get('a')).toBe('A');
expect(m.get('b')).toBe('B');
expect(m.get('c')).toBe('C');
expect(m.toArray()).toEqual(['C', 'B', 'A']);
expect(m.toArray()).toEqual([['c', 'C'], ['b', 'B'], ['a', 'A']]);
});

it('constructor provides initial values', () => {
Expand All @@ -25,7 +25,7 @@ describe('OrderedMap', () => {
expect(m.get('b')).toBe('B');
expect(m.get('c')).toBe('C');
expect(m.size).toBe(3);
expect(m.toArray()).toEqual(['A', 'B', 'C']);
expect(m.toArray()).toEqual([['a', 'A'], ['b', 'B'], ['c', 'C']]);
});

it('provides initial values in a mixed order', () => {
Expand All @@ -34,7 +34,7 @@ describe('OrderedMap', () => {
expect(m.get('b')).toBe('B');
expect(m.get('c')).toBe('C');
expect(m.size).toBe(3);
expect(m.toArray()).toEqual(['C', 'B', 'A']);
expect(m.toArray()).toEqual([['c', 'C'], ['b', 'B'], ['a', 'A']]);
});

it('constructor accepts sequences', () => {
Expand All @@ -44,7 +44,7 @@ describe('OrderedMap', () => {
expect(m.get('b')).toBe('B');
expect(m.get('c')).toBe('C');
expect(m.size).toBe(3);
expect(m.toArray()).toEqual(['C', 'B', 'A']);
expect(m.toArray()).toEqual([['c', 'C'], ['b', 'B'], ['a', 'A']]);
});

it('maintains order when new keys are set', () => {
Expand All @@ -53,7 +53,7 @@ describe('OrderedMap', () => {
.set('Z', 'zebra')
.set('A', 'antelope');
expect(m.size).toBe(2);
expect(m.toArray()).toEqual(['antelope', 'zebra']);
expect(m.toArray()).toEqual([['A', 'antelope'], ['Z', 'zebra']]);
});

it('resets order when a keys is deleted', () => {
Expand All @@ -63,7 +63,7 @@ describe('OrderedMap', () => {
.remove('A')
.set('A', 'antelope');
expect(m.size).toBe(2);
expect(m.toArray()).toEqual(['zebra', 'antelope']);
expect(m.toArray()).toEqual([['Z', 'zebra'], ['A', 'antelope']]);
});

it('removes correctly', () => {
Expand Down
7 changes: 5 additions & 2 deletions __tests__/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ describe('concat', () => {
it('iterates repeated keys', () => {
let a = Seq({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).valueSeq().toArray()).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3]);
expect(a.concat(a, a).keySeq().toArray()).toEqual(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']);
expect(a.concat(a, a).toArray()).toEqual(
[['a', 1], ['b', 2], ['c', 3], ['a', 1], ['b', 2], ['c', 3], ['a', 1], ['b', 2], ['c', 3]],
);
});

it('lazily reverses un-indexed sequences', () => {
Expand All @@ -145,7 +148,7 @@ describe('concat', () => {
let a = Seq([1, 2, 3]).filter(x => true);
expect(a.size).toBe(undefined); // Note: lazy filter does not know what size in O(1).
expect(a.concat(a, a).toKeyedSeq().reverse().size).toBe(undefined);
expect(a.concat(a, a).toKeyedSeq().reverse().entrySeq().toArray()).toEqual(
expect(a.concat(a, a).toKeyedSeq().reverse().toArray()).toEqual(
[[8, 3], [7, 2], [6, 1], [5, 3], [4, 2], [3, 1], [2, 3], [1, 2], [0, 1]],
);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('groupBy', () => {

// Each group should be a keyed sequence, not an indexed sequence
const firstGroup = grouped.get(1);
expect(firstGroup && firstGroup.toArray()).toEqual([1, 3]);
expect(firstGroup && firstGroup.toArray()).toEqual([['a', 1], ['c', 3]]);
});

it('groups indexed sequence', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/CollectionImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ mixin(Collection, {
toArray() {
assertNotInfinite(this.size);
const array = new Array(this.size || 0);
this.valueSeq().__iterate((v, i) => {
array[i] = v;
const useTuples = isKeyed(this);
let i = 0;
this.__iterate((v, k) => {
// Keyed collections produce an array of tuples.
array[i++] = useTuples ? [k, v] : v;
});
return array;
},
Expand Down
1 change: 1 addition & 0 deletions src/Operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ export function sortFactory(collection, comparator, mapper) {
const entries = collection
.toSeq()
.map((v, k) => [k, v, index++, mapper ? mapper(v, k, collection) : v])
.valueSeq()
.toArray();
entries.sort((a, b) => comparator(a[3], b[3]) || a[2] - b[2]).forEach(
isKeyedCollection
Expand Down
37 changes: 35 additions & 2 deletions type-definitions/Immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,11 @@ declare module Immutable {
*/
toJSON(): { [key: string]: V };

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<[K, V]>;

/**
* Returns itself
*/
Expand Down Expand Up @@ -2772,6 +2777,11 @@ declare module Immutable {
*/
toJSON(): Array<T>;

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<T>;

/**
* Returns itself
*/
Expand Down Expand Up @@ -2916,6 +2926,11 @@ declare module Immutable {
*/
toJSON(): Array<T>;

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<T>;

/**
* Returns itself
*/
Expand Down Expand Up @@ -3182,6 +3197,11 @@ declare module Immutable {
*/
toJSON(): { [key: string]: V };

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<[K, V]>;

/**
* Returns Seq.Keyed.
* @override
Expand Down Expand Up @@ -3330,6 +3350,11 @@ declare module Immutable {
*/
toJSON(): Array<T>;

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<T>;

// Reading values

/**
Expand Down Expand Up @@ -3605,6 +3630,11 @@ declare module Immutable {
*/
toJSON(): Array<T>;

/**
* Shallowly converts this collection to an Array.
*/
toArray(): Array<T>;

/**
* Returns Seq.Set.
* @override
Expand Down Expand Up @@ -3822,9 +3852,12 @@ declare module Immutable {
toJSON(): Array<V> | { [key: string]: V };

/**
* Shallowly converts this collection to an Array, discarding keys.
* Shallowly converts this collection to an Array.
*
* `Collection.Indexed`, and `Collection.Set` produce an Array of values.
* `Collection.Keyed` produce an Array of [key, value] tuples.
*/
toArray(): Array<V>;
toArray(): Array<V> | Array<[K, V]>;

/**
* Shallowly converts this Collection to an Object.
Expand Down
5 changes: 4 additions & 1 deletion type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ declare class _Collection<K, +V> /*implements ValueObject*/ {

toJS(): Array<any> | { [key: string]: mixed };
toJSON(): Array<V> | { [key: string]: V };
toArray(): Array<V>;
toArray(): Array<V> | Array<[K,V]>;
toObject(): { [key: string]: V };
toMap(): Map<K, V>;
toOrderedMap(): OrderedMap<K, V>;
Expand Down Expand Up @@ -209,6 +209,7 @@ declare class KeyedCollection<K, +V> extends Collection<K, V> {

toJS(): { [key: string]: mixed };
toJSON(): { [key: string]: V };
toArray(): Array<[K, V]>;
@@iterator(): Iterator<[K, V]>;
toSeq(): KeyedSeq<K, V>;
flip(): KeyedCollection<V, K>;
Expand Down Expand Up @@ -247,6 +248,7 @@ declare class IndexedCollection<+T> extends Collection<number, T> {

toJS(): Array<mixed>;
toJSON(): Array<T>;
toArray(): Array<T>;
@@iterator(): Iterator<T>;
toSeq(): IndexedSeq<T>;
fromEntrySeq<K, V>(): KeyedSeq<K, V>;
Expand Down Expand Up @@ -388,6 +390,7 @@ declare class SetCollection<+T> extends Collection<T, T> {

toJS(): Array<mixed>;
toJSON(): Array<T>;
toArray(): Array<T>;
@@iterator(): Iterator<T>;
toSeq(): SetSeq<T>;

Expand Down