immutable 5.1.1 → 5.1.3

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.
package/README.md CHANGED
@@ -73,10 +73,8 @@ bun add immutable
73
73
 
74
74
  Then require it into any module.
75
75
 
76
- <!-- runkit:activate -->
77
-
78
76
  ```js
79
- const { Map } = require('immutable');
77
+ import { Map } from 'immutable';
80
78
  const map1 = Map({ a: 1, b: 2, c: 3 });
81
79
  const map2 = map1.set('b', 50);
82
80
  map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
@@ -136,10 +134,8 @@ lib. Include either `"target": "es2015"` or `"lib": "es2015"` in your
136
134
  `tsconfig.json`, or provide `--target es2015` or `--lib es2015` to the
137
135
  `tsc` command.
138
136
 
139
- <!-- runkit:activate -->
140
-
141
137
  ```js
142
- const { Map } = require('immutable');
138
+ import { Map } from 'immutable';
143
139
  const map1 = Map({ a: 1, b: 2, c: 3 });
144
140
  const map2 = map1.set('b', 50);
145
141
  map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
@@ -187,10 +183,8 @@ treat Immutable.js collections as values, it's important to use the
187
183
  `Immutable.is()` function or `.equals()` method to determine _value equality_
188
184
  instead of the `===` operator which determines object _reference identity_.
189
185
 
190
- <!-- runkit:activate -->
191
-
192
186
  ```js
193
- const { Map } = require('immutable');
187
+ import { Map } from 'immutable';
194
188
  const map1 = Map({ a: 1, b: 2, c: 3 });
195
189
  const map2 = Map({ a: 1, b: 2, c: 3 });
196
190
  map1.equals(map2); // true
@@ -205,10 +199,8 @@ which would prefer to re-run the function if a deeper equality check could
205
199
  potentially be more costly. The `===` equality check is also used internally by
206
200
  `Immutable.is` and `.equals()` as a performance optimization.
207
201
 
208
- <!-- runkit:activate -->
209
-
210
202
  ```js
211
- const { Map } = require('immutable');
203
+ import { Map } from 'immutable';
212
204
  const map1 = Map({ a: 1, b: 2, c: 3 });
213
205
  const map2 = map1.set('b', 2); // Set to same value
214
206
  map1 === map2; // true
@@ -219,10 +211,8 @@ to it instead of copying the entire object. Because a reference is much smaller
219
211
  than the object itself, this results in memory savings and a potential boost in
220
212
  execution speed for programs which rely on copies (such as an undo-stack).
221
213
 
222
- <!-- runkit:activate -->
223
-
224
214
  ```js
225
- const { Map } = require('immutable');
215
+ import { Map } from 'immutable';
226
216
  const map = Map({ a: 1, b: 2, c: 3 });
227
217
  const mapCopy = map; // Look, "copies" are free!
228
218
  ```
@@ -230,7 +220,6 @@ const mapCopy = map; // Look, "copies" are free!
230
220
  [React]: https://reactjs.org/
231
221
  [Flux]: https://facebook.github.io/flux/docs/in-depth-overview/
232
222
 
233
-
234
223
  ## JavaScript-first API
235
224
 
236
225
  While Immutable.js is inspired by Clojure, Scala, Haskell and other functional
@@ -248,10 +237,8 @@ the collection, like `push`, `set`, `unshift` or `splice`, instead return a new
248
237
  immutable collection. Methods which return new arrays, like `slice` or `concat`,
249
238
  instead return new immutable collections.
250
239
 
251
- <!-- runkit:activate -->
252
-
253
240
  ```js
254
- const { List } = require('immutable');
241
+ import { List } from 'immutable';
255
242
  const list1 = List([1, 2]);
256
243
  const list2 = list1.push(3, 4, 5);
257
244
  const list3 = list2.unshift(0);
@@ -268,10 +255,8 @@ Almost all of the methods on [Array][] will be found in similar form on
268
255
  found on `Immutable.Set`, including collection operations like `forEach()`
269
256
  and `map()`.
270
257
 
271
- <!-- runkit:activate -->
272
-
273
258
  ```js
274
- const { Map } = require('immutable');
259
+ import { Map } from 'immutable';
275
260
  const alpha = Map({ a: 1, b: 2, c: 3, d: 4 });
