Skip to content

Commit b3ead31

Browse files
committed
Merge branch 'no-constructor-returns' of github.com:alexvictoor/immutable-js into alexvictoor-no-constructor-returns
2 parents bdc0634 + 8dc0c43 commit b3ead31

File tree

16 files changed

+343
-352
lines changed

16 files changed

+343
-352
lines changed

__tests__/List.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('List', () => {
111111
expect(out.getIn(['c', 0])).toEqual('v');
112112
expect(out.get('a')).toBeInstanceOf(Array);
113113
expect(out.get('b')).toBeInstanceOf(Array);
114-
expect(out.get('c')).toBeInstanceOf(Map);
114+
expect(Map.isMap(out.get('c'))).toBe(true);
115115
expect(out.get('c')?.keySeq().first()).toBe(0);
116116
});
117117

__tests__/groupBy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ describe('groupBy', () => {
3434
const grouped = col.groupBy((v) => v);
3535

3636
// all groupBy should be instance of Map
37-
expect(grouped).toBeInstanceOf(Map);
37+
expect(Map.isMap(grouped)).toBe(true);
3838

3939
// ordered objects should be instance of OrderedMap
4040
expect(isOrdered(col)).toBe(constructorIsOrdered);
4141
expect(isOrdered(grouped)).toBe(constructorIsOrdered);
4242
if (constructorIsOrdered) {
43-
expect(grouped).toBeInstanceOf(OrderedMap);
43+
expect(OrderedMap.isOrderedMap(grouped)).toBe(true);
4444
} else {
45-
expect(grouped).not.toBeInstanceOf(OrderedMap);
45+
expect(OrderedMap.isOrderedMap(grouped)).toBe(false);
4646
}
4747
}
4848
);

src/Collection.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,24 @@ import { isKeyed } from './predicates/isKeyed';
44
import { isIndexed } from './predicates/isIndexed';
55
import { isAssociative } from './predicates/isAssociative';
66

7-
export class Collection {
8-
constructor(value) {
9-
// eslint-disable-next-line no-constructor-return
10-
return isCollection(value) ? value : Seq(value);
11-
}
12-
}
13-
14-
export class KeyedCollection extends Collection {
15-
constructor(value) {
16-
// eslint-disable-next-line no-constructor-return
17-
return isKeyed(value) ? value : KeyedSeq(value);
18-
}
19-
}
20-
21-
export class IndexedCollection extends Collection {
22-
constructor(value) {
23-
// eslint-disable-next-line no-constructor-return
24-
return isIndexed(value) ? value : IndexedSeq(value);
25-
}
26-
}
27-
28-
export class SetCollection extends Collection {
29-
constructor(value) {
30-
// eslint-disable-next-line no-constructor-return
31-
return isCollection(value) && !isAssociative(value) ? value : SetSeq(value);
32-
}
33-
}
34-
35-
Collection.Keyed = KeyedCollection;
36-
Collection.Indexed = IndexedCollection;
37-
Collection.Set = SetCollection;
7+
export const Collection = (value) => (isCollection(value) ? value : Seq(value));
8+
export class CollectionImpl {}
9+
10+
export const KeyedCollection = (value) =>
11+
isKeyed(value) ? value : KeyedSeq(value);
12+
13+
export class KeyedCollectionImpl extends CollectionImpl {}
14+
15+
export const IndexedCollection = (value) =>
16+
isIndexed(value) ? value : IndexedSeq(value);
17+
18+
export class IndexedCollectionImpl extends CollectionImpl {}
19+
20+
export const SetCollection = (value) =>
21+
isCollection(value) && !isAssociative(value) ? value : SetSeq(value);
22+
23+
export class SetCollectionImpl extends CollectionImpl {}
24+
25+
Collection.Keyed = KeyedCollectionImpl;
26+
Collection.Indexed = IndexedCollectionImpl;
27+
Collection.Set = SetCollectionImpl;

