Skip to content

Avoid return in constructors #2041

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
Feb 22, 2025
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
2 changes: 1 addition & 1 deletion __tests__/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('List', () => {
expect(out.getIn(['c', 0])).toEqual('v');
expect(out.get('a')).toBeInstanceOf(Array);
expect(out.get('b')).toBeInstanceOf(Array);
expect(out.get('c')).toBeInstanceOf(Map);
expect(Map.isMap(out.get('c'))).toBe(true);
expect(out.get('c')?.keySeq().first()).toBe(0);
});

Expand Down
6 changes: 3 additions & 3 deletions __tests__/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('groupBy', () => {
const grouped = col.groupBy(v => v);

// all groupBy should be instance of Map
expect(grouped).toBeInstanceOf(Map);
expect(Map.isMap(grouped)).toBe(true);

// ordered objects should be instance of OrderedMap
expect(isOrdered(col)).toBe(constructorIsOrdered);
expect(isOrdered(grouped)).toBe(constructorIsOrdered);
if (constructorIsOrdered) {
expect(grouped).toBeInstanceOf(OrderedMap);
expect(OrderedMap.isOrderedMap(grouped)).toBe(true);
} else {
expect(grouped).not.toBeInstanceOf(OrderedMap);
expect(OrderedMap.isOrderedMap(grouped)).toBe(false);
}
}
);
Expand Down
52 changes: 21 additions & 31 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@ import { isKeyed } from './predicates/isKeyed';
import { isIndexed } from './predicates/isIndexed';
import { isAssociative } from './predicates/isAssociative';

export class Collection {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return isCollection(value) ? value : Seq(value);
}
}

export class KeyedCollection extends Collection {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return isKeyed(value) ? value : KeyedSeq(value);
}
}

export class IndexedCollection extends Collection {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return isIndexed(value) ? value : IndexedSeq(value);
}
}

export class SetCollection extends Collection {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return isCollection(value) && !isAssociative(value) ? value : SetSeq(value);
}
}

Collection.Keyed = KeyedCollection;
Collection.Indexed = IndexedCollection;
Collection.Set = SetCollection;
export const Collection = value => (isCollection(value) ? value : Seq(value));
export class CollectionImpl {}

export const KeyedCollection = value =>
isKeyed(value) ? value : KeyedSeq(value);

export class KeyedCollectionImpl extends CollectionImpl {}

export const IndexedCollection = value =>
isIndexed(value) ? value : IndexedSeq(value);

export class IndexedCollectionImpl extends CollectionImpl {}

export const SetCollection = value =>
isCollection(value) && !isAssociative(value) ? value : SetSeq(value);

export class SetCollectionImpl extends CollectionImpl {}

Collection.Keyed = KeyedCollectionImpl;
Collection.Indexed = IndexedCollectionImpl;
Collection.Set = SetCollectionImpl;
121 changes: 64 additions & 57 deletions src/CollectionImpl.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,86 @@
import {
Collection,
KeyedCollection,
IndexedCollection,
SetCollection,
CollectionImpl,
IndexedCollectionImpl,
KeyedCollectionImpl,
SetCollectionImpl,
} from './Collection';
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
import { isKeyed, IS_KEYED_SYMBOL } from './predicates/isKeyed';
import { isIndexed, IS_INDEXED_SYMBOL } from './predicates/isIndexed';
import { isOrdered, IS_ORDERED_SYMBOL } from './predicates/isOrdered';
import { is } from './is';
import {
NOT_SET,
ensureSize,
wrapIndex,
returnTrue,
resolveBegin,
} from './TrieUtils';
import { hash } from './Hash';
import { imul, smi } from './Math';
import { is } from './is';
import {
Iterator,
ITERATOR_SYMBOL,
ITERATE_ENTRIES,
ITERATE_KEYS,
ITERATE_VALUES,
ITERATE_ENTRIES,
Iterator,
ITERATOR_SYMBOL,
} from './Iterator';
import { imul, smi } from './Math';
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
import { IS_INDEXED_SYMBOL, isIndexed } from './predicates/isIndexed';
import { IS_KEYED_SYMBOL, isKeyed } from './predicates/isKeyed';
import { IS_ORDERED_SYMBOL, isOrdered } from './predicates/isOrdered';
import {
ensureSize,
NOT_SET,
resolveBegin,
returnTrue,
wrapIndex,
} from './TrieUtils';

