@@ -41,12 +41,12 @@ npm install immutable
41
41
42
42
Then require it into any module.
43
43
44
+ <!-- runkit:activate -->
44
45
``` js
45
46
const { Map } = require (' immutable' )
46
47
const map1 = Map ({ a: 1 , b: 2 , c: 3 })
47
48
const map2 = map1 .set (' b' , 50 )
48
- map1 .get (' b' ) // 2
49
- map2 .get (' b' ) // 50
49
+ map1 .get (' b' ) + " vs. " + map2 .get (' b' ) // 2 vs. 50
50
50
```
51
51
52
52
### Browser
@@ -99,12 +99,12 @@ lib. Include either `"target": "es2015"` or `"lib": "es2015"` in your
99
99
` tsconfig.json ` , or provide ` --target es2015 ` or ` --lib es2015 ` to the
100
100
` tsc ` command.
101
101
102
+ <!-- runkit:activate -->
102
103
``` js
103
- import { Map } from " immutable" ;
104
+ const { Map } = require ( " immutable" ) ;
104
105
const map1 = Map ({ a: 1 , b: 2 , c: 3 });
105
106
const map2 = map1 .set (' b' , 50 );
106
- map1 .get (' b' ); // 2
107
- map2 .get (' b' ); // 50
107
+ map1 .get (' b' ) + " vs. " + map2 .get (' b' ) // 2 vs. 50
108
108
```
109
109
110
110
#### Using TypeScript with Immutable.js v3 and earlier:
@@ -114,7 +114,7 @@ via relative path to the type definitions at the top of your file.
114
114
115
115
``` js
116
116
// /<reference path='./node_modules/immutable/dist/immutable.d.ts'/>
117
- import Immutable = require(' immutable' );
117
+ import Immutable from require(' immutable' );
118
118
var map1: Immutable .Map < string, number> ;
119
119
map1 = Immutable .Map ({a: 1 , b: 2 , c: 3 });
120
120
var map2 = map1 .set (' b' , 50 );
@@ -151,13 +151,15 @@ treat Immutable.js collections as values, it's important to use the
151
151
` Immutable.is() ` function or ` .equals() ` method to determine value equality
152
152
instead of the ` === ` operator which determines object reference identity.
153
153
154
+ <!-- runkit:activate -->
154
155
``` js
155
156
const { Map } = require (' immutable' )
156
157
const map1 = Map ( {a: 1 , b: 2 , c: 3 })
157
158
const map2 = map1 .set (' b' , 2 )
158
- assert (map1 .equals (map2) === true )
159
+ assert .equal (map1, map2) // uses map1.equals
160
+ assert .strictEqual (map1, map2) // uses ===
159
161
const map3 = map1 .set (' b' , 50 )
160
- assert ( map1 . equals ( map3) === false )
162
+ assert . notEqual (map1, map3) // uses map1.equals
161
163
```
162
164
163
165
Note: As a performance optimization Immutable.js attempts to return the existing
@@ -173,6 +175,7 @@ to it instead of copying the entire object. Because a reference is much smaller
173
175
than the object itself, this results in memory savings and a potential boost in
174
176
execution speed for programs which rely on copies (such as an undo-stack).
175
177
178
+ <!-- runkit:activate -->
176
179
``` js
177
180
const { Map } = require (' immutable' )
178
181
const map1 = Map ({ a: 1 , b: 2 , c: 3 })
@@ -197,28 +200,30 @@ of [ES2015][] [Array][], [Map][], and [Set][].
197
200
[ Set ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
198
201
199
202
The difference for the immutable collections is that methods which would mutate
200
- the collection, like ` push ` , ` set ` , ` unshift ` or ` splice ` instead return a new
201
- immutable collection. Methods which return new arrays like ` slice ` or ` concat `
203
+ the collection, like ` push ` , ` set ` , ` unshift ` or ` splice ` , instead return a new
204
+ immutable collection. Methods which return new arrays, like ` slice ` or ` concat ` ,
202
205
instead return new immutable collections.
203
206
207
+ <!-- runkit:activate -->
204
208
``` js
205
209
const { List } = require (' immutable' )
206
210
const list1 = List ([ 1 , 2 ]);
207
211
const list2 = list1 .push (3 , 4 , 5 );
208
212
const list3 = list2 .unshift (0 );
209
213
const list4 = list1 .concat (list2, list3);
210
- assert (list1 .size === 2 );
211
- assert (list2 .size === 5 );
212
- assert (list3 .size === 6 );
213
- assert (list4 .size === 13 );
214
- assert (list4 .get (0 ) === 1 );
214
+ assert . equal (list1 .size , 2 );
215
+ assert . equal (list2 .size , 5 );
216
+ assert . equal (list3 .size , 6 );
217
+ assert . equal (list4 .size , 13 );
218
+ assert . equal (list4 .get (0 ), 1 );
215
219
```
216
220
217
221
Almost all of the methods on [ Array] [ ] will be found in similar form on
218
222
` Immutable.List ` , those of [ Map] [ ] found on ` Immutable.Map ` , and those of [ Set] [ ]
219
223
found on ` Immutable.Set ` , including collection operations like ` forEach() `
220
224
and ` map() ` .
221
225
226
+ <!-- runkit:activate -->
222
227
``` js
223
228
const { Map } = require (' immutable' )
224
229
const alpha = Map ({ a: 1 , b: 2 , c: 3 , d: 4 });
@@ -232,6 +237,7 @@ Designed to inter-operate with your existing JavaScript, Immutable.js
232
237
accepts plain JavaScript Arrays and Objects anywhere a method expects an
233
238
` Collection ` .
234
239
240
+ <!-- runkit:activate -->
235
241
``` js
236
242
const { Map } = require (' immutable' )
237
243
const map1 = Map ({ a: 1 , b: 2 , c: 3 , d: 4 })
@@ -247,6 +253,7 @@ collection methods on JavaScript Objects, which otherwise have a very sparse
247
253
native API. Because Seq evaluates lazily and does not cache intermediate
248
254
results, these operations can be extremely efficient.
249
255
256
+ <!-- runkit:activate -->
250
257
``` js
251
258
const { Seq } = require (' immutable' )
252
259
const myObject = { a: 1 , b: 2 , c: 3 }
@@ -258,17 +265,16 @@ Keep in mind, when using JS objects to construct Immutable Maps, that
258
265
JavaScript Object properties are always strings, even if written in a quote-less
259
266
shorthand, while Immutable Maps accept keys of any type.
260
267
268
+ <!-- runkit:activate -->
261
269
``` js
262
270
const { fromJS } = require (' immutable' )
263
271
264
272
const obj = { 1 : " one" }
265
273
Object .keys (obj) // [ "1" ]
266
- obj[" 1" ] // "one"
267
- obj[1 ] // "one"
274
+ assert .equal (obj[" 1" ], obj[1 ]) // "one" === "one"
268
275
269
276
const map = fromJS (obj)
270
- map .get (" 1" ) // "one"
271
- map .get (1 ) // undefined
277
+ assert .notEqual (map .get (" 1" ), map .get (1 )) // "one" !== undefined
272
278
```
273
279
274
280
Property access for JavaScript Objects first converts the key to a string, but
@@ -283,12 +289,13 @@ Objects shallowly with `toArray()` and `toObject()` or deeply with `toJS()`.
283
289
All Immutable Collections also implement ` toJSON() ` allowing them to be passed
284
290
to ` JSON.stringify ` directly.
285
291
292
+ <!-- runkit:activate -->
286
293
``` js
287
294
const { Map , List } = require (' immutable' )
288
295
const deep = Map ({ a: 1 , b: 2 , c: List ([ 3 , 4 , 5 ]) })
289
- deep .toObject () // { a: 1, b: 2, c: List [ 3, 4, 5 ] }
290
- deep .toArray () // [ 1, 2, List [ 3, 4, 5 ] ]
291
- deep .toJS () // { a: 1, b: 2, c: [ 3, 4, 5 ] }
296
+ console . log ( deep .toObject () ) // { a: 1, b: 2, c: List [ 3, 4, 5 ] }
297
+ console . log ( deep .toArray () ) // [ 1, 2, List [ 3, 4, 5 ] ]
298
+ console . log ( deep .toJS () ) // { a: 1, b: 2, c: [ 3, 4, 5 ] }
292
299
JSON .stringify (deep) // '{"a":1,"b":2,"c":[3,4,5]}'
293
300
```
294
301
@@ -301,12 +308,12 @@ JavaScript in [ES2015][], the latest standard version of JavaScript, including
301
308
by the native [ Map] [ ] and [ Set] [ ] collections added to ES2015.
302
309
303
310
All examples in the Documentation are presented in ES2015. To run in all
304
- browsers, they need to be translated to ES3 .
311
+ browsers, they need to be translated to ES5 .
305
312
306
313
``` js
307
314
// ES2015
308
315
const mapped = foo .map (x => x * x);
309
- // ES3
316
+ // ES5
310
317
var mapped = foo .map (function (x ) { return x * x; });
311
318
```
312
319
@@ -322,6 +329,7 @@ Nested Structures
322
329
The collections in Immutable.js are intended to be nested, allowing for deep
323
330
trees of data, similar to JSON.
324
331
332
+ <!-- runkit:activate -->
325
333
``` js
326
334
const { fromJS } = require (' immutable' )
327
335
const nested = fromJS ({ a: { b: { c: [ 3 , 4 , 5 ] } } })
@@ -332,13 +340,18 @@ A few power-tools allow for reading and operating on nested data. The
332
340
most useful are ` mergeDeep ` , ` getIn ` , ` setIn ` , and ` updateIn ` , found on ` List ` ,
333
341
` Map ` and ` OrderedMap ` .
334
342
343
+ <!-- runkit:activate -->
335
344
``` js
345
+ const { fromJS } = require (' immutable' )
346
+ const nested = fromJS ({ a: { b: { c: [ 3 , 4 , 5 ] } } })
347
+
336
348
const nested2 = nested .mergeDeep ({ a: { b: { d: 6 } } })
337
349
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ], d: 6 } } }
338
350
339
- nested2 .getIn ([ ' a' , ' b' , ' d' ]) // 6
351
+ console . log ( nested2 .getIn ([ ' a' , ' b' , ' d' ]) ) // 6
340
352
341
353
const nested3 = nested2 .updateIn ([ ' a' , ' b' , ' d' ], value => value + 1 )
354
+ console .log (nested3);
342
355
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ], d: 7 } } }
343
356
344
357
const nested4 = nested3 .updateIn ([ ' a' , ' b' , ' c' ], list => list .push (6 ))
@@ -379,6 +392,7 @@ console.log(oddSquares.get(1)); // 9
379
392
380
393
Any collection can be converted to a lazy Seq with ` .toSeq() ` .
381
394
395
+ <!-- runkit:activate -->
382
396
``` js
383
397
const { Map } = require (' immutable' )
384
398
const seq = Map ({ a: 1 , b: 2 , c: 3 }).toSeq ()
@@ -389,11 +403,12 @@ converting to a different concrete type (such as to a JS object):
389
403
390
404
``` js
391
405
seq .flip ().map (key => key .toUpperCase ()).flip ().toObject ();
392
- // { A: 1, B: 1 , C: 1 }
406
+ // { A: 1, B: 2 , C: 3 }
393
407
```
394
408
395
409
As well as expressing logic that would otherwise seem memory-limited:
396
410
411
+ <!-- runkit:activate -->
397
412
``` js
398
413
const { Range } = require (' immutable' )
399
414
Range (1 , Infinity )
@@ -415,13 +430,14 @@ Equality treats Collections as Data
415
430
Immutable.js provides equality which treats immutable data structures as pure
416
431
data, performing a deep equality check if necessary.
417
432
433
+ <!-- runkit:activate -->
418
434
``` js
419
435
const { Map , is } = require (' immutable' )
420
436
const map1 = Map ({ a: 1 , b: 2 , c: 3 })
421
437
const map2 = Map ({ a: 1 , b: 2 , c: 3 })
422
- assert (map1 !== map2) // two different instances
423
- assert (is (map1, map2)) // have equivalent values
424
- assert (map1 .equals (map2)) // alternatively use the equals method
438
+ assert . equal (map1 !== map2, true ) // two different instances
439
+ assert . equal (is (map1, map2), true ) // have equivalent values
440
+ assert . equal (map1 .equals (map2), true ) // alternatively use the equals method
425
441
```
426
442
427
443
` Immutable.is() ` uses the same measure of equality as [ Object.is] [ ]
@@ -451,14 +467,15 @@ exactly how Immutable.js applies complex mutations itself.
451
467
As an example, building ` list2 ` results in the creation of 1, not 3, new
452
468
immutable Lists.
453
469
470
+ <!-- runkit:activate -->
454
471
``` js
455
472
const { List } = require (' immutable' )
456
473
const list1 = List ([ 1 , 2 , 3 ]);
457
474
const list2 = list1 .withMutations (function (list ) {
458
475
list .push (4 ).push (5 ).push (6 );
459
476
});
460
- assert (list1 .size === 3 );
461
- assert (list2 .size === 6 );
477
+ assert . equal (list1 .size , 3 );
478
+ assert . equal (list2 .size , 6 );
462
479
```
463
480
464
481
Note: Immutable.js also provides ` asMutable ` and ` asImmutable ` , but only
0 commit comments