276
261
  alpha.map((v, k) => k.toUpperCase()).join();
277
262
  // 'A,B,C,D'
@@ -283,10 +268,8 @@ Designed to inter-operate with your existing JavaScript, Immutable.js
283
268
  accepts plain JavaScript Arrays and Objects anywhere a method expects a
284
269
  `Collection`.
285
270
 
286
- <!-- runkit:activate -->
287
-
288
271
  ```js
289
- const { Map, List } = require('immutable');
272
+ import { Map, List } from 'immutable';
290
273
  const map1 = Map({ a: 1, b: 2, c: 3, d: 4 });
291
274
  const map2 = Map({ c: 10, a: 20, t: 30 });
292
275
  const obj = { d: 100, o: 200, g: 300 };
@@ -305,13 +288,11 @@ collection methods on JavaScript Objects, which otherwise have a very sparse
305
288
  native API. Because Seq evaluates lazily and does not cache intermediate
306
289
  results, these operations can be extremely efficient.
307
290
 
308
- <!-- runkit:activate -->
309
-
310
291
  ```js
311
- const { Seq } = require('immutable');
292
+ import { Seq } from 'immutable';
312
293
  const myObject = { a: 1, b: 2, c: 3 };
313
294
  Seq(myObject)
314
- .map(x => x * x)
295
+ .map((x) => x * x)
315
296
  .toObject();
316
297
  // { a: 1, b: 4, c: 9 }
317
298
  ```
@@ -320,10 +301,8 @@ Keep in mind, when using JS objects to construct Immutable Maps, that
320
301
  JavaScript Object properties are always strings, even if written in a quote-less
321
302
  shorthand, while Immutable Maps accept keys of any type.
322
303
 
323
- <!-- runkit:activate -->
324
-
325
304
  ```js
326
- const { fromJS } = require('immutable');
305
+ import { fromJS } from 'immutable';
327
306
 
328
307
  const obj = { 1: 'one' };
329
308
  console.log(Object.keys(obj)); // [ "1" ]
@@ -345,10 +324,8 @@ All Immutable Collections also implement `toJSON()` allowing them to be passed
345
324
  to `JSON.stringify` directly. They also respect the custom `toJSON()` methods of
346
325
  nested objects.
347
326
 
348
- <!-- runkit:activate -->
349
-
350
327
  ```js
351
- const { Map, List } = require('immutable');
328
+ import { Map, List } from 'immutable';
352
329
  const deep = Map({ a: 1, b: 2, c: List([3, 4, 5]) });
353
330
  console.log(deep.toObject()); // { a: 1, b: 2, c: List [ 3, 4, 5 ] }
354
331
  console.log(deep.toArray()); // [ 1, 2, List [ 3, 4, 5 ] ]
@@ -369,7 +346,7 @@ browsers, they need to be translated to ES5.
369
346
 
370
347
  ```js
371
348
  // ES2015
372
- const mapped = foo.map(x => x * x);
349
+ const mapped = foo.map((x) => x * x);
373
350
  // ES5