import arrCopy from './utils/arrCopy';
import assertNotInfinite from './utils/assertNotInfinite';
import deepEqual from './utils/deepEqual';
import mixin from './utils/mixin';
import quoteString from './utils/quoteString';

import { toJS } from './toJS';
import { Map } from './Map';
import { OrderedMap } from './OrderedMap';
import { List } from './List';
import { Set } from './Set';
import { OrderedSet } from './OrderedSet';
import { Stack } from './Stack';
import { Range } from './Range';
import { KeyedSeq, IndexedSeq, SetSeq, ArraySeq } from './Seq';
import { Map } from './Map';
import { getIn } from './methods/getIn';
import { hasIn } from './methods/hasIn';
import { toObject } from './methods/toObject';
import {
reify,
ToKeyedSequence,
ToIndexedSequence,
ToSetSequence,
FromEntriesSequence,
concatFactory,
countByFactory,
filterFactory,
flatMapFactory,
flattenFactory,
flipFactory,
FromEntriesSequence,
groupByFactory,
interposeFactory,
mapFactory,
maxFactory,
partitionFactory,
reify,
reverseFactory,
filterFactory,
countByFactory,
groupByFactory,
sliceFactory,
takeWhileFactory,
skipWhileFactory,
concatFactory,
flattenFactory,
flatMapFactory,
interposeFactory,
sliceFactory,
sortFactory,
maxFactory,
takeWhileFactory,
ToIndexedSequence,
ToKeyedSequence,
ToSetSequence,
zipWithFactory,
partitionFactory,
} from './Operations';
import { getIn } from './methods/getIn';
import { hasIn } from './methods/hasIn';
import { toObject } from './methods/toObject';
import { OrderedMap } from './OrderedMap';
import { OrderedSet } from './OrderedSet';
import { Range } from './Range';
import {
ArraySeq,
IndexedSeq,
IndexedSeqImpl,
KeyedSeqImpl,
SetSeqImpl,
} from './Seq';
import { Set } from './Set';
import { Stack } from './Stack';
import { toJS } from './toJS';

export { Collection, CollectionPrototype, IndexedCollectionPrototype };

Collection.Iterator = Iterator;