src/CollectionImpl.js

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,86 @@
11
import {
22
Collection,
3-
KeyedCollection,
4-
IndexedCollection,
5-
SetCollection,
3+
CollectionImpl,
4+
IndexedCollectionImpl,
5+
KeyedCollectionImpl,
6+
SetCollectionImpl,
67
} from './Collection';
7-
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
8-
import { isKeyed, IS_KEYED_SYMBOL } from './predicates/isKeyed';
9-
import { isIndexed, IS_INDEXED_SYMBOL } from './predicates/isIndexed';
10-
import { isOrdered, IS_ORDERED_SYMBOL } from './predicates/isOrdered';
11-
import { is } from './is';
12-
import {
13-
NOT_SET,
14-
ensureSize,
15-
wrapIndex,
16-
returnTrue,
17-
resolveBegin,
18-
} from './TrieUtils';
198
import { hash } from './Hash';
20-
import { imul, smi } from './Math';
9+
import { is } from './is';
2110
import {
22-
Iterator,
23-
ITERATOR_SYMBOL,
11+
ITERATE_ENTRIES,
2412
ITERATE_KEYS,
2513
ITERATE_VALUES,
26-
ITERATE_ENTRIES,
14+
Iterator,
15+
ITERATOR_SYMBOL,
2716
} from './Iterator';
17+
import { imul, smi } from './Math';
18+
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
19+
import { IS_INDEXED_SYMBOL, isIndexed } from './predicates/isIndexed';
20+
import { IS_KEYED_SYMBOL, isKeyed } from './predicates/isKeyed';
21+
import { IS_ORDERED_SYMBOL, isOrdered } from './predicates/isOrdered';
22+
import {
23+
ensureSize,
24+
NOT_SET,
25+
resolveBegin,
26+
returnTrue,
27+
wrapIndex,
28+
} from './TrieUtils';
2829

2930
import arrCopy from './utils/arrCopy';
3031
import assertNotInfinite from './utils/assertNotInfinite';
3132
import deepEqual from './utils/deepEqual';
3233
import mixin from './utils/mixin';
3334
import quoteString from './utils/quoteString';
3435

35-
import { toJS } from './toJS';
36-
import { Map } from './Map';
37-
import { OrderedMap } from './OrderedMap';
3836
import { List } from './List';
39-
import { Set } from './Set';
40-
import { OrderedSet } from './OrderedSet';
41-
import { Stack } from './Stack';
42-
import { Range } from './Range';
43-
import { KeyedSeq, IndexedSeq, SetSeq, ArraySeq } from './Seq';
37+
import { Map } from './Map';
38+
import { getIn } from './methods/getIn';
39+
import { hasIn } from './methods/hasIn';
40+
import { toObject } from './methods/toObject';
4441
import {
45-
reify,
46-
ToKeyedSequence,
47-
ToIndexedSequence,
48-
ToSetSequence,
49-
FromEntriesSequence,
42+
concatFactory,
43+
countByFactory,
44+
filterFactory,
45+
flatMapFactory,
46+
flattenFactory,
5047
flipFactory,
48+
FromEntriesSequence,
49+
groupByFactory,
50+
interposeFactory,
5151
mapFactory,
52+
maxFactory,
53+
partitionFactory,
54+
reify,
5255
reverseFactory,
53-
filterFactory,
54-
countByFactory,
55-
groupByFactory,
56-
sliceFactory,
57-
takeWhileFactory,
5856
skipWhileFactory,
59-
concatFactory,
60-
flattenFactory,
61-
flatMapFactory,
62-
interposeFactory,
57+
sliceFactory,
6358
sortFactory,
64-
maxFactory,
59+
takeWhileFactory,
60+
ToIndexedSequence,
61+
ToKeyedSequence,
62+
ToSetSequence,
6563
zipWithFactory,
66-
partitionFactory,
6764
} from './Operations';
68-
import { getIn } from './methods/getIn';
69-
import { hasIn } from './methods/hasIn';
70-
import { toObject } from './methods/toObject';
65+
import { OrderedMap } from './OrderedMap';
66+
import { OrderedSet } from './OrderedSet';
67+
import { Range } from './Range';
68+
import {
69+
ArraySeq,
70+
IndexedSeq,
71+
IndexedSeqImpl,
72+
KeyedSeqImpl,
73+
SetSeqImpl,
74+
} from './Seq';
75+
import { Set } from './Set';
76+
import { Stack } from './Stack';
77+
import { toJS } from './toJS';
7178

