Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13fc231

Browse files
authored
Merge pull request #2128 from immutable-js/ts-migrate-range
2 parents f1f39e2 + f469f1b commit 13fc231

23 files changed

+413
-219
lines changed

src/Collection.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Collection.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { ITERATE_ENTRIES, type IteratorType } from './Iterator';
2+
import { IndexedSeq, KeyedSeq, Seq, SetSeq } from './Seq';
3+
import type ValueObject from './ValueObject';
4+
import { isAssociative } from './predicates/isAssociative';
5+
import { isCollection } from './predicates/isCollection';
6+
import { isIndexed } from './predicates/isIndexed';
7+
import { isKeyed } from './predicates/isKeyed';
8+
import assertNotInfinite from './utils/assertNotInfinite';
9+
import deepEqual from './utils/deepEqual';
10+
import { hashCollection } from './utils/hashCollection';
11+
12+
export function Collection<I extends CollectionImpl<unknown, unknown>>(
13+
collection: I
14+
): I;
15+
export function Collection<T>(
16+
collection: Iterable<T> | ArrayLike<T>
17+
): IndexedCollectionImpl<T>;
18+
export function Collection<V>(obj: {
19+
[key: string]: V;
20+
}): KeyedCollectionImpl<string, V>;
21+
export function Collection<K = unknown, V = unknown>(
22+
value: never
23+
): CollectionImpl<K, V>;
24+
export function Collection(value: unknown): CollectionImpl<unknown, unknown> {
25+
return isCollection(value) ? value : Seq(value);
26+
}
27+
28+
export class CollectionImpl<K, V> implements ValueObject {
29+
private __hash: number | undefined;
30+
31+
size: number = 0;
32+
33+
equals(other: unknown): boolean {
34+
return deepEqual(this, other);
35+
}
36+
37+
hashCode() {
38+
return this.__hash || (this.__hash = hashCollection(this));
39+
}
40+
41+
every(
42+
predicate: (value: V, key: K, iter: this) => boolean,
43+
context?: CollectionImpl<K, V>
44+
): boolean {
45+
assertNotInfinite(this.size);
46+
let returnValue = true;
47+
this.__iterate((v, k, c) => {
48+
if (!predicate.call(context, v, k, c)) {
49+
returnValue = false;
50+
return false;
51+
}
52+
});
53+
return returnValue;
54+
}
55+
56+
entries() {
57+
return this.__iterator(ITERATE_ENTRIES);
58+
}
59+
60+
__iterate(
61+
fn: (value: V, index: K, iter: this) => boolean,
62+
reverse?: boolean
63+
): number;
64+
__iterate(
65+
fn: (value: V, index: K, iter: this) => void,
66+
reverse?: boolean
67+
): void;
68+
__iterate(
69+
fn: (value: V, index: K, iter: this) => boolean | void,
70+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71+
reverse: boolean = false
72+
): number | void {
73+
throw new Error(
74+
'CollectionImpl does not implement __iterate. Use a subclass instead.'
75+
);
76+
}
77+
78+
__iterator(
79+
type: IteratorType,
80+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
81+
reverse: boolean = false
82+
): Iterator<K | V | [K, V]> {
83+
throw new Error(
84+
'CollectionImpl does not implement __iterator. Use a subclass instead.'
85+
);
86+
}
87+
}
88+
89+
/**
90+
* Always returns a Seq.Keyed, if input is not keyed, expects an
91+
* collection of [K, V] tuples.
92+
*
93+
* Note: `Seq.Keyed` is a conversion function and not a class, and does not
94+
* use the `new` keyword during construction.
95+
*/
96+
export function KeyedCollection<K, V>(
97+
collection?: Iterable<[K, V]>
98+
): KeyedCollectionImpl<K, V>;
99+
export function KeyedCollection<V>(obj: {
100+
[key: string]: V;
101+
}): KeyedCollectionImpl<string, V>;
102+
export function KeyedCollection(
103+
value: unknown
104+
): KeyedCollectionImpl<unknown, unknown> {
105+
return isKeyed(value) ? value : KeyedSeq(value);
106+
}
107+
108+
export class KeyedCollectionImpl<K, V> extends CollectionImpl<K, V> {}
109+
110+
export function IndexedCollection<T>(
111+
value: Iterable<T> | ArrayLike<T>
112+
): IndexedCollectionImpl<T> {
113+
return isIndexed(value) ? value : IndexedSeq(value);
114+
}
115+
116+
/**
117+
* Interface representing all oredered collections.
118+
* This includes `List`, `Stack`, `Map`, `OrderedMap`, `Set`, and `OrderedSet`.
119+
* return of `isOrdered()` return true in that case.
120+
*/
121+
interface OrderedCollection<T> {
122+
/**
123+
* Shallowly converts this collection to an Array.
124+
*/
125+
toArray(): Array<T>;
126+
127+
[Symbol.iterator](): IterableIterator<T>;
128+
}
129+
130+
export class IndexedCollectionImpl<T>
131+
extends CollectionImpl<number, T>
132+
implements OrderedCollection<T>
133+
{
134+
declare toArray: () => T[];
135+
136+
declare [Symbol.iterator]: () => IterableIterator<T>;
137+
}
138+
139+
export function SetCollection<T>(
140+
value: Iterable<T> | ArrayLike<T>
141+
): SetCollectionImpl<T> {
142+
return isCollection(value) && !isAssociative(value) ? value : SetSeq(value);
143+
}
144+
145+
export class SetCollectionImpl<T> extends CollectionImpl<T, T> {}
146+
147+
Collection.Keyed = KeyedCollection;
148+
Collection.Indexed = IndexedCollection;
149+
Collection.Set = SetCollection;

