function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}n/a
function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function Error() { [native code] }n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else if (self instanceof Date) {
return fnDate.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
You can also call `.asObject` without passing an iterator, in which case it will proceed assuming the Array
is already organized as desired.
### asMutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = Immutable.asMutable(array);
mutableArray.push("!!!");
mutableArray // ["hello", "world", "!!!"]
```
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, except that whenever the provided
iterator function returns an Array, that Array's elements are each added to the final result.
### asObject
```javascript
var array = Immutable(["hey", "you"]);
Immutable.asObject(array, function(str) {
return [str, str.toUpperCase()];
});
// returns Immutable({hey: "HEY", you: "YOU"})
```
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, expecting that the iterator function
will return an array of two elements - the first representing a key, the other
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Beyond [the usual Array fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessor_methods
), the following methods have been added.
### flatMap
```javascript
var array = Immutable(["here", "we", "go"]);
Immutable.flatMap(array, function(str) {
return [str, str, str];
});
// returns Immutable(["here", "here", "here", "we", "we", "we", "
;go", "go", "go"])
var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
Immutable.flatMap(array, function(value) {
if (typeof value === "number") {
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}...
## Immutable.from
If your linter cringes with the use of `Immutable` without a preceding `new`
(e.g. ESLint's [new-cap](http://eslint.org/docs/rules/new-cap) rule),
use `Immutable.from`:
```javascript
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
```
## Immutable Array
Like a regular Array, but immutable! You can construct these by passing
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### getIn
Returns the value at the given path. A default value can be provided as a second argument.
```javascript
var obj = Immutable({type: {main: "parrot", subtype: "Norwegian Blue"}, status: "alive"});
Immutable.getIn(obj, ["type", "subtype"]);
// returns "Norwegian Blue"
Immutable.getIn(obj, ["type", "class"], "Aves");
// returns "Aves"
```
### update
...function isImmutable(target) {
if (typeof target === "object") {
return target === null || Boolean(
Object.getOwnPropertyDescriptor(target, immutabilityTag)
);
} else {
// In JavaScript, only objects are even potentially mutable.
// strings, numbers, null, and undefined are all naturally immutable.
return true;
}
}...
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
### isImmutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = ["hello", "world"];
Immutable.isImmutable(array)
// returns true
Immutable.isImmutable(mutableArray)
// returns false
```
Returns whether an object is immutable or not.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
[1, 2, 3].concat(Immutable([6, 5, 4])).sort()
// This will succeed, and will yield a sorted mutable array containing
// [1, 2, 3, 4, 5, 6], because a vanilla array's concat() method has
// no knowledge of Immutable.
var obj = Immutable({all: "your base", are: {belong: "to them"}});
Immutable.merge(obj, {are: {belong: "to us"}})
// This will return the following:
// Immutable({all: "your base", are: {belong: "to us"}})
```
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
A third argument can be provided to perform a deep merge: `{deep: true}`.
### replace
```javascript
var obj1 = Immutable({a: {b: 'test'}, c: 'test'});
var obj2 = Immutable.replace(obj1, {a: {b: 'test'}}, {deep: true});
// returns Immutable({a: {b: 'test'}});
obj1 === obj2
// returns false
obj1.a === obj2.a
// returns true because child .a objects were identical
```
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if ("development" !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
A second argument can be provided to perform a deep merge: `{deep: true}`.
### set
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
Immutable.set(obj, "status", "dead");
// returns Immutable({type: "parrot", subtype: "Norwegian Blue", status: "dead"})
```
Returns an Immutable Object with a single property set to the provided value.
Basically a more straightforward way of saying
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
```
var Immutable = require("seamless-immutable").static;
Immutable.setIn(obj, 'key', data)
```
```
var Immutable = require("seamless-immutable");
obj.setIn('key', data)
```
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}n/a
function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### update
Returns an Immutable Object with a single property updated using the provided updater function.
```javascript
function inc (x) { return x + 1 }
var obj = Immutable({foo: 1});
Immutable.update(obj, "foo", inc);
// returns Immutable({foo: 2})
```
All additional arguments will be passed to the updater function.
```javascript
function add (x, y) { return x + y }
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### updateIn
Like [update](#update), but accepts a nested path to the property.
```javascript
function add (x, y) { return x + y }
var obj = Immutable({foo: {bar: 1}});
Immutable.updateIn(obj, ["foo", "bar"], add, 10);
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, "with");
// returns Immutable({the: "forests", will: "echo"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, ["will", "with"]);
// returns Immutable({the: "forests"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function Error() { [native code] }n/a
function Function() { [native code] }n/a
function captureStackTrace() { [native code] }n/a
function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}...
## Immutable.from
If your linter cringes with the use of `Immutable` without a preceding `new`
(e.g. ESLint's [new-cap](http://eslint.org/docs/rules/new-cap) rule),
use `Immutable.from`:
```javascript
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
```
## Immutable Array
Like a regular Array, but immutable! You can construct these by passing
...function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else if (self instanceof Date) {
return fnDate.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
You can also call `.asObject` without passing an iterator, in which case it will proceed assuming the Array
is already organized as desired.
### asMutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = Immutable.asMutable(array);
mutableArray.push("!!!");
mutableArray // ["hello", "world", "!!!"]
```
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, except that whenever the provided
iterator function returns an Array, that Array's elements are each added to the final result.
### asObject
```javascript
var array = Immutable(["hey", "you"]);
Immutable.asObject(array, function(str) {
return [str, str.toUpperCase()];
});
// returns Immutable({hey: "HEY", you: "YOU"})
```
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, expecting that the iterator function
will return an array of two elements - the first representing a key, the other
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Beyond [the usual Array fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessor_methods
), the following methods have been added.
### flatMap
```javascript
var array = Immutable(["here", "we", "go"]);
Immutable.flatMap(array, function(str) {
return [str, str, str];
});
// returns Immutable(["here", "here", "here", "we", "we", "we", "
;go", "go", "go"])
var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
Immutable.flatMap(array, function(value) {
if (typeof value === "number") {
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### getIn
Returns the value at the given path. A default value can be provided as a second argument.
```javascript
var obj = Immutable({type: {main: "parrot", subtype: "Norwegian Blue"}, status: "alive"});
Immutable.getIn(obj, ["type", "subtype"]);
// returns "Norwegian Blue"
Immutable.getIn(obj, ["type", "class"], "Aves");
// returns "Aves"
```
### update
...function isImmutable(target) {
if (typeof target === "object") {
return target === null || Boolean(
Object.getOwnPropertyDescriptor(target, immutabilityTag)
);
} else {
// In JavaScript, only objects are even potentially mutable.
// strings, numbers, null, and undefined are all naturally immutable.
return true;
}
}...
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
### isImmutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = ["hello", "world"];
Immutable.isImmutable(array)
// returns true
Immutable.isImmutable(mutableArray)
// returns false
```
Returns whether an object is immutable or not.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
[1, 2, 3].concat(Immutable([6, 5, 4])).sort()
// This will succeed, and will yield a sorted mutable array containing
// [1, 2, 3, 4, 5, 6], because a vanilla array's concat() method has
// no knowledge of Immutable.
var obj = Immutable({all: "your base", are: {belong: "to them"}});
Immutable.merge(obj, {are: {belong: "to us"}})
// This will return the following:
// Immutable({all: "your base", are: {belong: "to us"}})
```
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
A third argument can be provided to perform a deep merge: `{deep: true}`.
### replace
```javascript
var obj1 = Immutable({a: {b: 'test'}, c: 'test'});
var obj2 = Immutable.replace(obj1, {a: {b: 'test'}}, {deep: true});
// returns Immutable({a: {b: 'test'}});
obj1 === obj2
// returns false
obj1.a === obj2.a
// returns true because child .a objects were identical
```
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
A second argument can be provided to perform a deep merge: `{deep: true}`.
### set
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
Immutable.set(obj, "status", "dead");
// returns Immutable({type: "parrot", subtype: "Norwegian Blue", status: "dead"})
```
Returns an Immutable Object with a single property set to the provided value.
Basically a more straightforward way of saying
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
```
var Immutable = require("seamless-immutable").static;
Immutable.setIn(obj, 'key', data)
```
```
var Immutable = require("seamless-immutable");
obj.setIn('key', data)
```
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### update
Returns an Immutable Object with a single property updated using the provided updater function.
```javascript
function inc (x) { return x + 1 }
var obj = Immutable({foo: 1});
Immutable.update(obj, "foo", inc);
// returns Immutable({foo: 2})
```
All additional arguments will be passed to the updater function.
```javascript
function add (x, y) { return x + y }
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### updateIn
Like [update](#update), but accepts a nested path to the property.
```javascript
function add (x, y) { return x + y }
var obj = Immutable({foo: {bar: 1}});
Immutable.updateIn(obj, ["foo", "bar"], add, 10);
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, "with");
// returns Immutable({the: "forests", will: "echo"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, ["will", "with"]);
// returns Immutable({the: "forests"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else if (self instanceof Date) {
return fnDate.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
You can also call `.asObject` without passing an iterator, in which case it will proceed assuming the Array
is already organized as desired.
### asMutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = Immutable.asMutable(array);
mutableArray.push("!!!");
mutableArray // ["hello", "world", "!!!"]
```
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, except that whenever the provided
iterator function returns an Array, that Array's elements are each added to the final result.
### asObject
```javascript
var array = Immutable(["hey", "you"]);
Immutable.asObject(array, function(str) {
return [str, str.toUpperCase()];
});
// returns Immutable({hey: "HEY", you: "YOU"})
```
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, expecting that the iterator function
will return an array of two elements - the first representing a key, the other
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Beyond [the usual Array fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessor_methods
), the following methods have been added.
### flatMap
```javascript
var array = Immutable(["here", "we", "go"]);
Immutable.flatMap(array, function(str) {
return [str, str, str];
});
// returns Immutable(["here", "here", "here", "we", "we", "we", "
;go", "go", "go"])
var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
Immutable.flatMap(array, function(value) {
if (typeof value === "number") {
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if ("development" !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}...
## Immutable.from
If your linter cringes with the use of `Immutable` without a preceding `new`
(e.g. ESLint's [new-cap](http://eslint.org/docs/rules/new-cap) rule),
use `Immutable.from`:
```javascript
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
```
## Immutable Array
Like a regular Array, but immutable! You can construct these by passing
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### getIn
Returns the value at the given path. A default value can be provided as a second argument.
```javascript
var obj = Immutable({type: {main: "parrot", subtype: "Norwegian Blue"}, status: "alive"});
Immutable.getIn(obj, ["type", "subtype"]);
// returns "Norwegian Blue"
Immutable.getIn(obj, ["type", "class"], "Aves");
// returns "Aves"
```
### update
...function isImmutable(target) {
if (typeof target === "object") {
return target === null || Boolean(
Object.getOwnPropertyDescriptor(target, immutabilityTag)
);
} else {
// In JavaScript, only objects are even potentially mutable.
// strings, numbers, null, and undefined are all naturally immutable.
return true;
}
}...
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
### isImmutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = ["hello", "world"];
Immutable.isImmutable(array)
// returns true
Immutable.isImmutable(mutableArray)
// returns false
```
Returns whether an object is immutable or not.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
[1, 2, 3].concat(Immutable([6, 5, 4])).sort()
// This will succeed, and will yield a sorted mutable array containing
// [1, 2, 3, 4, 5, 6], because a vanilla array's concat() method has
// no knowledge of Immutable.
var obj = Immutable({all: "your base", are: {belong: "to them"}});
Immutable.merge(obj, {are: {belong: "to us"}})
// This will return the following:
// Immutable({all: "your base", are: {belong: "to us"}})
```
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
A third argument can be provided to perform a deep merge: `{deep: true}`.
### replace
```javascript
var obj1 = Immutable({a: {b: 'test'}, c: 'test'});
var obj2 = Immutable.replace(obj1, {a: {b: 'test'}}, {deep: true});
// returns Immutable({a: {b: 'test'}});
obj1 === obj2
// returns false
obj1.a === obj2.a
// returns true because child .a objects were identical
```
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
A second argument can be provided to perform a deep merge: `{deep: true}`.
### set
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
Immutable.set(obj, "status", "dead");
// returns Immutable({type: "parrot", subtype: "Norwegian Blue", status: "dead"})
```
Returns an Immutable Object with a single property set to the provided value.
Basically a more straightforward way of saying
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
```
var Immutable = require("seamless-immutable").static;
Immutable.setIn(obj, 'key', data)
```
```
var Immutable = require("seamless-immutable");
obj.setIn('key', data)
```
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if ("development" !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### update
Returns an Immutable Object with a single property updated using the provided updater function.
```javascript
function inc (x) { return x + 1 }
var obj = Immutable({foo: 1});
Immutable.update(obj, "foo", inc);
// returns Immutable({foo: 2})
```
All additional arguments will be passed to the updater function.
```javascript
function add (x, y) { return x + y }
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### updateIn
Like [update](#update), but accepts a nested path to the property.
```javascript
function add (x, y) { return x + y }
var obj = Immutable({foo: {bar: 1}});
Immutable.updateIn(obj, ["foo", "bar"], add, 10);
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, "with");
// returns Immutable({the: "forests", will: "echo"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, ["will", "with"]);
// returns Immutable({the: "forests"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else if (self instanceof Date) {
return fnDate.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
You can also call `.asObject` without passing an iterator, in which case it will proceed assuming the Array
is already organized as desired.
### asMutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = Immutable.asMutable(array);
mutableArray.push("!!!");
mutableArray // ["hello", "world", "!!!"]
```
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, except that whenever the provided
iterator function returns an Array, that Array's elements are each added to the final result.
### asObject
```javascript
var array = Immutable(["hey", "you"]);
Immutable.asObject(array, function(str) {
return [str, str.toUpperCase()];
});
// returns Immutable({hey: "HEY", you: "YOU"})
```
Effectively performs a [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) over the
elements in the array, expecting that the iterator function
will return an array of two elements - the first representing a key, the other
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
Beyond [the usual Array fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessor_methods
), the following methods have been added.
### flatMap
```javascript
var array = Immutable(["here", "we", "go"]);
Immutable.flatMap(array, function(str) {
return [str, str, str];
});
// returns Immutable(["here", "here", "here", "we", "we", "we", "
;go", "go", "go"])
var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
Immutable.flatMap(array, function(value) {
if (typeof value === "number") {
...function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj) || isReactElement(obj) || isFileObject(obj) || isError(obj)) {
return obj;
} else if (isPromise(obj)) {
return obj.then(Immutable);
} else if (Array.isArray(obj)) {
return makeImmutableArray(obj.slice());
} else if (obj instanceof Date) {
return makeImmutableDate(new Date(obj.getTime()));
} else {
// Don't freeze the object we were given; make a clone and use that.
var prototype = options && options.prototype;
var instantiateEmptyObject =
(!prototype || prototype === Object.prototype) ?
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();
if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}
if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}
for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}
return makeImmutableObject(clone);
}
}...
## Immutable.from
If your linter cringes with the use of `Immutable` without a preceding `new`
(e.g. ESLint's [new-cap](http://eslint.org/docs/rules/new-cap) rule),
use `Immutable.from`:
```javascript
Immutable.from([1, 2, 3]);
// is functionally the same as calling:
Immutable([1, 2, 3])
```
## Immutable Array
Like a regular Array, but immutable! You can construct these by passing
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### getIn
Returns the value at the given path. A default value can be provided as a second argument.
```javascript
var obj = Immutable({type: {main: "parrot", subtype: "Norwegian Blue"}, status: "alive"});
Immutable.getIn(obj, ["type", "subtype"]);
// returns "Norwegian Blue"
Immutable.getIn(obj, ["type", "class"], "Aves");
// returns "Aves"
```
### update
...function isImmutable(target) {
if (typeof target === "object") {
return target === null || Boolean(
Object.getOwnPropertyDescriptor(target, immutabilityTag)
);
} else {
// In JavaScript, only objects are even potentially mutable.
// strings, numbers, null, and undefined are all naturally immutable.
return true;
}
}...
Returns a mutable copy of the array. For a deeply mutable copy, in which any instances of `Immutable` contained in nested data structures
within the array have been converted back to mutable data structures, call `Immutable.asMutable(obj, {deep: true})` instead.
### isImmutable
```javascript
var array = Immutable(["hello", "world"]);
var mutableArray = ["hello", "world"];
Immutable.isImmutable(array)
// returns true
Immutable.isImmutable(mutableArray)
// returns false
```
Returns whether an object is immutable or not.
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
[1, 2, 3].concat(Immutable([6, 5, 4])).sort()
// This will succeed, and will yield a sorted mutable array containing
// [1, 2, 3, 4, 5, 6], because a vanilla array's concat() method has
// no knowledge of Immutable.
var obj = Immutable({all: "your base", are: {belong: "to them"}});
Immutable.merge(obj, {are: {belong: "to us"}})
// This will return the following:
// Immutable({all: "your base", are: {belong: "to us"}})
```
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
A third argument can be provided to perform a deep merge: `{deep: true}`.
### replace
```javascript
var obj1 = Immutable({a: {b: 'test'}, c: 'test'});
var obj2 = Immutable.replace(obj1, {a: {b: 'test'}}, {deep: true});
// returns Immutable({a: {b: 'test'}});
obj1 === obj2
// returns false
obj1.a === obj2.a
// returns true because child .a objects were identical
```
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
A second argument can be provided to perform a deep merge: `{deep: true}`.
### set
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
Immutable.set(obj, "status", "dead");
// returns Immutable({type: "parrot", subtype: "Norwegian Blue", status: "dead"})
```
Returns an Immutable Object with a single property set to the provided value.
Basically a more straightforward way of saying
```javascript
var obj = Immutable({type: "parrot", subtype: "Norwegian Blue", status: "alive"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
if (Array.isArray(self)) {
return fnArray.apply(self, args);
} else {
return fnObject.apply(self, args);
}
}...
## Static or instance syntax
Seamless-immutable supports both static and instance syntaxes:
```
var Immutable = require("seamless-immutable").static;
Immutable.setIn(obj, 'key', data)
```
```
var Immutable = require("seamless-immutable");
obj.setIn('key', data)
```
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### update
Returns an Immutable Object with a single property updated using the provided updater function.
```javascript
function inc (x) { return x + 1 }
var obj = Immutable({foo: 1});
Immutable.update(obj, "foo", inc);
// returns Immutable({foo: 2})
```
All additional arguments will be passed to the updater function.
```javascript
function add (x, y) { return x + y }
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
### updateIn
Like [update](#update), but accepts a nested path to the property.
```javascript
function add (x, y) { return x + y }
var obj = Immutable({foo: {bar: 1}});
Immutable.updateIn(obj, ["foo", "bar"], add, 10);
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function staticWrapper() {
var args = [].slice.call(arguments);
var self = args.shift();
return fn.apply(self, args);
}...
// returns Immutable({foo: {bar: 11}})
```
### without
```javascript
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, "with");
// returns Immutable({the: "forests", will: "echo"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
Immutable.without(obj, ["will", "with"]);
// returns Immutable({the: "forests"})
var obj = Immutable({the: "forests", will: "echo", with: "laughter"});
...function ImmutableError(message) {
this.name = 'MyError';
this.message = message;
this.stack = (new Error()).stack;
}n/a
function Error() { [native code] }n/a