7279
export { Collection, CollectionPrototype, IndexedCollectionPrototype };
7380

7481
Collection.Iterator = Iterator;
7582

76-
mixin(Collection, {
83+
mixin(CollectionImpl, {
7784
// ### Conversion to other types
7885

7986
toArray() {
@@ -490,7 +497,7 @@ mixin(Collection, {
490497
// abstract __iterator(type, reverse)
491498
});
492499

493-
const CollectionPrototype = Collection.prototype;
500+
const CollectionPrototype = CollectionImpl.prototype;
494501
CollectionPrototype[IS_COLLECTION_SYMBOL] = true;
495502
CollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.values;
496503
CollectionPrototype.toJSON = CollectionPrototype.toArray;
@@ -501,7 +508,7 @@ CollectionPrototype.inspect = CollectionPrototype.toSource = function () {
501508
CollectionPrototype.chain = CollectionPrototype.flatMap;
502509
CollectionPrototype.contains = CollectionPrototype.includes;
503510

504-
mixin(KeyedCollection, {
511+
mixin(KeyedCollectionImpl, {
505512
// ### More sequential methods
506513

507514
flip() {
@@ -529,14 +536,14 @@ mixin(KeyedCollection, {
529536
},
530537
});
531538

532-
const KeyedCollectionPrototype = KeyedCollection.prototype;
539+
const KeyedCollectionPrototype = KeyedCollectionImpl.prototype;
533540
KeyedCollectionPrototype[IS_KEYED_SYMBOL] = true;
534541
KeyedCollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.entries;
535542
KeyedCollectionPrototype.toJSON = toObject;
536543
KeyedCollectionPrototype.__toStringMapper = (v, k) =>
537544
quoteString(k) + ': ' + quoteString(v);
538545

539-
mixin(IndexedCollection, {
546+
mixin(IndexedCollectionImpl, {
540547
// ### Conversion to other types
541548

542549
toKeyedSeq() {
@@ -668,11 +675,11 @@ mixin(IndexedCollection, {
668675
},
669676
});
670677

671-
const IndexedCollectionPrototype = IndexedCollection.prototype;
678+
const IndexedCollectionPrototype = IndexedCollectionImpl.prototype;
672679
IndexedCollectionPrototype[IS_INDEXED_SYMBOL] = true;
673680
IndexedCollectionPrototype[IS_ORDERED_SYMBOL] = true;
674681

675-
mixin(SetCollection, {
682+
mixin(SetCollectionImpl, {
676683
// ### ES6 Collection methods (ES6 Array and Map)
677684

678685
get(value, notSetValue) {
@@ -690,16 +697,16 @@ mixin(SetCollection, {
690697
},
691698
});
692699

693-
const SetCollectionPrototype = SetCollection.prototype;
700+
const SetCollectionPrototype = SetCollectionImpl.prototype;
694701
SetCollectionPrototype.has = CollectionPrototype.includes;
695702
SetCollectionPrototype.contains = SetCollectionPrototype.includes;
696703
SetCollectionPrototype.keys = SetCollectionPrototype.values;
697704

698705
// Mixin subclasses
699706

700-
mixin(KeyedSeq, KeyedCollectionPrototype);
701-
mixin(IndexedSeq, IndexedCollectionPrototype);
702-
mixin(SetSeq, SetCollectionPrototype);
707+
mixin(KeyedSeqImpl, KeyedCollectionPrototype);
708+
mixin(IndexedSeqImpl, IndexedCollectionPrototype);
709+
mixin(SetSeqImpl, SetCollectionPrototype);
703710

704711
// #pragma Helper functions
705712

src/List.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
resolveEnd,
1313
} from './TrieUtils';
1414
import { IS_LIST_SYMBOL, isList } from './predicates/isList';
15-
import { IndexedCollection } from './Collection';
15+
import { IndexedCollectionImpl, IndexedCollection } from './Collection';
1616
import { hasIterator, Iterator, iteratorValue, iteratorDone } from './Iterator';
1717
import { setIn } from './methods/setIn';
1818
import { deleteIn } from './methods/deleteIn';
@@ -26,39 +26,38 @@ import { asImmutable } from './methods/asImmutable';
2626
import { wasAltered } from './methods/wasAltered';
2727
import assertNotInfinite from './utils/assertNotInfinite';
2828

29-
export class List extends IndexedCollection {
30-
// @pragma Construction
31-
32-
constructor(value) {
33-
const empty = emptyList();
34-
if (value === undefined || value === null) {
35-
// eslint-disable-next-line no-constructor-return
36-
return empty;
37-
}
38-
if (isList(value)) {
39-
// eslint-disable-next-line no-constructor-return
40-
return value;
41-
}
42-
const iter = IndexedCollection(value);
43-
const size = iter.size;
44-
if (size === 0) {
45-
// eslint-disable-next-line no-constructor-return
46-
return empty;
47-
}
48-
assertNotInfinite(size);
49-
if (size > 0 && size < SIZE) {
50-
// eslint-disable-next-line no-constructor-return
51-
return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
52-
}
53-
// eslint-disable-next-line no-constructor-return
54-
return empty.withMutations((list) => {
55-
list.setSize(size);
56-
iter.forEach((v, i) => list.set(i, v));
57-
});
29+
export const List = (value) => {
30+
const empty = emptyList();
31+
if (value === undefined || value === null) {
32+
return empty;
33+
}
34+
if (isList(value)) {
35+
return value;
5836
}
37+
const iter = IndexedCollection(value);
38+
const size = iter.size;
39+
if (size === 0) {
40+
return empty;
41+
}
42+
assertNotInfinite(size);
43+
if (size > 0 && size < SIZE) {
44+
return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
45+
}
46+
return empty.withMutations((list) => {
47+
list.setSize(size);
48+
iter.forEach((v, i) => list.set(i, v));
49+
});
50+
};
51+
52+
List.of = function (/*...values*/) {
53+
return List(arguments);
54+
};
55+
56+
export class ListImpl extends IndexedCollectionImpl {
57+
// @pragma Construction
5958

60-
static of(/*...values*/) {
61-
return this(arguments);
59+
create(value) {
60+
return List(value);
6261
}
6362

6463
toString() {
@@ -159,7 +158,7 @@ export class List extends IndexedCollection {
159158
return this;
160159
}
161160
if (this.size === 0 && !this.__ownerID && seqs.length === 1) {
162-
return this.constructor(seqs[0]);
161+
return List(seqs[0]);
163162
}
164163
return this.withMutations((list) => {
165164
seqs.forEach((seq) => seq.forEach((value) => list.push(value)));
@@ -241,7 +240,7 @@ export class List extends IndexedCollection {
241240

242241
List.isList = isList;
243242

244-
const ListPrototype = List.prototype;
243+
const ListPrototype = ListImpl.prototype;
245244
ListPrototype[IS_LIST_SYMBOL] = true;
246245
ListPrototype[DELETE] = ListPrototype.remove;
247246
ListPrototype.merge = ListPrototype.concat;

0 commit comments

Comments
 (0)