Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4066019

Browse filesBrowse files
committed
Special case keySeq of an IndexedSeq to return a Range
Fixes immutable-js#811
1 parent a920f9e commit 4066019
Copy full SHA for 4066019

File tree

Expand file treeCollapse file tree

6 files changed

+72
-44
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+72
-44
lines changed

‎__tests__/IndexedSeq.ts

Copy file name to clipboardExpand all lines: __tests__/IndexedSeq.ts
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,21 @@ describe('IndexedSequence', () => {
3636
expect(operated.first()).toEqual('E');
3737
expect(operated.last()).toEqual('A');
3838
});
39+
40+
it('negative indexes correctly', () => {
41+
var seq = Seq(['A', 'B', 'C', 'D', 'E']);
42+
43+
expect(seq.first()).toEqual('A');
44+
expect(seq.last()).toEqual('E');
45+
expect(seq.get(-0)).toEqual('A');
46+
expect(seq.get(2)).toEqual('C');
47+
expect(seq.get(-2)).toEqual('D');
48+
49+
var indexes = seq.keySeq();
50+
expect(indexes.first()).toEqual(0);
51+
expect(indexes.last()).toEqual(4);
52+
expect(indexes.get(-0)).toEqual(0);
53+
expect(indexes.get(2)).toEqual(2);
54+
expect(indexes.get(-2)).toEqual(3);
55+
});
3956
});

‎dist/immutable.d.ts

