Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 784d648

Browse files
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: seamless-immutable.development.js seamless-immutable.development.min.js seamless-immutable.production.min.js src/seamless-immutable.js test/Immutable.spec.js
2 parents 70b2220 + 1c945eb commit 784d648

11 files changed

+337
-8
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ Immutable(new Square(2), {prototype: Square.prototype}).area();
155155

156156
Beyond [the usual Object fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_Object_instances), the following methods have been added.
157157

158+
### Stack overflow protection
159+
160+
Currently you can't construct Immutable from an object with circular references. To protect from ugly stack overflows, we provide a simple protection during development. We stop at a suspiciously deep stack level and [show an error message][deep].
161+
162+
If your objects are deep, but not circular, you can increase this level from default `64`. For example:
163+
164+
```javascript
165+
Immutable(deepObject, null, 256);
166+
```
167+
168+
This check is not performed in the production build.
169+
170+
[deep]: https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected
171+
158172
### merge
159173

160174
```javascript
@@ -199,6 +213,34 @@ Immutable({type: {main: "parrot", sub: "Norwegian Blue"}, status: "alive"}).setI
199213
// returns Immutable({type: {main: "parrot", sub: "Norwegian Ridgeback"}, status: "alive"})
200214
```
201215

216+
### update
217+
218+
Returns an Immutable Object with a single property updated using the provided updater function.
219+
220+
```javascript
221+
function inc (x) { return x + 1 }
222+
Immutable({foo: 1}).update("foo", inc)
223+
// returns Immutable({foo: 2})
224+
```
225+
226+
All additional arguments will be passed to the updater function.
227+
228+
```javascript
229+
function add (x, y) { return x + y }
230+
Immutable({foo: 1}).update("foo", add, 10)
231+
// returns Immutable({foo: 11})
232+
```
233+
234+
### updateIn
235+
236+
Like [update](#update), but accepts a nested path to the property.
237+
238+
```javascript
239+
function add (x, y) { return x + y }
240+
Immutable({foo: {bar: 1}}).updateIn(["foo", "bar"], add, 10)
241+
// returns Immutable({foo: {bar: 11}})
242+
```
243+
202244
### without
203245

204246
```javascript
@@ -234,6 +276,14 @@ Returns a mutable copy of the object. For a deeply mutable copy, in which any in
234276

235277
### Releases
236278

279+
#### 6.0.0
280+
281+
Add cycle detection.
282+
283+
#### 5.2.0
284+
285+
Add `update` and `updateIn`.
286+
237287
#### 5.1.1
238288

239289
`Immutable(Object.create(null))` now works as expected.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "seamless-immutable",
3-
"version": "5.1.1",
3+
"version": "6.0.0",
44
"description": "Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.",
55
"main": "src/seamless-immutable.js",
66
"devDependencies": {

seamless-immutable.development.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
addPropertyTo(array, "asMutable", asMutableArray);
156156
addPropertyTo(array, "set", arraySet);
157157
addPropertyTo(array, "setIn", arraySetIn);
158+
addPropertyTo(array, "update", update);
159+
addPropertyTo(array, "updateIn", updateIn);
158160

159161
for(var i = 0, length = array.length; i < length; i++) {
160162
array[i] = Immutable(array[i]);
@@ -434,6 +436,28 @@
434436
return makeImmutableObject(mutable, this);
435437
}
436438

439+
function update(property, updater) {
440+
var restArgs = Array.prototype.slice.call(arguments, 2);
441+
var initialVal = this[property];
442+
return this.set(property, updater.apply(initialVal, [initialVal].concat(restArgs)));
443+
}
444+
445+
function getInPath(obj, path) {
446+
/*jshint eqnull:true */
447+
for (var i = 0, l = path.length; obj != null && i < l; i++) {
448+
obj = obj[path[i]];
449+
}
450+
451+
return (i && i == l) ? obj : undefined;
452+
}
453+
454+
function updateIn(path, updater) {
455+
var restArgs = Array.prototype.slice.call(arguments, 2);
456+
var initialVal = getInPath(this, path);
457+
458+
return this.setIn(path, updater.apply(initialVal, [initialVal].concat(restArgs)));
459+
}
460+
437461
function asMutableObject(opts) {
438462
var result = this.instantiateEmptyObject(), key;
439463

@@ -471,6 +495,8 @@
471495
addPropertyTo(obj, "instantiateEmptyObject", instantiateEmptyObject);
472496
addPropertyTo(obj, "set", objectSet);
473497
addPropertyTo(obj, "setIn", objectSetIn);
498+
addPropertyTo(obj, "update", update);
499+
addPropertyTo(obj, "updateIn", updateIn);
474500

475501
return makeImmutable(obj, mutatingObjectMethods);
476502
}
@@ -483,7 +509,7 @@
483509
(obj.$$typeof === REACT_ELEMENT_TYPE_FALLBACK || obj.$$typeof === REACT_ELEMENT_TYPE);
484510
}
485511

486-
function Immutable(obj, options) {
512+
function Immutable(obj, options, stackRemaining) {
487513
if (isImmutable(obj) || isReactElement(obj)) {
488514
return obj;
489515
} else if (obj instanceof Array) {
@@ -498,9 +524,22 @@
498524
instantiatePlainObject : (function() { return Object.create(prototype); });
499525
var clone = instantiateEmptyObject();
500526

527+
if ("development" !== "production") {
528+
/*jshint eqnull:true */
529+
if (stackRemaining == null) {
530+
stackRemaining = 64;
531+
}
532+
if (stackRemaining <= 0) {
533+
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
534+
" Have you tried to wrap an object with circular references (e.g. React element)?" +
535+
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
536+
}
537+
stackRemaining -= 1;
538+
}
539+
501540
for (var key in obj) {
502541
if (Object.getOwnPropertyDescriptor(obj, key)) {
503-
clone[key] = Immutable(obj[key]);
542+
clone[key] = Immutable(obj[key], undefined, stackRemaining);
504543
}
505544
}
506545

seamless-immutable.development.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)