374
351
  var mapped = foo.map(function (x) {
375
352
  return x * x;
@@ -379,10 +356,8 @@ var mapped = foo.map(function (x) {
379
356
  All Immutable.js collections are [Iterable][iterators], which allows them to be
380
357
  used anywhere an Iterable is expected, such as when spreading into an Array.
381
358
 
382
- <!-- runkit:activate -->
383
-
384
359
  ```js
385
- const { List } = require('immutable');
360
+ import { List } from 'immutable';
386
361
  const aList = List([1, 2, 3]);
387
362
  const anArray = [0, ...aList, 4, 5]; // [ 0, 1, 2, 3, 4, 5 ]
388
363
  ```
@@ -395,16 +370,13 @@ not always be well defined, as is the case for the `Map` and `Set`.
395
370
  [Classes]: https://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
396
371
  [Modules]: https://www.2ality.com/2014/09/es6-modules-final.html
397
372
 
398
-
399
373
  ## Nested Structures
400
374
 
401
375
  The collections in Immutable.js are intended to be nested, allowing for deep
402
376
  trees of data, similar to JSON.
403
377
 
404
- <!-- runkit:activate -->
405
-
406
378
  ```js
407
- const { fromJS } = require('immutable');
379
+ import { fromJS } from 'immutable';
408
380
  const nested = fromJS({ a: { b: { c: [3, 4, 5] } } });
409
381
  // Map { a: Map { b: Map { c: List [ 3, 4, 5 ] } } }
410
382
  ```
@@ -413,10 +385,8 @@ A few power-tools allow for reading and operating on nested data. The
413
385
  most useful are `mergeDeep`, `getIn`, `setIn`, and `updateIn`, found on `List`,
414
386
  `Map` and `OrderedMap`.
415
387
 
416
- <!-- runkit:activate -->
417
-
418
388
  ```js
419
- const { fromJS } = require('immutable');
389
+ import { fromJS } from 'immutable';
420
390
  const nested = fromJS({ a: { b: { c: [3, 4, 5] } } });
421
391
 
422
392
  const nested2 = nested.mergeDeep({ a: { b: { d: 6 } } });
@@ -424,11 +394,11 @@ const nested2 = nested.mergeDeep({ a: { b: { d: 6 } } });
424
394
 
425
395
  console.log(nested2.getIn(['a', 'b', 'd'])); // 6
426
396
 
427
- const nested3 = nested2.updateIn(['a', 'b', 'd'], value => value + 1);
397
+ const nested3 = nested2.updateIn(['a', 'b', 'd'], (value) => value + 1);
428
398
  console.log(nested3);
429
399
  // Map { a: Map { b: Map { c: List [ 3, 4, 5 ], d: 7 } } }
430
400
 
431
- const nested4 = nested3.updateIn(['a', 'b', 'c'], list => list.push(6));
401
+ const nested4 = nested3.updateIn(['a', 'b', 'c'], (list) => list.push(6));
432
402
  // Map { a: Map { b: Map { c: List [ 3, 4, 5, 6 ], d: 7 } } }
433
403
  ```
434
404
 
@@ -443,15 +413,13 @@ determines if two variables represent references to the same object instance.
443
413
  Consider the example below where two identical `Map` instances are not
444
414
  _reference equal_ but are _value equal_.
445
415
 
446
- <!-- runkit:activate -->
447
-
448
416
  ```js
449
417
  // First consider:
450
418
  const obj1 = { a: 1, b: 2, c: 3 };
451
419
  const obj2 = { a: 1, b: 2, c: 3 };
452
420
  obj1 !== obj2; // two different instances are always not equal with ===
453
421
 
454
- const { Map, is } = require('immutable');
422
+ import { Map, is } from 'immutable';
455
423
  const map1 = Map({ a: 1, b: 2, c: 3 });
456
424
  const map2 = Map({ a: 1, b: 2, c: 3 });
457
425
  map1 !== map2; // two different instances are not reference-equal
@@ -462,10 +430,8 @@ is(map1, map2); // alternatively can use the is() function
462
430
  Value equality allows Immutable.js collections to be used as keys in Maps or
463
431
  values in Sets, and retrieved with different but equivalent collections:
464
432
 
465
- <!-- runkit:activate -->
466
-
467
433
  ```js
468
- const { Map, Set } = require('immutable');
434
+ import { Map, Set } from 'immutable';
469
435
  const map1 = Map({ a: 1, b: 2, c: 3 });
470
436
  const map2 = Map({ a: 1, b: 2, c: 3 });
471
437
  const set = Set().add(map1);
@@ -502,10 +468,8 @@ When possible, Immutable.js avoids creating new objects for updates where no
502
468
  change in _value_ occurred, to allow for efficient _reference equality_ checking
503
469
  to quickly determine if no change occurred.
504
470
 
505
- <!-- runkit:activate -->
506
-
507
471
  ```js
508
- const { Map } = require('immutable');
472
+ import { Map } from 'immutable';
509
473
  const originalMap = Map({ a: 1, b: 2, c: 3 });
510
474
  const updatedMap = originalMap.set('b', 2);
511
475
  updatedMap === originalMap; // No-op .set() returned the original reference.
@@ -515,10 +479,8 @@ However updates which do result in a change will return a new reference. Each
515
479
  of these operations occur independently, so two similar updates will not return
516
480
  the same reference:
517
481
 
518
- <!-- runkit:activate -->
519
-
520
482
  ```js
521
- const { Map } = require('immutable');
483
+ import { Map } from 'immutable';
522
484
  const originalMap = Map({ a: 1, b: 2, c: 3 });
523
485
  const updatedMap = originalMap.set('b', 1000);
524
486
  // New instance, leaving the original immutable.
@@ -549,10 +511,8 @@ exactly how Immutable.js applies complex mutations itself.
549
511
  As an example, building `list2` results in the creation of 1, not 3, new
550
512
  immutable Lists.
551
513
 
552
- <!-- runkit:activate -->
553
-
554
514
  ```js
555
- const { List } = require('immutable');
515
+ import { List } from 'immutable';
556
516
  const list1 = List([1, 2, 3]);
557
517
  const list2 = list1.withMutations(function (list) {
558
518
  list.push(4).push(5).push(6);
@@ -590,10 +550,10 @@ For example, the following performs no work, because the resulting
590
550
  `Seq`'s values are never iterated:
591
551
 
592
552
  ```js
593
- const { Seq } = require('immutable');
553
+ import { Seq } from 'immutable';
594
554
  const oddSquares = Seq([1, 2, 3, 4, 5, 6, 7, 8])
595
- .filter(x => x % 2 !== 0)
596
- .map(x => x * x);
555
+ .filter((x) => x % 2 !== 0)
556
+ .map((x) => x * x);
597
557
  ```
598
558
 
599
559
  Once the `Seq` is used, it performs only the work necessary. In this
@@ -606,10 +566,8 @@ oddSquares.get(1); // 9
606
566
 
607
567
  Any collection can be converted to a lazy Seq with `Seq()`.
608
568
 
609
- <!-- runkit:activate -->
610
-
611
569
  ```js
612
- const { Map, Seq } = require('immutable');
570
+ import { Map, Seq } from 'immutable';
613
571
  const map = Map({ a: 1, b: 2, c: 3 });
614
572
  const lazySeq = Seq(map);
615
573
  ```
@@ -620,7 +578,7 @@ expression of logic that can otherwise be very tedious:
620
578
  ```js
621
579
  lazySeq
622
580
  .flip()
623
- .map(key => key.toUpperCase())
581
+ .map((key) => key.toUpperCase())
624
582
  .flip();
625
583
  // Seq { A: 1, B: 2, C: 3 }
626
584
  ```
@@ -628,14 +586,12 @@ lazySeq
628
586
  As well as expressing logic that would otherwise seem memory or time
629
587
  limited, for example `Range` is a special kind of Lazy sequence.
630
588
 
631
- <!-- runkit:activate -->
632
-
633
589
  ```js
634
- const { Range } = require('immutable');
590
+ import { Range } from 'immutable';
635
591
  Range(1, Infinity)
636
592
  .skip(1000)
637
- .map(n => -n)
638
- .filter(n => n % 2 === 0)
593
+ .map((n) => -n)
594
+ .filter((n) => n % 2 === 0)
639
595
  .take(2)
640
596
  .reduce((r, n) => r * n, 1);
641
597
  // 1006008
@@ -646,8 +602,8 @@ Range(1, Infinity)
646
602
  The `filter()`, `groupBy()`, and `partition()` methods are similar in that they
647
603
  all divide a collection into parts based on applying a function to each element.
648
604
  All three call the predicate or grouping function once for each item in the
649
- input collection. All three return zero or more collections of the same type as
650
- their input. The returned collections are always distinct from the input
605
+ input collection. All three return zero or more collections of the same type as
606
+ their input. The returned collections are always distinct from the input
651
607
  (according to `===`), even if the contents are identical.
652
608
 
653
609
  Of these methods, `filter()` is the only one that is lazy and the only one which
@@ -658,21 +614,21 @@ methods to form a pipeline of operations.
658
614
  The `partition()` method is similar to an eager version of `filter()`, but it
659
615
  returns two collections; the first contains the items that would have been
660
616
  discarded by `filter()`, and the second contains the items that would have been
661
- kept. It always returns an array of exactly two collections, which can make it
662
- easier to use than `groupBy()`. Compared to making two separate calls to
617
+ kept. It always returns an array of exactly two collections, which can make it
618
+ easier to use than `groupBy()`. Compared to making two separate calls to
663
619
  `filter()`, `partition()` makes half as many calls it the predicate passed to
664
620
  it.
665
621
 
666
622
  The `groupBy()` method is a more generalized version of `partition()` that can
667
- group by an arbitrary function rather than just a predicate. It returns a map
623
+ group by an arbitrary function rather than just a predicate. It returns a map
668
624
  with zero or more entries, where the keys are the values returned by the
669
625
  grouping function, and the values are nonempty collections of the corresponding
670
- arguments. Although `groupBy()` is more powerful than `partition()`, it can be
626
+ arguments. Although `groupBy()` is more powerful than `partition()`, it can be
671
627
  harder to use because it is not always possible predict in advance how many
672
628
  entries the returned map will have and what their keys will be.
673
629
 
674
630
  | Summary | `filter` | `partition` | `groupBy` |
675
- |:------------------------------|:---------|:------------|:---------------|
631
+ | :---------------------------- | :------- | :---------- | :------------- |
676
632
  | ease of use | easiest | moderate | hardest |
677
633
  | generality | least | moderate | most |
678
634
  | laziness | lazy | eager | eager |
@@ -684,49 +640,63 @@ entries the returned map will have and what their keys will be.
684
640
  ## Additional Tools and Resources
685
641
 
686
642
  - [Atom-store](https://github.com/jameshopkins/atom-store/)
643
+
687
644
  - A Clojure-inspired atom implementation in Javascript with configurability
688
645
  for external persistance.
689
646
 
690
647
  - [Chai Immutable](https://github.com/astorije/chai-immutable)
648
+
691
649
  - If you are using the [Chai Assertion Library](https://chaijs.com/), this
692
650
  provides a set of assertions to use against Immutable.js collections.
693
651
 
694
652
  - [Fantasy-land](https://github.com/fantasyland/fantasy-land)
653
+
695
654
  - Specification for interoperability of common algebraic structures in JavaScript.
696
655
 
697
656
  - [Immutagen](https://github.com/pelotom/immutagen)
657
+
698
658
  - A library for simulating immutable generators in JavaScript.
699
659
 
700
660
  - [Immutable-cursor](https://github.com/redbadger/immutable-cursor)
661
+
701
662
  - Immutable cursors incorporating the Immutable.js interface over
702
- Clojure-inspired atom.
663
+ Clojure-inspired atom.
703
664
 
704
665
  - [Immutable-ext](https://github.com/DrBoolean/immutable-ext)
666
+
705
667
  - Fantasyland extensions for immutablejs
706
668
 
707
669
  - [Immutable-js-tools](https://github.com/madeinfree/immutable-js-tools)
670
+
708
671
  - Util tools for immutable.js
709
672
 
710
673
  - [Immutable-Redux](https://github.com/gajus/redux-immutable)
674
+
711
675
  - redux-immutable is used to create an equivalent function of Redux
712
- combineReducers that works with Immutable.js state.
676
+ combineReducers that works with Immutable.js state.
713
677
 
714
678
  - [Immutable-Treeutils](https://github.com/lukasbuenger/immutable-treeutils)
679
+
715
680
  - Functional tree traversal helpers for ImmutableJS data structures.
716
681
 
717
682
  - [Irecord](https://github.com/ericelliott/irecord)
683
+
718
684
  - An immutable store that exposes an RxJS observable. Great for React.
719
685
 
720
686
  - [Mudash](https://github.com/brianneisler/mudash)
687
+
721
688
  - Lodash wrapper providing Immutable.JS support.
722
689
 
723
690
  - [React-Immutable-PropTypes](https://github.com/HurricaneJames/react-immutable-proptypes)
691
+
724
692
  - PropType validators that work with Immutable.js.
725
693
 
726
694
  - [Redux-Immutablejs](https://github.com/indexiatech/redux-immutablejs)
695
+
727
696
  - Redux Immutable facilities.
728
697
 
729
698
  - [Rxstate](https://github.com/yamalight/rxstate)
699
+
730
700
  - Simple opinionated state management library based on RxJS and Immutable.js.
731
701
 
732
702
  - [Transit-Immutable-js](https://github.com/glenjamin/transit-immutable-js)