immutable 4.0.0-rc.14 → 4.0.0-rc.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -244,6 +244,11 @@ function iteratorDone() {
244
244
  }
245
245
 
246
246
  function hasIterator(maybeIterable) {
247
+ if (Array.isArray(maybeIterable)) {
248
+ // IE11 trick as it does not support `Symbol.iterator`
249
+ return true;
250
+ }
251
+
247
252
  return !!getIteratorFn(maybeIterable);
248
253
  }
249
254
 
@@ -266,6 +271,16 @@ function getIteratorFn(iterable) {
266
271
  }
267
272
  }
268
273
 
274
+ function isEntriesIterable(maybeIterable) {
275
+ var iteratorFn = getIteratorFn(maybeIterable);
276
+ return iteratorFn && iteratorFn === maybeIterable.entries;
277
+ }
278
+
279
+ function isKeysIterable(maybeIterable) {
280
+ var iteratorFn = getIteratorFn(maybeIterable);
281
+ return iteratorFn && iteratorFn === maybeIterable.keys;
282
+ }
283
+
269
284
  var hasOwnProperty = Object.prototype.hasOwnProperty;
270
285
 
271
286
  function isArrayLike(value) {
@@ -596,11 +611,7 @@ function emptySequence() {
596
611
  }
597
612
 
598
613
  function keyedSeqFromValue(value) {
599
- var seq = Array.isArray(value)
600
- ? new ArraySeq(value)
601
- : hasIterator(value)
602
- ? new CollectionSeq(value)
603
- : undefined;
614
+ var seq = maybeIndexedSeqFromValue(value);
604
615
  if (seq) {
605
616
  return seq.fromEntrySeq();
606
617
  }
@@ -626,7 +637,11 @@ function indexedSeqFromValue(value) {
626
637
  function seqFromValue(value) {
627
638
  var seq = maybeIndexedSeqFromValue(value);
628
639
  if (seq) {
629
- return seq;
640
+ return isEntriesIterable(value)
641
+ ? seq.fromEntrySeq()
642
+ : isKeysIterable(value)
643
+ ? seq.toSetSeq()
644
+ : seq;
630
645
  }
631
646
  if (typeof value === 'object') {
632
647
  return new ObjectSeq(value);
@@ -2271,7 +2286,9 @@ function mergeWithSources(collection, sources, merger) {
2271
2286
 
2272
2287
  function deepMergerWith(merger) {
2273
2288
  function deepMerger(oldValue, newValue, key) {
2274
- return isDataStructure(oldValue) && isDataStructure(newValue)
2289
+ return isDataStructure(oldValue) &&
2290
+ isDataStructure(newValue) &&
2291
+ areMergeable(oldValue, newValue)
2275
2292
  ? mergeWithSources(oldValue, [newValue], deepMerger)
2276
2293
  : merger
2277
2294
  ? merger(oldValue, newValue, key)
@@ -2280,6 +2297,22 @@ function deepMergerWith(merger) {
2280
2297
  return deepMerger;
2281
2298
  }
2282
2299
 
2300
+ /**
2301
+ * It's unclear what the desired behavior is for merging two collections that
2302
+ * fall into separate categories between keyed, indexed, or set-like, so we only
2303
+ * consider them mergeable if they fall into the same category.
2304
+ */
2305
+ function areMergeable(oldDataStructure, newDataStructure) {
2306
+ var oldSeq = Seq(oldDataStructure);
2307
+ var newSeq = Seq(newDataStructure);
2308
+ // This logic assumes that a sequence can only fall into one of the three
2309
+ // categories mentioned above (since there's no `isSetLike()` method).
2310
+ return (
2311
+ isIndexed(oldSeq) === isIndexed(newSeq) &&
2312
+ isKeyed(oldSeq) === isKeyed(newSeq)
2313
+ );
2314
+ }
2315
+
2283
2316
  function mergeDeep() {
2284
2317
  var iters = [], len = arguments.length;
2285
2318
  while ( len-- ) iters[ len ] = arguments[ len ];
@@ -5295,14 +5328,16 @@ mixin(SetCollection, {
5295
5328
  },
5296
5329
  });
5297
5330
 
5298
- SetCollection.prototype.has = CollectionPrototype.includes;
5299
- SetCollection.prototype.contains = SetCollection.prototype.includes;
5331
+ var SetCollectionPrototype = SetCollection.prototype;
5332
+ SetCollectionPrototype.has = CollectionPrototype.includes;
5333
+ SetCollectionPrototype.contains = SetCollectionPrototype.includes;
5334
+ SetCollectionPrototype.keys = SetCollectionPrototype.values;
5300
5335
 
5301
5336
  // Mixin subclasses
5302
5337
 
5303
- mixin(KeyedSeq, KeyedCollection.prototype);
5304
- mixin(IndexedSeq, IndexedCollection.prototype);
5305
- mixin(SetSeq, SetCollection.prototype);
5338
+ mixin(KeyedSeq, KeyedCollectionPrototype);
5339
+ mixin(IndexedSeq, IndexedCollectionPrototype);
5340
+ mixin(SetSeq, SetCollectionPrototype);
5306
5341
 
5307
5342
  // #pragma Helper functions
5308
5343
 
@@ -5799,12 +5834,11 @@ function fromJS(value, converter) {
5799
5834
  }
5800
5835
 
5801
5836
  function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
5802
- var toSeq = Array.isArray(value)
5803
- ? IndexedSeq
5804
- : isPlainObject(value)
5805
- ? KeyedSeq
5806
- : null;
5807
- if (toSeq) {
5837
+ if (
5838
+ typeof value !== 'string' &&
5839
+ !isImmutable(value) &&
5840
+ (isArrayLike(value) || hasIterator(value) || isPlainObject(value))
5841
+ ) {
5808
5842
  if (~stack.indexOf(value)) {
5809
5843
  throw new TypeError('Cannot convert circular structure to Immutable');
5810
5844
  }
@@ -5813,7 +5847,7 @@ function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
5813
5847
  var converted = converter.call(
5814
5848
  parentValue,
5815
5849
  key,
5816
- toSeq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
5850
+ Seq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
5817
5851
  ),
5818
5852
  keyPath && keyPath.slice()
5819
5853
  );
@@ -5825,10 +5859,11 @@ function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
5825
5859
  }
5826
5860
 
5827
5861
  function defaultConverter(k, v) {
5828
- return isKeyed(v) ? v.toMap() : v.toList();
5862
+ // Effectively the opposite of "Collection.toSeq()"
5863
+ return isIndexed(v) ? v.toList() : isKeyed(v) ? v.toMap() : v.toSet();
5829
5864
  }
5830
5865
 
5831
- var version = "4.0.0-rc.14";
5866
+ var version = "4.0.0-rc.15";
5832
5867
 
5833
5868
  var Immutable = {
5834
5869
  version: version,
package/dist/immutable.js CHANGED
@@ -250,6 +250,11 @@
250
250
  }
251
251
 
252
252
  function hasIterator(maybeIterable) {
253
+ if (Array.isArray(maybeIterable)) {
254
+ // IE11 trick as it does not support `Symbol.iterator`
255
+ return true;
256
+ }
257
+
253
258
  return !!getIteratorFn(maybeIterable);
254
259
  }
255
260
 
@@ -272,6 +277,16 @@
272
277
  }
273
278
  }
274
279
 
280
+ function isEntriesIterable(maybeIterable) {
281
+ var iteratorFn = getIteratorFn(maybeIterable);
282
+ return iteratorFn && iteratorFn === maybeIterable.entries;
283
+ }
284
+
285
+ function isKeysIterable(maybeIterable) {
286
+ var iteratorFn = getIteratorFn(maybeIterable);
287
+ return iteratorFn && iteratorFn === maybeIterable.keys;
288
+ }
289
+
275
290
  var hasOwnProperty = Object.prototype.hasOwnProperty;
276
291
 
277
292
  function isArrayLike(value) {
@@ -602,11 +617,7 @@
602
617
  }
603
618
 
604
619
  function keyedSeqFromValue(value) {
605
- var seq = Array.isArray(value)
606
- ? new ArraySeq(value)
607
- : hasIterator(value)
608
- ? new CollectionSeq(value)
609
- : undefined;
620
+ var seq = maybeIndexedSeqFromValue(value);
610
621
  if (seq) {
611
622
  return seq.fromEntrySeq();
612
623
  }
@@ -632,7 +643,11 @@
632
643
  function seqFromValue(value) {
633
644
  var seq = maybeIndexedSeqFromValue(value);
634
645
  if (seq) {
635
- return seq;
646
+ return isEntriesIterable(value)
647
+ ? seq.fromEntrySeq()
648
+ : isKeysIterable(value)
649
+ ? seq.toSetSeq()
650
+ : seq;
636
651
  }
637
652
  if (typeof value === 'object') {
638
653
  return new ObjectSeq(value);
@@ -2277,7 +2292,9 @@
2277
2292
 
2278
2293
  function deepMergerWith(merger) {
2279
2294
  function deepMerger(oldValue, newValue, key) {
2280
- return isDataStructure(oldValue) && isDataStructure(newValue)
2295
+ return isDataStructure(oldValue) &&
2296
+ isDataStructure(newValue) &&
2297
+ areMergeable(oldValue, newValue)
2281
2298
  ? mergeWithSources(oldValue, [newValue], deepMerger)
2282
2299
  : merger
2283
2300
  ? merger(oldValue, newValue, key)
@@ -2286,6 +2303,22 @@
2286
2303
  return deepMerger;
2287
2304
  }
2288
2305
 
2306
+ /**
2307
+ * It's unclear what the desired behavior is for merging two collections that
2308
+ * fall into separate categories between keyed, indexed, or set-like, so we only
2309
+ * consider them mergeable if they fall into the same category.
2310
+ */
2311
+ function areMergeable(oldDataStructure, newDataStructure) {
2312
+ var oldSeq = Seq(oldDataStructure);
2313
+ var newSeq = Seq(newDataStructure);
2314
+ // This logic assumes that a sequence can only fall into one of the three
2315
+ // categories mentioned above (since there's no `isSetLike()` method).
2316
+ return (
2317
+ isIndexed(oldSeq) === isIndexed(newSeq) &&
2318
+ isKeyed(oldSeq) === isKeyed(newSeq)
2319
+ );
2320
+ }
2321
+
2289
2322
  function mergeDeep() {
2290
2323
  var iters = [], len = arguments.length;
2291
2324
  while ( len-- ) iters[ len ] = arguments[ len ];
@@ -5301,14 +5334,16 @@
5301
5334
  },
5302
5335
  });
5303
5336
 
5304
- SetCollection.prototype.has = CollectionPrototype.includes;
5305
- SetCollection.prototype.contains = SetCollection.prototype.includes;
5337
+ var SetCollectionPrototype = SetCollection.prototype;
5338
+ SetCollectionPrototype.has = CollectionPrototype.includes;
5339
+ SetCollectionPrototype.contains = SetCollectionPrototype.includes;
5340
+ SetCollectionPrototype.keys = SetCollectionPrototype.values;
5306
5341
 
5307
5342
  // Mixin subclasses
5308
5343
 
5309
- mixin(KeyedSeq, KeyedCollection.prototype);
5310
- mixin(IndexedSeq, IndexedCollection.prototype);
5311
- mixin(SetSeq, SetCollection.prototype);
5344
+ mixin(KeyedSeq, KeyedCollectionPrototype);
5345
+ mixin(IndexedSeq, IndexedCollectionPrototype);
5346
+ mixin(SetSeq, SetCollectionPrototype);
5312
5347
 
5313
5348
  // #pragma Helper functions
5314
5349
 
@@ -5805,12 +5840,11 @@
5805
5840
  }
5806
5841
 
5807
5842
  function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
5808
- var toSeq = Array.isArray(value)
5809
- ? IndexedSeq
5810
- : isPlainObject(value)
5811
- ? KeyedSeq
5812
- : null;
5813
- if (toSeq) {
5843
+ if (
5844
+ typeof value !== 'string' &&
5845
+ !isImmutable(value) &&
5846
+ (isArrayLike(value) || hasIterator(value) || isPlainObject(value))
5847
+ ) {
5814
5848
  if (~stack.indexOf(value)) {
5815
5849
  throw new TypeError('Cannot convert circular structure to Immutable');
5816
5850
  }
@@ -5819,7 +5853,7 @@
5819
5853
  var converted = converter.call(
5820
5854
  parentValue,
5821
5855
  key,
5822
- toSeq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
5856
+ Seq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
5823
5857
  ),
5824
5858
  keyPath && keyPath.slice()
5825
5859
  );
@@ -5831,10 +5865,11 @@
5831
5865
  }
5832
5866
 
5833
5867
  function defaultConverter(k, v) {
5834
- return isKeyed(v) ? v.toMap() : v.toList();
5868
+ // Effectively the opposite of "Collection.toSeq()"
5869
+ return isIndexed(v) ? v.toList() : isKeyed(v) ? v.toMap() : v.toSet();
5835
5870
  }
5836
5871
 
5837
- var version = "4.0.0-rc.14";
5872
+ var version = "4.0.0-rc.15";
5838
5873
 
5839
5874
  var Immutable = {
5840
5875
  version: version,
@@ -25,11 +25,14 @@
25
25
  // some constructors and functions.
26
26
  type PlainObjInput<K, V> = { +[key: K]: V, __proto__: null };
27
27
 
28
+ type K<T> = $Keys<T>;
29
+
28
30
  // Helper types to extract the "keys" and "values" use by the *In() methods.
29
31
  type $KeyOf<C> = $Call<
30
32
  (<K>(?_Collection<K, mixed>) => K) &
31
33
  (<T>(?$ReadOnlyArray<T>) => number) &
32
- (<T>(?RecordInstance<T> | T) => $Keys<T>),
34
+ (<T>(?RecordInstance<T> | T) => $Keys<T>) &
35
+ (<T: Object>(T) => $Keys<T>),
33
36
  C
34
37
  >;
35
38
 
@@ -37,7 +40,7 @@ type $ValOf<C, K = $KeyOf<C>> = $Call<
37
40
  (<V>(?_Collection<any, V>) => V) &
38
41
  (<T>(?$ReadOnlyArray<T>) => T) &
39
42
  (<T, K: $Keys<T>>(?RecordInstance<T> | T, K) => $ElementType<T, K>) &
40
- (<V>(?{ [any]: V }) => V),
43
+ (<T: Object>(T) => $Values<T>),
41
44
  C,
42
45
  K
43
46
  >;
@@ -593,7 +596,8 @@ declare class KeyedSeq<K, +V> extends Seq<K, V> mixins KeyedCollection<K, V> {
593
596
 
594
597
  declare class IndexedSeq<+T>
595
598
  extends Seq<number, T>
596
- mixins IndexedCollection<T> {
599
+ mixins IndexedCollection<T>
600
+ {
597
601
  static <T>(values?: Iterable<T>): IndexedSeq<T>;
598
602
 
599
603
  static of<T>(...values: T[]): IndexedSeq<T>;
@@ -892,7 +896,8 @@ declare function isList(maybeList: mixed): boolean %checks(maybeList instanceof
892
896
  List);
893
897
  declare class List<+T>
894
898
  extends IndexedCollection<T>
895
- mixins UpdatableInCollection<number, T> {
899
+ mixins UpdatableInCollection<number, T>
900
+ {
896
901
  static (collection?: Iterable<T>): List<T>;
897
902
 
898
903
  static of<T>(...values: T[]): List<T>;
@@ -1050,7 +1055,8 @@ declare function isMap(maybeMap: mixed): boolean %checks(maybeMap instanceof
1050
1055
  Map);
1051
1056
  declare class Map<K, +V>
1052
1057
  extends KeyedCollection<K, V>
1053
- mixins UpdatableInCollection<K, V> {
1058
+ mixins UpdatableInCollection<K, V>
1059
+ {
1054
1060
  static <K, V>(values?: Iterable<[K, V]> | PlainObjInput<K, V>): Map<K, V>;
1055
1061
 
1056
1062
  static isMap: typeof isMap;
@@ -1147,7 +1153,8 @@ declare function isOrderedMap(
1147
1153
  ): boolean %checks(maybeOrderedMap instanceof OrderedMap);
1148
1154
  declare class OrderedMap<K, +V>
1149
1155
  extends Map<K, V>
1150
- mixins UpdatableInCollection<K, V> {
1156
+ mixins UpdatableInCollection<K, V>
1157
+ {
1151
1158
  static <K, V>(
1152
1159
  values?: Iterable<[K, V]> | PlainObjInput<K, V>
1153
1160
  ): OrderedMap<K, V>;
@@ -1905,7 +1912,7 @@ declare function fromJS(
1905
1912
  sequence: KeyedCollection<string, mixed> | IndexedCollection<mixed>,
1906
1913
  path?: Array<string | number>
1907
1914
  ) => mixed
1908
- ): mixed;
1915
+ ): Collection<mixed, mixed>;
1909
1916
 
1910
1917
  declare function is(first: mixed, second: mixed): boolean;
1911
1918
  declare function hash(value: mixed): number;