mixin(Collection, {
mixin(CollectionImpl, {
// ### Conversion to other types

toArray() {
Expand Down Expand Up @@ -489,7 +496,7 @@ mixin(Collection, {
// abstract __iterator(type, reverse)
});

const CollectionPrototype = Collection.prototype;
const CollectionPrototype = CollectionImpl.prototype;
CollectionPrototype[IS_COLLECTION_SYMBOL] = true;
CollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.values;
CollectionPrototype.toJSON = CollectionPrototype.toArray;
Expand All @@ -500,7 +507,7 @@ CollectionPrototype.inspect = CollectionPrototype.toSource = function () {
CollectionPrototype.chain = CollectionPrototype.flatMap;
CollectionPrototype.contains = CollectionPrototype.includes;

mixin(KeyedCollection, {
mixin(KeyedCollectionImpl, {
// ### More sequential methods

flip() {
Expand Down Expand Up @@ -528,14 +535,14 @@ mixin(KeyedCollection, {
},
});

const KeyedCollectionPrototype = KeyedCollection.prototype;
const KeyedCollectionPrototype = KeyedCollectionImpl.prototype;
KeyedCollectionPrototype[IS_KEYED_SYMBOL] = true;
KeyedCollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.entries;
KeyedCollectionPrototype.toJSON = toObject;
KeyedCollectionPrototype.__toStringMapper = (v, k) =>
quoteString(k) + ': ' + quoteString(v);

mixin(IndexedCollection, {
mixin(IndexedCollectionImpl, {
// ### Conversion to other types

toKeyedSeq() {
Expand Down Expand Up @@ -667,11 +674,11 @@ mixin(IndexedCollection, {
},
});

const IndexedCollectionPrototype = IndexedCollection.prototype;
const IndexedCollectionPrototype = IndexedCollectionImpl.prototype;
IndexedCollectionPrototype[IS_INDEXED_SYMBOL] = true;
IndexedCollectionPrototype[IS_ORDERED_SYMBOL] = true;

mixin(SetCollection, {
mixin(SetCollectionImpl, {
// ### ES6 Collection methods (ES6 Array and Map)

get(value, notSetValue) {
Expand All @@ -689,16 +696,16 @@ mixin(SetCollection, {
},
});

const SetCollectionPrototype = SetCollection.prototype;
const SetCollectionPrototype = SetCollectionImpl.prototype;
SetCollectionPrototype.has = CollectionPrototype.includes;
SetCollectionPrototype.contains = SetCollectionPrototype.includes;
SetCollectionPrototype.keys = SetCollectionPrototype.values;

// Mixin subclasses

mixin(KeyedSeq, KeyedCollectionPrototype);
mixin(IndexedSeq, IndexedCollectionPrototype);
mixin(SetSeq, SetCollectionPrototype);
mixin(KeyedSeqImpl, KeyedCollectionPrototype);
mixin(IndexedSeqImpl, IndexedCollectionPrototype);
mixin(SetSeqImpl, SetCollectionPrototype);

// #pragma Helper functions

Expand Down
67 changes: 33 additions & 34 deletions src/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
resolveEnd,
} from './TrieUtils';
import { IS_LIST_SYMBOL, isList } from './predicates/isList';
import { IndexedCollection } from './Collection';
import { IndexedCollectionImpl, IndexedCollection } from './Collection';
import { hasIterator, Iterator, iteratorValue, iteratorDone } from './Iterator';
import { setIn } from './methods/setIn';
import { deleteIn } from './methods/deleteIn';
Expand All @@ -26,39 +26,38 @@ import { asImmutable } from './methods/asImmutable';
import { wasAltered } from './methods/wasAltered';
import assertNotInfinite from './utils/assertNotInfinite';

export class List extends IndexedCollection {
// @pragma Construction

constructor(value) {
const empty = emptyList();
if (value === undefined || value === null) {
// eslint-disable-next-line no-constructor-return
return empty;
}
if (isList(value)) {
// eslint-disable-next-line no-constructor-return
return value;
}
const iter = IndexedCollection(value);
const size = iter.size;
if (size === 0) {
// eslint-disable-next-line no-constructor-return
return empty;
}
assertNotInfinite(size);
if (size > 0 && size < SIZE) {
// eslint-disable-next-line no-constructor-return
return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
}
// eslint-disable-next-line no-constructor-return
return empty.withMutations(list => {
list.setSize(size);
iter.forEach((v, i) => list.set(i, v));
});
export const List = value => {
const empty = emptyList();
if (value === undefined || value === null) {
return empty;
}
if (isList(value)) {
return value;
}
const iter = IndexedCollection(value);
const size = iter.size;
if (size === 0) {
return empty;
}
assertNotInfinite(size);
if (size > 0 && size < SIZE) {
return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
}
return empty.withMutations(list => {
list.setSize(size);
iter.forEach((v, i) => list.set(i, v));
});
};

List.of = function (/*...values*/) {
return List(arguments);
};

export class ListImpl extends IndexedCollectionImpl {
// @pragma Construction

static of(/*...values*/) {
return this(arguments);
create(value) {
return List(value);
}

toString() {
Expand Down Expand Up @@ -159,7 +158,7 @@ export class List extends IndexedCollection {
return this;
}
if (this.size === 0 && !this.__ownerID && seqs.length === 1) {
return this.constructor(seqs[0]);
return List(seqs[0]);
}
return this.withMutations(list => {
seqs.forEach(seq => seq.forEach(value => list.push(value)));
Expand Down Expand Up @@ -241,7 +240,7 @@ export class List extends IndexedCollection {

List.isList = isList;

const ListPrototype = List.prototype;
const ListPrototype = ListImpl.prototype;
ListPrototype[IS_LIST_SYMBOL] = true;
ListPrototype[DELETE] = ListPrototype.remove;
ListPrototype.merge = ListPrototype.concat;
Expand Down
Loading
Loading