8000 Accept null/undefined as constructor for Collections · spmjs/immutable-js@d111f3b · GitHub
[go: up one dir, main page]

Skip to content

Commit d111f3b

Browse files
committed
Accept null/undefined as constructor for Collections
ES6 Map and Set allow null/undefined as the constructor argument and return empty Map/Set in those cases. They do not allow false. Mirroring that behavior actually simplifies a few things as well.
1 parent 228a537 commit d111f3b

File tree

10 files changed

+63
-71
lines changed

10 files changed

+63
-71
lines changed

dist/immutable.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function _iteratorFn(iterable) {
276276
}
277277
}
278278
var Iterable = function Iterable(value) {
279-
return isIterable(value) ? value : Seq.apply(undefined, arguments);
279+
return isIterable(value) ? value : Seq(value);
280280
};
281281
var $Iterable = Iterable;
282282
($traceurRuntime.createClass)(Iterable, {
@@ -644,7 +644,7 @@ IterablePrototype.chain = IterablePrototype.flatMap;
644644
} catch (e) {}
645645
})();
646646
var KeyedIterable = function KeyedIterable(value) {
647-
return isKeyed(value) ? value : KeyedSeq.apply(undefined, arguments);
647+
return isKeyed(value) ? value : KeyedSeq(value);
648648
};
649649
($traceurRuntime.createClass)(KeyedIterable, {
650650
flip: function() {
@@ -693,7 +693,7 @@ KeyedIterablePrototype.__toStringMapper = (function(v, k) {
693693
return k + ': ' + quoteString(v);
694694
});
695695
var IndexedIterable = function IndexedIterable(value) {
696-
return isIndexed(value) ? value : IndexedSeq.apply(undefined, arguments);
696+
return isIndexed(value) ? value : IndexedSeq(value);
697697
};
698698
($traceurRuntime.createClass)(IndexedIterable, {
699699
toKeyedSeq: function() {
@@ -794,7 +794,7 @@ var IndexedIterable = function IndexedIterable(value) {
794794
}, {}, Iterable);
795795
IndexedIterable.prototype[IS_INDEXED_SENTINEL] = true;
796796
var SetIterable = function SetIterable(value) {
797-
return isIterable(value) && !isAssociative(value) ? value : SetSeq.apply(undefined, arguments);
797+
return isIterable(value) && !isAssociative(value) ? value : SetSeq(value);
798798
};
799799
($traceurRuntime.createClass)(SetIterable, {
800800
get: function(value, notSetValue) {
@@ -856,7 +856,7 @@ function mixin(ctor, methods) {
856856
return ctor;
857857
}
858858
var Seq = function Seq(value) {
859-
return arguments.length === 0 ? emptySequence() : (isIterable(value) ? value : seqFromValue(value, false)).toSeq();
859+
return value === null || value === undefined ? emptySequence() : (isIterable(value) ? value : seqFromValue(value, false)).toSeq();
860860
};
861861
var $Seq = Seq;
862862
($traceurRuntime.createClass)(Seq, {
@@ -883,7 +883,7 @@ var $Seq = Seq;
883883
return $Seq(arguments);
884884
}}, Iterable);
885885
var KeyedSeq = function KeyedSeq(value) {
886-
if (arguments.length === 0) {
886+
if (value === null || value === undefined) {
887887
return emptySequence().toKeyedSeq();
888888
}
889889
if (!isIterable(value)) {
@@ -904,7 +904,7 @@ var $KeyedSeq = KeyedSeq;
904904
}}, Seq);
905905
mixin(KeyedSeq, KeyedIterable.prototype);
906906
var IndexedSeq = function IndexedSeq(value) {
907-
return arguments.length === 0 ? emptySequence() : (isIterable(value) ? value : seqFromValue(value, false)).toIndexedSeq();
907+
return value === null || value === undefined ? emptySequence() : (isIterable(value) ? value : seqFromValue(value, false)).toIndexedSeq();
908908
};
909909
var $IndexedSeq = IndexedSeq;
910910
($traceurRuntime.createClass)(IndexedSeq, {
@@ -925,7 +925,7 @@ var $IndexedSeq = IndexedSeq;
925925
}}, Seq);
926926
mixin(IndexedSeq, IndexedIterable.prototype);
927927
var SetSeq = function SetSeq(value) {
928-
return arguments.length === 0 ? emptySequence().toSetSeq() : (isIterable(value) ? value : seqFromValue(value, false)).toSetSeq();
928+
return value === null || value === undefined ? emptySequence().toSetSeq() : (isIterable(value) ? value : seqFromValue(value, false)).toSetSeq();
929929
};
930930
var $SetSeq = SetSeq;
931931
($traceurRuntime.createClass)(SetSeq, {toSetSeq: function() {
@@ -1192,9 +1192,8 @@ Collection.Keyed = KeyedCollection;
11921192
Collection.Indexed = IndexedCollection;
11931193
Collection.Set = SetCollection;
11941194
var Map = function Map(value) {
1195-
return arguments.length === 0 ? emptyMap() : value && value.constructor === $Map ? value : emptyMap().merge(KeyedIterable(value));
1195+
return value === null || value === undefined ? emptyMap() : isMap(value) ? value : emptyMap().merge(KeyedIterable(value));
11961196
};
1197-
var $Map = Map;
11981197
($traceurRuntime.createClass)(Map, {
11991198
toString: function() {
12001199
return this.__toString('Map {', '}');
@@ -2362,10 +2361,10 @@ function cacheResultThrough() {
23622361
}
23632362
var List = function List(value) {
23642363
var empty = emptyList();
2365-
if (arguments.length === 0) {
2364+
if (value === null || value === undefined) {
23662365
return empty;
23672366
}
2368-
if (value && value.constructor === $List) {
2367+
if (isList(value)) {
23692368
return value;
23702369
}
23712370
value = Iterable(value);
@@ -2378,7 +2377,6 @@ var List = function List(value) {
23782377
}
23792378
return empty.merge(value);
23802379
};
2381-
var $List = List;
23822380
($traceurRuntime.createClass)(List, {
23832381
toString: function() {
23842382
return this.__toString('List [', ']');
@@ -2870,7 +2868,7 @@ function getTailOffset(size) {
28702868
return size < SIZE ? 0 : (((size - 1) >>> SHIFT) << SHIFT);
28712869
}
28722870
var Stack = function Stack(value) {
2873-
return arguments.length === 0 ? emptyStack() : value && value.constructor === $Stack ? value : emptyStack().unshiftAll(value);
2871+
return value === null || value === undefined ? emptyStack() : isStack(value) ? value : emptyStack().unshiftAll(value);
28742872
};
28752873
var $Stack = Stack;
28762874
($traceurRuntime.createClass)(Stack, {
@@ -3047,9 +3045,8 @@ function emptyStack() {
30473045
return EMPTY_STACK || (EMPTY_STACK = makeStack(0));
30483046
}
30493047
var Set = function Set(value) {
3050-
return arguments.length === 0 ? emptySet() : value && value.constructor === $Set ? value : emptySet().union(value);
3048+
return value === null || value === undefined ? emptySet() : isSet(value) ? value : emptySet().union(value);
30513049
};
3052-
var $Set = Set;
30533050
($traceurRuntime.createClass)(Set, {
30543051
toString: function() {
30553052
return this.__toString('Set {', '}');
@@ -3209,9 +3206,8 @@ function emptySet() {
32093206
return EMPTY_SET || (EMPTY_SET = makeSet(emptyMap()));
32103207
}
32113208
var OrderedMap = function OrderedMap(value) {
3212-
return arguments.length === 0 ? emptyOrderedMap() : value && value.constructor === $OrderedMap ? value : emptyOrderedMap().merge(KeyedIterable(value));
3209+
return value === null || value === undefined ? emptyOrderedMap() : isOrderedMap(value) ? value : emptyOrderedMap().merge(KeyedIterable(value));
32133210
};
3214-
var $OrderedMap = OrderedMap;
32153211
($traceurRuntime.createClass)(OrderedMap, {
32163212
toString: function() {
32173213
return this.__toString('OrderedMap {', '}');
@@ -3315,7 +3311,7 @@ var Record = function Record(defaultValues, name) {
33153311
if (!(this instanceof RecordType)) {
33163312
return new RecordType(values);
33173313
}
3318-
this._map = arguments.length === 0 ? Map() : Map(values);
3314+
this._map = Map(values);
33193315
};
33203316
var keys = Object.keys(defaultValues);
33213317
var RecordTypePrototype = RecordType.prototype = Object.create(RecordPrototype);

0 commit comments

Comments
 (0)
0