immutable 4.0.0-rc.12 → 4.0.0-rc.14
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/LICENSE +1 -1
- package/README.md +111 -97
- package/dist/immutable-nonambient.d.ts +713 -443
- package/dist/immutable.d.ts +715 -445
- package/dist/immutable.es.js +521 -446
- package/dist/immutable.js +552 -468
- package/dist/immutable.js.flow +928 -262
- package/dist/immutable.min.js +53 -36
- package/package.json +6 -94
- package/contrib/cursor/README.md +0 -43
- package/contrib/cursor/__tests__/Cursor.ts.skip +0 -395
- package/contrib/cursor/index.d.ts +0 -287
- package/contrib/cursor/index.js +0 -360
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2014-present,
|
|
3
|
+
Copyright (c) 2014-present, Lee Byron and other contributors.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
Immutable collections for JavaScript
|
|
2
|
-
====================================
|
|
1
|
+
# Immutable collections for JavaScript
|
|
3
2
|
|
|
4
|
-
[](https://github.com/immutable-js/immutable-js/actions/workflows/ci.yml?query=branch%3Amain) [Chat on slack](https://immutable-js.slack.com)
|
|
5
4
|
|
|
6
5
|
[Immutable][] data cannot be changed once created, leading to much simpler
|
|
7
6
|
application development, no defensive copying, and enabling advanced memoization
|
|
@@ -22,16 +21,14 @@ intermediate representations. Create some `Seq` with `Range` and `Repeat`.
|
|
|
22
21
|
|
|
23
22
|
Want to hear more? Watch the presentation about Immutable.js:
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
[](https://youtu.be/I7IdS-PbEgI)
|
|
26
25
|
|
|
27
|
-
[Persistent]:
|
|
28
|
-
[Immutable]:
|
|
29
|
-
[hash maps tries]:
|
|
30
|
-
[vector tries]:
|
|
26
|
+
[Persistent]: https://en.wikipedia.org/wiki/Persistent_data_structure
|
|
27
|
+
[Immutable]: https://en.wikipedia.org/wiki/Immutable_object
|
|
28
|
+
[hash maps tries]: https://en.wikipedia.org/wiki/Hash_array_mapped_trie
|
|
29
|
+
[vector tries]: https://hypirion.com/musings/understanding-persistent-vector-pt-1
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
Getting started
|
|
34
|
-
---------------
|
|
31
|
+
## Getting started
|
|
35
32
|
|
|
36
33
|
Install `immutable` using npm.
|
|
37
34
|
|
|
@@ -39,14 +36,21 @@ Install `immutable` using npm.
|
|
|
39
36
|
npm install immutable
|
|
40
37
|
```
|
|
41
38
|
|
|
39
|
+
Or install using yarn.
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
yarn add immutable
|
|
43
|
+
```
|
|
44
|
+
|
|
42
45
|
Then require it into any module.
|
|
43
46
|
|
|
44
47
|
<!-- runkit:activate -->
|
|
48
|
+
|
|
45
49
|
```js
|
|
46
50
|
const { Map } = require('immutable');
|
|
47
51
|
const map1 = Map({ a: 1, b: 2, c: 3 });
|
|
48
52
|
const map2 = map1.set('b', 50);
|
|
49
|
-
map1.get('b') +
|
|
53
|
+
map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
|
|
50
54
|
```
|
|
51
55
|
|
|
52
56
|
### Browser
|
|
@@ -55,7 +59,7 @@ Immutable.js has no dependencies, which makes it predictable to include in a Bro
|
|
|
55
59
|
|
|
56
60
|
It's highly recommended to use a module bundler like [webpack](https://webpack.github.io/),
|
|
57
61
|
[rollup](https://rollupjs.org/), or
|
|
58
|
-
[browserify](
|
|
62
|
+
[browserify](https://browserify.org/). The `immutable` npm module works
|
|
59
63
|
without any additional consideration. All examples throughout the documentation
|
|
60
64
|
will assume use of this kind of tool.
|
|
61
65
|
|
|
@@ -75,7 +79,7 @@ Use a script tag to directly add `Immutable` to the global scope:
|
|
|
75
79
|
</script>
|
|
76
80
|
```
|
|
77
81
|
|
|
78
|
-
Or use an AMD-style loader (such as [RequireJS](
|
|
82
|
+
Or use an AMD-style loader (such as [RequireJS](https://requirejs.org/)):
|
|
79
83
|
|
|
80
84
|
```js
|
|
81
85
|
require(['./immutable.min.js'], function (Immutable) {
|
|
@@ -89,7 +93,7 @@ require(['./immutable.min.js'], function (Immutable) {
|
|
|
89
93
|
### Flow & TypeScript
|
|
90
94
|
|
|
91
95
|
Use these Immutable collections and sequences as you would use native
|
|
92
|
-
collections in your [Flowtype](https://flowtype.org/) or [TypeScript](
|
|
96
|
+
collections in your [Flowtype](https://flowtype.org/) or [TypeScript](https://typescriptlang.org) programs while still taking
|
|
93
97
|
advantage of type generics, error detection, and auto-complete in your IDE.
|
|
94
98
|
|
|
95
99
|
Installing `immutable` via npm brings with it type definitions for Flow (v0.55.0 or higher)
|
|
@@ -104,11 +108,12 @@ lib. Include either `"target": "es2015"` or `"lib": "es2015"` in your
|
|
|
104
108
|
`tsc` command.
|
|
105
109
|
|
|
106
110
|
<!-- runkit:activate -->
|
|
111
|
+
|
|
107
112
|
```js
|
|
108
|
-
const { Map } = require(
|
|
113
|
+
const { Map } = require('immutable');
|
|
109
114
|
const map1 = Map({ a: 1, b: 2, c: 3 });
|
|
110
115
|
const map2 = map1.set('b', 50);
|
|
111
|
-
map1.get('b') +
|
|
116
|
+
map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
|
|
112
117
|
```
|
|
113
118
|
|
|
114
119
|
#### Using TypeScript with Immutable.js v3 and earlier:
|
|
@@ -126,9 +131,7 @@ map1.get('b'); // 2
|
|
|
126
131
|
map2.get('b'); // 50
|
|
127
132
|
```
|
|
128
133
|
|
|
129
|
-
|
|
130
|
-
The case for Immutability
|
|
131
|
-
-------------------------
|
|
134
|
+
## The case for Immutability
|
|
132
135
|
|
|
133
136
|
Much of what makes application development difficult is tracking mutation and
|
|
134
137
|
maintaining state. Developing with immutable data encourages you to think
|
|
@@ -147,15 +150,16 @@ and especially well with an application designed using the ideas of [Flux][].
|
|
|
147
150
|
When data is passed from above rather than being subscribed to, and you're only
|
|
148
151
|
interested in doing work when something has changed, you can use equality.
|
|
149
152
|
|
|
150
|
-
Immutable collections should be treated as
|
|
153
|
+
Immutable collections should be treated as _values_ rather than _objects_. While
|
|
151
154
|
objects represent some thing which could change over time, a value represents
|
|
152
155
|
the state of that thing at a particular instance of time. This principle is most
|
|
153
156
|
important to understanding the appropriate use of immutable data. In order to
|
|
154
157
|
treat Immutable.js collections as values, it's important to use the
|
|
155
|
-
`Immutable.is()` function or `.equals()` method to determine
|
|
156
|
-
instead of the `===` operator which determines object
|
|
158
|
+
`Immutable.is()` function or `.equals()` method to determine _value equality_
|
|
159
|
+
instead of the `===` operator which determines object _reference identity_.
|
|
157
160
|
|
|
158
161
|
<!-- runkit:activate -->
|
|
162
|
+
|
|
159
163
|
```js
|
|
160
164
|
const { Map } = require('immutable');
|
|
161
165
|
const map1 = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -173,6 +177,7 @@ potentially be more costly. The `===` equality check is also used internally by
|
|
|
173
177
|
`Immutable.is` and `.equals()` as a performance optimization.
|
|
174
178
|
|
|
175
179
|
<!-- runkit:activate -->
|
|
180
|
+
|
|
176
181
|
```js
|
|
177
182
|
const { Map } = require('immutable');
|
|
178
183
|
const map1 = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -186,28 +191,28 @@ than the object itself, this results in memory savings and a potential boost in
|
|
|
186
191
|
execution speed for programs which rely on copies (such as an undo-stack).
|
|
187
192
|
|
|
188
193
|
<!-- runkit:activate -->
|
|
194
|
+
|
|
189
195
|
```js
|
|
190
196
|
const { Map } = require('immutable');
|
|
191
197
|
const map = Map({ a: 1, b: 2, c: 3 });
|
|
192
198
|
const mapCopy = map; // Look, "copies" are free!
|
|
193
199
|
```
|
|
194
200
|
|
|
195
|
-
[React]:
|
|
196
|
-
[Flux]:
|
|
201
|
+
[React]: https://reactjs.org/
|
|
202
|
+
[Flux]: https://facebook.github.io/flux/docs/in-depth-overview/
|
|
197
203
|
|
|
198
204
|
|
|
199
|
-
JavaScript-first API
|
|
200
|
-
--------------------
|
|
205
|
+
## JavaScript-first API
|
|
201
206
|
|
|
202
207
|
While Immutable.js is inspired by Clojure, Scala, Haskell and other functional
|
|
203
208
|
programming environments, it's designed to bring these powerful concepts to
|
|
204
209
|
JavaScript, and therefore has an Object-Oriented API that closely mirrors that
|
|
205
210
|
of [ES2015][] [Array][], [Map][], and [Set][].
|
|
206
211
|
|
|
207
|
-
[
|
|
208
|
-
[
|
|
209
|
-
[
|
|
210
|
-
[
|
|
212
|
+
[es2015]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
|
|
213
|
+
[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
214
|
+
[map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
|
215
|
+
[set]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
|
211
216
|
|
|
212
217
|
The difference for the immutable collections is that methods which would mutate
|
|
213
218
|
the collection, like `push`, `set`, `unshift` or `splice`, instead return a new
|
|
@@ -215,9 +220,10 @@ immutable collection. Methods which return new arrays, like `slice` or `concat`,
|
|
|
215
220
|
instead return new immutable collections.
|
|
216
221
|
|
|
217
222
|
<!-- runkit:activate -->
|
|
223
|
+
|
|
218
224
|
```js
|
|
219
225
|
const { List } = require('immutable');
|
|
220
|
-
const list1 = List([
|
|
226
|
+
const list1 = List([1, 2]);
|
|
221
227
|
const list2 = list1.push(3, 4, 5);
|
|
222
228
|
const list3 = list2.unshift(0);
|
|
223
229
|
const list4 = list1.concat(list2, list3);
|
|
@@ -234,6 +240,7 @@ found on `Immutable.Set`, including collection operations like `forEach()`
|
|
|
234
240
|
and `map()`.
|
|
235
241
|
|
|
236
242
|
<!-- runkit:activate -->
|
|
243
|
+
|
|
237
244
|
```js
|
|
238
245
|
const { Map } = require('immutable');
|
|
239
246
|
const alpha = Map({ a: 1, b: 2, c: 3, d: 4 });
|
|
@@ -248,6 +255,7 @@ accepts plain JavaScript Arrays and Objects anywhere a method expects a
|
|
|
248
255
|
`Collection`.
|
|
249
256
|
|
|
250
257
|
<!-- runkit:activate -->
|
|
258
|
+
|
|
251
259
|
```js
|
|
252
260
|
const { Map, List } = require('immutable');
|
|
253
261
|
const map1 = Map({ a: 1, b: 2, c: 3, d: 4 });
|
|
@@ -255,9 +263,9 @@ const map2 = Map({ c: 10, a: 20, t: 30 });
|
|
|
255
263
|
const obj = { d: 100, o: 200, g: 300 };
|
|
256
264
|
const map3 = map1.merge(map2, obj);
|
|
257
265
|
// Map { a: 20, b: 2, c: 10, d: 100, t: 30, o: 200, g: 300 }
|
|
258
|
-
const list1 = List([
|
|
259
|
-
const list2 = List([
|
|
260
|
-
const array = [
|
|
266
|
+
const list1 = List([1, 2, 3]);
|
|
267
|
+
const list2 = List([4, 5, 6]);
|
|
268
|
+
const array = [7, 8, 9];
|
|
261
269
|
const list3 = list1.concat(list2, array);
|
|
262
270
|
// List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
|
263
271
|
```
|
|
@@ -269,10 +277,13 @@ native API. Because Seq evaluates lazily and does not cache intermediate
|
|
|
269
277
|
results, these operations can be extremely efficient.
|
|
270
278
|
|
|
271
279
|
<!-- runkit:activate -->
|
|
280
|
+
|
|
272
281
|
```js
|
|
273
282
|
const { Seq } = require('immutable');
|
|
274
283
|
const myObject = { a: 1, b: 2, c: 3 };
|
|
275
|
-
Seq(myObject)
|
|
284
|
+
Seq(myObject)
|
|
285
|
+
.map(x => x * x)
|
|
286
|
+
.toObject();
|
|
276
287
|
// { a: 1, b: 4, c: 9 }
|
|
277
288
|
```
|
|
278
289
|
|
|
@@ -281,22 +292,22 @@ JavaScript Object properties are always strings, even if written in a quote-less
|
|
|
281
292
|
shorthand, while Immutable Maps accept keys of any type.
|
|
282
293
|
|
|
283
294
|
<!-- runkit:activate -->
|
|
295
|
+
|
|
284
296
|
```js
|
|
285
297
|
const { fromJS } = require('immutable');
|
|
286
298
|
|
|
287
|
-
const obj = { 1:
|
|
299
|
+
const obj = { 1: 'one' };
|
|
288
300
|
console.log(Object.keys(obj)); // [ "1" ]
|
|
289
|
-
console.log(obj[
|
|
301
|
+
console.log(obj['1'], obj[1]); // "one", "one"
|
|
290
302
|
|
|
291
303
|
const map = fromJS(obj);
|
|
292
|
-
console.log(map.get(
|
|
304
|
+
console.log(map.get('1'), map.get(1)); // "one", undefined
|
|
293
305
|
```
|
|
294
306
|
|
|
295
307
|
Property access for JavaScript Objects first converts the key to a string, but
|
|
296
308
|
since Immutable Map keys can be of any type the argument to `get()` is
|
|
297
309
|
not altered.
|
|
298
310
|
|
|
299
|
-
|
|
300
311
|
### Converts back to raw JavaScript objects.
|
|
301
312
|
|
|
302
313
|
All Immutable.js Collections can be converted to plain JavaScript Arrays and
|
|
@@ -306,9 +317,10 @@ to `JSON.stringify` directly. They also respect the custom `toJSON()` methods of
|
|
|
306
317
|
nested objects.
|
|
307
318
|
|
|
308
319
|
<!-- runkit:activate -->
|
|
320
|
+
|
|
309
321
|
```js
|
|
310
322
|
const { Map, List } = require('immutable');
|
|
311
|
-
const deep = Map({ a: 1, b: 2, c: List([
|
|
323
|
+
const deep = Map({ a: 1, b: 2, c: List([3, 4, 5]) });
|
|
312
324
|
console.log(deep.toObject()); // { a: 1, b: 2, c: List [ 3, 4, 5 ] }
|
|
313
325
|
console.log(deep.toArray()); // [ 1, 2, List [ 3, 4, 5 ] ]
|
|
314
326
|
console.log(deep.toJS()); // { a: 1, b: 2, c: [ 3, 4, 5 ] }
|
|
@@ -318,7 +330,7 @@ JSON.stringify(deep); // '{"a":1,"b":2,"c":[3,4,5]}'
|
|
|
318
330
|
### Embraces ES2015
|
|
319
331
|
|
|
320
332
|
Immutable.js supports all JavaScript environments, including legacy
|
|
321
|
-
browsers (even
|
|
333
|
+
browsers (even IE11). However it also takes advantage of features added to
|
|
322
334
|
JavaScript in [ES2015][], the latest standard version of JavaScript, including
|
|
323
335
|
[Iterators][], [Arrow Functions][], [Classes][], and [Modules][]. It's inspired
|
|
324
336
|
by the native [Map][] and [Set][] collections added to ES2015.
|
|
@@ -330,17 +342,20 @@ browsers, they need to be translated to ES5.
|
|
|
330
342
|
// ES2015
|
|
331
343
|
const mapped = foo.map(x => x * x);
|
|
332
344
|
// ES5
|
|
333
|
-
var mapped = foo.map(function (x) {
|
|
345
|
+
var mapped = foo.map(function (x) {
|
|
346
|
+
return x * x;
|
|
347
|
+
});
|
|
334
348
|
```
|
|
335
349
|
|
|
336
|
-
All Immutable.js collections are [Iterable][
|
|
350
|
+
All Immutable.js collections are [Iterable][iterators], which allows them to be
|
|
337
351
|
used anywhere an Iterable is expected, such as when spreading into an Array.
|
|
338
352
|
|
|
339
353
|
<!-- runkit:activate -->
|
|
354
|
+
|
|
340
355
|
```js
|
|
341
356
|
const { List } = require('immutable');
|
|
342
|
-
const aList = List([
|
|
343
|
-
const anArray = [
|
|
357
|
+
const aList = List([1, 2, 3]);
|
|
358
|
+
const anArray = [0, ...aList, 4, 5]; // [ 0, 1, 2, 3, 4, 5 ]
|
|
344
359
|
```
|
|
345
360
|
|
|
346
361
|
Note: A Collection is always iterated in the same order, however that order may
|
|
@@ -348,20 +363,20 @@ not always be well defined, as is the case for the `Map` and `Set`.
|
|
|
348
363
|
|
|
349
364
|
[Iterators]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol
|
|
350
365
|
[Arrow Functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
|
|
351
|
-
[Classes]:
|
|
352
|
-
[Modules]:
|
|
366
|
+
[Classes]: https://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
|
|
367
|
+
[Modules]: https://www.2ality.com/2014/09/es6-modules-final.html
|
|
353
368
|
|
|
354
369
|
|
|
355
|
-
Nested Structures
|
|
356
|
-
-----------------
|
|
370
|
+
## Nested Structures
|
|
357
371
|
|
|
358
372
|
The collections in Immutable.js are intended to be nested, allowing for deep
|
|
359
373
|
trees of data, similar to JSON.
|
|
360
374
|
|
|
361
375
|
<!-- runkit:activate -->
|
|
376
|
+
|
|
362
377
|
```js
|
|
363
378
|
const { fromJS } = require('immutable');
|
|
364
|
-
const nested = fromJS({ a: { b: { c: [
|
|
379
|
+
const nested = fromJS({ a: { b: { c: [3, 4, 5] } } });
|
|
365
380
|
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ] } } }
|
|
366
381
|
```
|
|
367
382
|
|
|
@@ -370,37 +385,37 @@ most useful are `mergeDeep`, `getIn`, `setIn`, and `updateIn`, found on `List`,
|
|
|
370
385
|
`Map` and `OrderedMap`.
|
|
371
386
|
|
|
372
387
|
<!-- runkit:activate -->
|
|
388
|
+
|
|
373
389
|
```js
|
|
374
390
|
const { fromJS } = require('immutable');
|
|
375
|
-
const nested = fromJS({ a: { b: { c: [
|
|
391
|
+
const nested = fromJS({ a: { b: { c: [3, 4, 5] } } });
|
|
376
392
|
|
|
377
393
|
const nested2 = nested.mergeDeep({ a: { b: { d: 6 } } });
|
|
378
394
|
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ], d: 6 } } }
|
|
379
395
|
|
|
380
|
-
console.log(nested2.getIn([
|
|
396
|
+
console.log(nested2.getIn(['a', 'b', 'd'])); // 6
|
|
381
397
|
|
|
382
|
-
const nested3 = nested2.updateIn([
|
|
398
|
+
const nested3 = nested2.updateIn(['a', 'b', 'd'], value => value + 1);
|
|
383
399
|
console.log(nested3);
|
|
384
400
|
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ], d: 7 } } }
|
|
385
401
|
|
|
386
|
-
const nested4 = nested3.updateIn([
|
|
402
|
+
const nested4 = nested3.updateIn(['a', 'b', 'c'], list => list.push(6));
|
|
387
403
|
// Map { a: Map { b: Map { c: List [ 3, 4, 5, 6 ], d: 7 } } }
|
|
388
404
|
```
|
|
389
405
|
|
|
406
|
+
## Equality treats Collections as Values
|
|
390
407
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
Immutable.js collections are treated as pure data *values*. Two immutable
|
|
395
|
-
collections are considered *value equal* (via `.equals()` or `is()`) if they
|
|
408
|
+
Immutable.js collections are treated as pure data _values_. Two immutable
|
|
409
|
+
collections are considered _value equal_ (via `.equals()` or `is()`) if they
|
|
396
410
|
represent the same collection of values. This differs from JavaScript's typical
|
|
397
|
-
|
|
411
|
+
_reference equal_ (via `===` or `==`) for Objects and Arrays which only
|
|
398
412
|
determines if two variables represent references to the same object instance.
|
|
399
413
|
|
|
400
414
|
Consider the example below where two identical `Map` instances are not
|
|
401
|
-
|
|
415
|
+
_reference equal_ but are _value equal_.
|
|
402
416
|
|
|
403
417
|
<!-- runkit:activate -->
|
|
418
|
+
|
|
404
419
|
```js
|
|
405
420
|
// First consider:
|
|
406
421
|
const obj1 = { a: 1, b: 2, c: 3 };
|
|
@@ -419,6 +434,7 @@ Value equality allows Immutable.js collections to be used as keys in Maps or
|
|
|
419
434
|
values in Sets, and retrieved with different but equivalent collections:
|
|
420
435
|
|
|
421
436
|
<!-- runkit:activate -->
|
|
437
|
+
|
|
422
438
|
```js
|
|
423
439
|
const { Map, Set } = require('immutable');
|
|
424
440
|
const map1 = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -432,7 +448,7 @@ strings and numbers, but uses value equality for Immutable collections,
|
|
|
432
448
|
determining if both are immutable and all keys and values are equal
|
|
433
449
|
using the same measure of equality.
|
|
434
450
|
|
|
435
|
-
[
|
|
451
|
+
[object.is]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
436
452
|
|
|
437
453
|
#### Performance tradeoffs
|
|
438
454
|
|
|
@@ -454,10 +470,11 @@ out the possibility that they may be value-equal.
|
|
|
454
470
|
#### Return self on no-op optimization
|
|
455
471
|
|
|
456
472
|
When possible, Immutable.js avoids creating new objects for updates where no
|
|
457
|
-
change in
|
|
473
|
+
change in _value_ occurred, to allow for efficient _reference equality_ checking
|
|
458
474
|
to quickly determine if no change occurred.
|
|
459
475
|
|
|
460
476
|
<!-- runkit:activate -->
|
|
477
|
+
|
|
461
478
|
```js
|
|
462
479
|
const { Map } = require('immutable');
|
|
463
480
|
const originalMap = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -470,6 +487,7 @@ of these operations occur independently, so two similar updates will not return
|
|
|
470
487
|
the same reference:
|
|
471
488
|
|
|
472
489
|
<!-- runkit:activate -->
|
|
490
|
+
|
|
473
491
|
```js
|
|
474
492
|
const { Map } = require('immutable');
|
|
475
493
|
const originalMap = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -483,8 +501,7 @@ anotherUpdatedMap !== updatedMap;
|
|
|
483
501
|
anotherUpdatedMap.equals(updatedMap);
|
|
484
502
|
```
|
|
485
503
|
|
|
486
|
-
Batching Mutations
|
|
487
|
-
------------------
|
|
504
|
+
## Batching Mutations
|
|
488
505
|
|
|
489
506
|
> If a tree falls in the woods, does it make a sound?
|
|
490
507
|
>
|
|
@@ -498,15 +515,16 @@ which can add up to a minor performance penalty. If you need to apply a series
|
|
|
498
515
|
of mutations locally before returning, Immutable.js gives you the ability to
|
|
499
516
|
create a temporary mutable (transient) copy of a collection and apply a batch of
|
|
500
517
|
mutations in a performant manner by using `withMutations`. In fact, this is
|
|
501
|
-
exactly how
|
|
518
|
+
exactly how Immutable.js applies complex mutations itself.
|
|
502
519
|
|
|
503
520
|
As an example, building `list2` results in the creation of 1, not 3, new
|
|
504
521
|
immutable Lists.
|
|
505
522
|
|
|
506
523
|
<!-- runkit:activate -->
|
|
524
|
+
|
|
507
525
|
```js
|
|
508
526
|
const { List } = require('immutable');
|
|
509
|
-
const list1 = List([
|
|
527
|
+
const list1 = List([1, 2, 3]);
|
|
510
528
|
const list2 = list1.withMutations(function (list) {
|
|
511
529
|
list.push(4).push(5).push(6);
|
|
512
530
|
});
|
|
@@ -518,15 +536,13 @@ Note: Immutable.js also provides `asMutable` and `asImmutable`, but only
|
|
|
518
536
|
encourages their use when `withMutations` will not suffice. Use caution to not
|
|
519
537
|
return a mutable copy, which could result in undesired behavior.
|
|
520
538
|
|
|
521
|
-
|
|
539
|
+
_Important!_: Only a select few methods can be used in `withMutations` including
|
|
522
540
|
`set`, `push` and `pop`. These methods can be applied directly against a
|
|
523
541
|
persistent data-structure where other methods like `map`, `filter`, `sort`,
|
|
524
542
|
and `splice` will always return new immutable data-structures and never mutate
|
|
525
543
|
a mutable collection.
|
|
526
544
|
|
|
527
|
-
|
|
528
|
-
Lazy Seq
|
|
529
|
-
--------
|
|
545
|
+
## Lazy Seq
|
|
530
546
|
|
|
531
547
|
`Seq` describes a lazy operation, allowing them to efficiently chain
|
|
532
548
|
use of all the higher-order collection methods (such as `map` and `filter`)
|
|
@@ -546,7 +562,7 @@ For example, the following performs no work, because the resulting
|
|
|
546
562
|
|
|
547
563
|
```js
|
|
548
564
|
const { Seq } = require('immutable');
|
|
549
|
-
const oddSquares = Seq([
|
|
565
|
+
const oddSquares = Seq([1, 2, 3, 4, 5, 6, 7, 8])
|
|
550
566
|
.filter(x => x % 2 !== 0)
|
|
551
567
|
.map(x => x * x);
|
|
552
568
|
```
|
|
@@ -562,6 +578,7 @@ oddSquares.get(1); // 9
|
|
|
562
578
|
Any collection can be converted to a lazy Seq with `Seq()`.
|
|
563
579
|
|
|
564
580
|
<!-- runkit:activate -->
|
|
581
|
+
|
|
565
582
|
```js
|
|
566
583
|
const { Map, Seq } = require('immutable');
|
|
567
584
|
const map = Map({ a: 1, b: 2, c: 3 });
|
|
@@ -576,13 +593,14 @@ lazySeq
|
|
|
576
593
|
.flip()
|
|
577
594
|
.map(key => key.toUpperCase())
|
|
578
595
|
.flip();
|
|
579
|
-
// Seq { A: 1, B:
|
|
596
|
+
// Seq { A: 1, B: 2, C: 3 }
|
|
580
597
|
```
|
|
581
598
|
|
|
582
599
|
As well as expressing logic that would otherwise seem memory or time
|
|
583
600
|
limited, for example `Range` is a special kind of Lazy sequence.
|
|
584
601
|
|
|
585
602
|
<!-- runkit:activate -->
|
|
603
|
+
|
|
586
604
|
```js
|
|
587
605
|
const { Range } = require('immutable');
|
|
588
606
|
Range(1, Infinity)
|
|
@@ -594,41 +612,39 @@ Range(1, Infinity)
|
|
|
594
612
|
// 1006008
|
|
595
613
|
```
|
|
596
614
|
|
|
615
|
+
## Documentation
|
|
597
616
|
|
|
598
|
-
Documentation
|
|
599
|
-
-------------
|
|
617
|
+
## Documentation
|
|
600
618
|
|
|
601
|
-
[Read the docs](
|
|
619
|
+
[Read the docs](https://immutable-js.com) and eat your vegetables.
|
|
602
620
|
|
|
603
|
-
Docs are automatically generated from [Immutable.d.ts](https://github.com/
|
|
621
|
+
Docs are automatically generated from [Immutable.d.ts](https://github.com/immutable-js/immutable-js/blob/main/type-definitions/Immutable.d.ts).
|
|
604
622
|
Please contribute!
|
|
605
623
|
|
|
606
|
-
Also, don't miss the [Wiki](https://github.com/
|
|
607
|
-
contains articles on specific topics. Can't find something? Open an [issue](https://github.com/
|
|
624
|
+
Also, don't miss the [Wiki](https://github.com/immutable-js/immutable-js/wiki) which
|
|
625
|
+
contains articles on specific topics. Can't find something? Open an [issue](https://github.com/immutable-js/immutable-js/issues).
|
|
608
626
|
|
|
627
|
+
## Testing
|
|
609
628
|
|
|
610
|
-
|
|
611
|
-
-------
|
|
629
|
+
If you are using the [Chai Assertion Library](https://chaijs.com/), [Chai Immutable](https://github.com/astorije/chai-immutable) provides a set of assertions to use against Immutable.js collections.
|
|
612
630
|
|
|
613
|
-
|
|
631
|
+
## Contribution
|
|
614
632
|
|
|
633
|
+
## Contribution
|
|
615
634
|
|
|
616
|
-
|
|
617
|
-
------------
|
|
635
|
+
Use [Github issues](https://github.com/immutable-js/immutable-js/issues) for requests.
|
|
618
636
|
|
|
619
|
-
|
|
637
|
+
We actively welcome pull requests, learn how to [contribute](https://github.com/immutable-js/immutable-js/blob/main/.github/CONTRIBUTING.md).
|
|
620
638
|
|
|
621
|
-
|
|
639
|
+
Immutable.js is maintained within the [Contributor Covenant's Code of Conduct](https://www.contributor-covenant.org/version/2/0/code_of_conduct/).
|
|
622
640
|
|
|
641
|
+
We actively welcome pull requests, learn how to [contribute](https://github.com/immutable-js/immutable-js/blob/main/.github/CONTRIBUTING.md).
|
|
623
642
|
|
|
624
|
-
Changelog
|
|
625
|
-
---------
|
|
643
|
+
## Changelog
|
|
626
644
|
|
|
627
|
-
Changes are tracked as [Github releases](https://github.com/
|
|
645
|
+
Changes are tracked as [Github releases](https://github.com/immutable-js/immutable-js/releases).
|
|
628
646
|
|
|
629
|
-
|
|
630
|
-
Thanks
|
|
631
|
-
------
|
|
647
|
+
## Thanks
|
|
632
648
|
|
|
633
649
|
[Phil Bagwell](https://www.youtube.com/watch?v=K2NYwP90bNs), for his inspiration
|
|
634
650
|
and research in persistent data structures.
|
|
@@ -636,8 +652,6 @@ and research in persistent data structures.
|
|
|
636
652
|
[Hugh Jackson](https://github.com/hughfdjackson/), for providing the npm package
|
|
637
653
|
name. If you're looking for his unsupported package, see [this repository](https://github.com/hughfdjackson/immutable).
|
|
638
654
|
|
|
655
|
+
## License
|
|
639
656
|
|
|
640
|
-
|
|
641
|
-
-------
|
|
642
|
-
|
|
643
|
-
Immutable.js is [MIT-licensed](https://github.com/facebook/immutable-js/blob/master/LICENSE).
|
|
657
|
+
Immutable.js is [MIT-licensed](./LICENSE).
|