Copy file name to clipboardExpand all lines: dist/immutable.d.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ declare module Immutable {
23302330
// Search for value
23312331

23322332
/**
2333-
* Returns the value for which the `predicate` returns true.
2333+
* Returns the first value for which the `predicate` returns true.
23342334
*/
23352335
find(
23362336
predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
@@ -2350,7 +2350,7 @@ declare module Immutable {
23502350
): V;
23512351

23522352
/**
2353-
* Returns the [key, value] entry for which the `predicate` returns true.
2353+
* Returns the first [key, value] entry for which the `predicate` returns true.
23542354
*/
23552355
findEntry(
23562356
predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,

‎dist/immutable.js

Copy file name to clipboardExpand all lines: dist/immutable.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,9 @@
10371037
}
10381038
var type = typeof o;
10391039
if (type === 'number') {
1040+
if (o !== o || o === Infinity) {
1041+
return 0;
1042+
}
10401043
var h = o | 0;
10411044
if (h !== o) {
10421045
h ^= o * 0xFFFFFFFF;
@@ -4815,6 +4818,10 @@
48154818
return reify(this, interleaved);
48164819
},
48174820

4821+
keySeq: function() {
4822+
return Range(0, this.size);
4823+
},
4824+
48184825
last: function() {
48194826
return this.get(-1);
48204827
},

‎dist/immutable.js.flow

Copy file name to clipboardExpand all lines: dist/immutable.js.flow
+39-40Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
* Note that Immutable values implement the `ESIterable` interface.
2929
*/
3030
type ESIterable<T> = $Iterable<T,void,void>;
31-
type booleany = any;
3231

33-
declare class Iterable<K,V> {
34-
static Keyed: Class<Object>;
35-
static Indexed: Class<Object>;
36-
static Set: Class<Object>;
32+
declare class Iterable<K, V> extends _Iterable<K, V, typeof KeyedIterable, typeof IndexedIterable, typeof SetIterable> {}
33+
34+
declare class _Iterable<K,V, KI, II, SI> {
35+
static Keyed: KI;
36+
static Indexed: II;
37+
static Set: SI;
3738

3839
static isIterable(maybeIterable: any): boolean;
3940
static isKeyed(maybeKeyed: any): boolean;
@@ -78,12 +79,12 @@ declare class Iterable<K,V> {
7879
entrySeq(): IndexedSeq<[K,V]>;
7980

8081
filter(
81-
predicate: (value: V, key: K, iter: this) => booleany,
82+
predicate: (value: V, key: K, iter: this) => mixed,
8283
context?: any
8384
): this;
8485

8586
filterNot(
86-
predicate: (value: V, key: K, iter: this) => booleany,
87+
predicate: (value: V, key: K, iter: this) => mixed,
8788
context?: any
8889
): this;
8990

@@ -110,12 +111,12 @@ declare class Iterable<K,V> {
110111
butLast(): this;
111112
skip(amount: number): this;
112113
skipLast(amount: number): this;
113-
skipWhile(predicate: (value: V, key: K, iter: this) => booleany, context?: any): this;
114-
skipUntil(predicate: (value: V, key: K, iter: this) => booleany, context?: any): this;
114+
skipWhile(predicate: (value: V, key: K, iter: this) => mixed, context?: any): this;
115+
skipUntil(predicate: (value: V, key: K, iter: this) => mixed, context?: any): this;
115116
take(amount: number): this;
116117
takeLast(amount: number): this;
117-
takeWhile(predicate: (value: V, key: K, iter: this) => booleany, context?: any): this;
118-
takeUntil(predicate: (value: V, key: K, iter: this) => booleany, context?: any): this;
118+
takeWhile(predicate: (value: V, key: K, iter: this) => mixed, context?: any): this;
119+
takeUntil(predicate: (value: V, key: K, iter: this) => mixed, context?: any): this;
119120
flatten(depth?: number): /*this*/Iterable<any,any>;
120121
flatten(shallow?: boolean): /*this*/Iterable<any,any>;
121122

@@ -131,35 +132,35 @@ declare class Iterable<K,V> {
131132
context?: any,
132133
): R;
133134

134-
every(predicate: (value: V, key: K, iter: this) => booleany, context?: any): boolean;
135-
some(predicate: (value: V, key: K, iter: this) => booleany, context?: any): boolean;
135+
every(predicate: (value: V, key: K, iter: this) => mixed, context?: any): boolean;
136+
some(predicate: (value: V, key: K, iter: this) => mixed, context?: any): boolean;
136137
join(separator?: string): string;
137138
isEmpty(): boolean;
138-
count(predicate?: (value: V, key: K, iter: this) => booleany, context?: any): number;
139+
count(predicate?: (value: V, key: K, iter: this) => mixed, context?: any): number;
139140
countBy<G>(grouper: (value: V, key: K, iter: this) => G, context?: any): Map<G,number>;
140141

141142
find<V_>(
142-
predicate: (value: V, key: K, iter: this) => booleany,
143+
predicate: (value: V, key: K, iter: this) => mixed,
143144
context: any,
144145
notSetValue: V_
145146
): V|V_;
146147
find<V_>(
147-
predicate: (value: V, key: K, iter: this) => booleany,
148+
predicate: (value: V, key: K, iter: this) => mixed,
148149
context?: any,
149150
): ?V;
150151

151152
findLast<V_>(
152-
predicate: (value: V, key: K, iter: this) => booleany,
153+
predicate: (value: V, key: K, iter: this) => mixed,
153154
context: any,
154155
notSetValue: V_
155156
): V|V_;
156157
findLast<V_>(
157-
predicate: (value: V, key: K, iter: this) => booleany,
158+
predicate: (value: V, key: K, iter: this) => mixed,
158159
context?: any,
159160
): ?V;
160161

161-
findEntry(predicate: (value: V, key: K, iter: this) => booleany): ?[K,V];
162-
findLastEntry(predicate: (value: V, key: K, iter: this) => booleany): ?[K,V];
162+
findEntry(predicate: (value: V, key: K, iter: this) => mixed): ?[K,V];
163+
findLastEntry(predicate: (value: V, key: K, iter: this) => mixed): ?[K,V];
163164

164165
max(comparator?: (valueA: V, valueB: V) => number): V;
165166
maxBy<C>(
@@ -198,8 +199,8 @@ declare class KeyedIterable<K,V> extends Iterable<K,V> {
198199

199200
keyOf(searchValue: V): ?K;
200201
lastKeyOf(searchValue: V): ?K;
201-
findKey(predicate: (value: V, key: K, iter: this) => booleany, context?: any): ?K;
202-
findLastKey(predicate: (value: V, key: K, iter: this) => booleany, context?: any): ?K;
202+
findKey(predicate: (value: V, key: K, iter: this) => mixed, context?: any): ?K;
203+
findLastKey(predicate: (value: V, key: K, iter: this) => mixed, context?: any): ?K;
203204

204205
concat(...iters: ESIterable<[K,V]>[]): this;
205206

@@ -298,11 +299,11 @@ declare class IndexedIterable<T> extends Iterable<number,T> {
298299
indexOf(searchValue: T): number;
299300
lastIndexOf(searchValue: T): number;
300301
findIndex(
301-
predicate: (value: T, index: number, iter: this) => booleany,
302+
predicate: (value: T, index: number, iter: this) => mixed,
302303
context?: any
303304
): number;
304305
findLastIndex(
305-
predicate: (value: T, index: number, iter: this) => booleany,
306+
predicate: (value: T, index: number, iter: this) => mixed,
306307
context?: any
307308
): number;
308309

@@ -342,11 +343,7 @@ declare class SetIterable<T> extends Iterable<T,T> {
342343
): /*this*/SetIterable<U>;
343344
}
344345

345-
declare class Collection<K,V> extends Iterable<K,V> {
346-
static Keyed: Class<Object>;
347-
static Indexed: Class<Object>;
348-
static Set: Class<Object>;
349-
346+
declare class Collection<K,V> extends _Iterable<K,V, typeof KeyedCollection, typeof IndexedCollection, typeof SetCollection> {
350347
size: number;
351348
}
352349

@@ -362,11 +359,7 @@ declare class SetCollection<T> extends Collection<T,T> mixins SetIterable<T> {
362359
toSeq(): SetSeq<T>;
363360
}
364361

365-
declare class Seq<K,V> extends Iterable<K,V> {
366-
static Keyed: Class<Object>;
367-
static Indexed: Class<Object>;
368-
static Set: Class<Object>;
369-
362+
declare class Seq<K,V> extends _Iterable<K,V, typeof KeyedSeq, typeof IndexedSeq, typeof SetSeq> {
370363
static <K,V>(iter: KeyedSeq<K,V>): KeyedSeq<K,V>;
371364
static <T> (iter: SetSeq<T>): SetSeq<K,V>;
372365
static <T> (iter?: ESIterable<T>): IndexedSeq<T>;
@@ -524,8 +517,9 @@ declare class Map<K,V> extends KeyedCollection<K,V> {
524517
): Map<K_,V>;
525518
}
526519

520+
// OrderedMaps have nothing that Maps do not have. We do not need to override constructor & other statics
527521
declare class OrderedMap<K,V> extends Map<K,V> {
528-
// TODO: can we just use the inherited constructor?
522+
static isOrderedMap(maybeOrderedMap: any): bool;
529523
}
530524

531525
declare class Set<T> extends SetCollection<T> {
@@ -562,7 +556,10 @@ declare class Set<T> extends SetCollection<T> {
562556
): Set<M>;
563557
}
564558

565-
declare class OrderedSet<T> extends Set<T> {}
559+
// OrderedSets have nothing that Sets do not have. We do not need to override constructor & other statics
560+
declare class OrderedSet<T> extends Set<T> {
561+
static isOrderedSet(maybeOrderedSet: any): bool;
562+
}
566563

567564
declare class Stack<T> extends IndexedCollection<T> {
568565
static <T>(iterable?: ESIterable<T>): Stack<T>;
@@ -599,11 +596,13 @@ declare class Stack<T> extends IndexedCollection<T> {
599596
declare function Range(start?: number, end?: number, step?: number): IndexedSeq<number>;
600597
declare function Repeat<T>(value: T, times?: number): IndexedSeq<T>;
601598

602-
declare class Record<T:Object> {
603-
static <T:Object>(spec: T, name?: string): (values: $Shape<T>) => (T & Record<T>);
599+
//TODO: Once flow can extend normal Objects we can change this back to actually reflect Record behavior.
600+
// For now fallback to any to not break existing Code
601+
declare class Record<T: Object> {
602+
static <T: Object>(spec: T, name?: string): /*T & Record<T>*/any;
604603
get<A>(key: $Keys<T>): A;
605-
set<A>(key: $Keys<T>, value: A): T & Record<T>;
606-
remove(key: $Keys<T>): T & Record<T>;
604+
set<A>(key: $Keys<T>, value: A): /*T & Record<T>*/this;
605+
remove(key: $Keys<T>): /*T & Record<T>*/this;
607606
}
608607

609608
declare function fromJS(json: Object, reviver?: (k: any, v: Iterable<any,any>) => any): any;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.