@@ -73,8 +73,6 @@ bun add immutable
73
73
74
74
Then require it into any module.
75
75
76
- <!-- runkit:activate -->
77
-
78
76
``` js
79
77
const { Map } = require (' immutable' );
80
78
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
@@ -136,8 +134,6 @@ 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
138
const { Map } = require (' immutable' );
143
139
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
@@ -187,8 +183,6 @@ 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
187
const { Map } = require (' immutable' );
194
188
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
@@ -205,8 +199,6 @@ 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
203
const { Map } = require (' immutable' );
212
204
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
@@ -219,8 +211,6 @@ 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
215
const { Map } = require (' immutable' );
226
216
const map = Map ({ a: 1 , b: 2 , c: 3 });
@@ -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,8 +237,6 @@ 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
241
const { List } = require (' immutable' );
255
242
const list1 = List ([1 , 2 ]);
@@ -268,8 +255,6 @@ 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
259
const { Map } = require (' immutable' );
275
260
const alpha = Map ({ a: 1 , b: 2 , c: 3 , d: 4 });
@@ -283,8 +268,6 @@ 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
272
const { Map , List } = require (' immutable' );
290
273
const map1 = Map ({ a: 1 , b: 2 , c: 3 , d: 4 });
@@ -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
292
const { Seq } = require (' 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,8 +301,6 @@ 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
305
const { fromJS } = require (' immutable' );
327
306
@@ -345,8 +324,6 @@ 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
328
const { Map , List } = require (' immutable' );
352
329
const deep = Map ({ a: 1 , b: 2 , c: 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,8 +356,6 @@ 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
360
const { List } = require (' immutable' );
386
361
const aList = List ([1 , 2 , 3 ]);
@@ -395,14 +370,11 @@ 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
379
const { fromJS } = require (' immutable' );
408
380
const nested = fromJS ({ a: { b: { c: [3 , 4 , 5 ] } } });
@@ -413,8 +385,6 @@ 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
389
const { fromJS } = require (' immutable' );
420
390
const nested = fromJS ({ a: { b: { c: [3 , 4 , 5 ] } } });
@@ -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,8 +413,6 @@ 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 };
@@ -462,8 +430,6 @@ 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
434
const { Map , Set } = require (' immutable' );
469
435
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
@@ -502,8 +468,6 @@ 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
472
const { Map } = require (' immutable' );
509
473
const originalMap = Map ({ a: 1 , b: 2 , c: 3 });
@@ -515,8 +479,6 @@ 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
483
const { Map } = require (' immutable' );
522
484
const originalMap = Map ({ a: 1 , b: 2 , c: 3 });
@@ -549,8 +511,6 @@ 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
515
const { List } = require (' immutable' );
556
516
const list1 = List ([1 , 2 , 3 ]);
@@ -592,8 +552,8 @@ For example, the following performs no work, because the resulting
592
552
``` js
593
553
const { Seq } = require (' 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,8 +566,6 @@ 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
570
const { Map , Seq } = require (' immutable' );
613
571
const map = Map ({ a: 1 , b: 2 , c: 3 });
@@ -620,22 +578,20 @@ 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
```
627
585
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
590
const { Range } = require (' 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 )
0 commit comments