src/CollectionImpl.js

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ import {
55
KeyedCollectionImpl,
66
SetCollectionImpl,
77
} from './Collection';
8-
import { hash } from './Hash';
9-
import {
10-
ITERATE_ENTRIES,
11-
ITERATE_KEYS,
12-
ITERATE_VALUES,
13-
Iterator,
14-
} from './Iterator';
8+
import { ITERATE_KEYS, ITERATE_VALUES, Iterator } from './Iterator';
159
import { List } from './List';
1610
import { Map } from './Map';
17-
import { imul, smi } from './Math';
1811
import {
1912
concatFactory,
2013
countByFactory,
@@ -65,10 +58,9 @@ import { toObject } from './methods/toObject';
6558
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
6659
import { IS_INDEXED_SYMBOL, isIndexed } from './predicates/isIndexed';
6760
import { IS_KEYED_SYMBOL, isKeyed } from './predicates/isKeyed';
68-
import { IS_ORDERED_SYMBOL, isOrdered } from './predicates/isOrdered';
61+
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered';
6962
import { toJS } from './toJS';
7063
import assertNotInfinite from './utils/assertNotInfinite';
71-
import deepEqual from './utils/deepEqual';
7264
import mixin from './utils/mixin';
7365
import quoteString from './utils/quoteString';
7466

@@ -176,22 +168,6 @@ mixin(CollectionImpl, {
176168
return this.some((value) => is(value, searchValue));
177169
},
178170

179-
entries() {
180-
return this.__iterator(ITERATE_ENTRIES);
181-
},
182-
183-
every(predicate, context) {
184-
assertNotInfinite(this.size);
185-
let returnValue = true;
186-
this.__iterate((v, k, c) => {
187-
if (!predicate.call(context, v, k, c)) {
188-
returnValue = false;
189-
return false;
190-
}
191-
});
192-
return returnValue;
193-
},
194-
195171
filter(predicate, context) {
196172
return reify(this, filterFactory(this, predicate, context, true));
197173
},
@@ -301,9 +277,9 @@ mixin(CollectionImpl, {
301277
return countByFactory(this, grouper, context);
302278
},
303279

304-
equals(other) {
305-
return deepEqual(this, other);
306-
},
280+
// equals(other) {
281+
// return deepEqual(this, other);
282+
// },
307283

308284
entrySeq() {
309285
// eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -482,9 +458,9 @@ mixin(CollectionImpl, {
482458

483459
// ### Hashable Object
484460

485-
hashCode() {
486-
return this.__hash || (this.__hash = hashCollection(this));
487-
},
461+
// hashCode() {
462+
// return this.__hash || (this.__hash = hashCollection(this));
463+
// },
488464

489465
// ### Internal
490466

@@ -755,47 +731,3 @@ function defaultZipper(...values) {
755731
function defaultNegComparator(a, b) {
756732
return a < b ? 1 : a > b ? -1 : 0;
757733
}
758-
759-
function hashCollection(collection) {
760-
if (collection.size === Infinity) {
761-
return 0;
762-
}
763-
const ordered = isOrdered(collection);
764-
const keyed = isKeyed(collection);
765-
let h = ordered ? 1 : 0;
766-
767-
collection.__iterate(
768-
keyed
769-
? ordered
770-
? (v, k) => {
771-
h = (31 * h + hashMerge(hash(v), hash(k))) | 0;
772-
}
773-
: (v, k) => {
774-
h = (h + hashMerge(hash(v), hash(k))) | 0;
775-
}
776-
: ordered
777-
? (v) => {
778-
h = (31 * h + hash(v)) | 0;
779-
}
780-
: (v) => {
781-
h = (h + hash(v)) | 0;
782-
}
783-
);
784-
785-
return murmurHashOfSize(collection.size, h);
786-
}
787-
788-
function murmurHashOfSize(size, h) {
789-
h = imul(h, 0xcc9e2d51);
790-
h = imul((h << 15) | (h >>> -15), 0x1b873593);
791-
h = imul((h << 13) | (h >>> -13), 5);
792-
h = ((h + 0xe6546b64) | 0) ^ size;
793-
h = imul(h ^ (h >>> 16), 0x85ebca6b);
794-
h = imul(h ^ (h >>> 13), 0xc2b2ae35);
795-
h = smi(h ^ (h >>> 16));
796-
return h;
797-
}
798-
799-
function hashMerge(a, b) {
800-
return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; // int
801-
}

0 commit comments

Comments
 (0)