seamless-immutable
Advanced tools
Comparing version 2.2.0 to 2.3.0
{ | ||
"name": "seamless-immutable", | ||
"main": "seamless-immutable.js", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"homepage": "https://github.com/rtfeldman/seamless-immutable", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "seamless-immutable", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.", | ||
@@ -5,0 +5,0 @@ "main": "seamless-immutable.development.js", |
@@ -35,4 +35,6 @@ seamless-immutable | ||
Note that these objects are [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze), and [Safari is relatively slow to iterate over frozen objects](http://jsperf.com/performance-frozen-object/20). If this makes a noticeable difference in your use case, you can monkey patch `Object.freeze` to be [the identity function](http://stackoverflow.com/questions/11485508/use-of-the-identity-function-in-javascript) before using `Immutable`. If you do, be aware that this carries the drawback of re-enabling property reassignment like `array[2] = "new value"`. | ||
In the development build, objects are [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze). (Note that [Safari is relatively slow to iterate over frozen objects](http://jsperf.com/performance-frozen-object/20).) The development build also overrides unsupported methods (methods that ordinarily mutate the underlying data structure) to throw helpful exceptions. | ||
The production (minified) build does neither of these, which significantly improves performance. | ||
## API Overview | ||
@@ -197,2 +199,6 @@ | ||
#### 2.3.0 | ||
Dates now retain their prototypes, the same way Arrays do. | ||
#### 2.2.0 | ||
@@ -199,0 +205,0 @@ |
@@ -36,4 +36,4 @@ (function(){ | ||
function isObject(target) { | ||
return target !== null && typeof target === "object" && !(target instanceof Array); | ||
function isMergableObject(target) { | ||
return target !== null && typeof target === "object" && !(target instanceof Array) && !(target instanceof Date); | ||
} | ||
@@ -262,3 +262,3 @@ | ||
result[key] = mergerResult; | ||
} else if (deep && isObject(currentObj[key]) && isObject(immutableValue)) { | ||
} else if (deep && isMergableObject(currentObj[key]) && isMergableObject(immutableValue)) { | ||
result[key] = currentObj[key].merge(immutableValue, config); | ||
@@ -327,2 +327,4 @@ } else { | ||
return makeImmutableArray(obj.slice()); | ||
} else if (obj instanceof Date) { | ||
return makeImmutable(new Date(obj)); | ||
} else { | ||
@@ -329,0 +331,0 @@ // Don't freeze the object we were given; make a clone and use that. |
@@ -1,1 +0,1 @@ | ||
!function(){"use strict";function a(a,b,c){Object.defineProperty(a,b,{enumerable:!1,configurable:!1,writable:!1,value:c})}function b(b){a(b,s,!0)}function c(a){return"object"==typeof a?null===a||a.hasOwnProperty(s):!0}function d(a){return null!==a&&"object"==typeof a&&!(a instanceof Array)}function e(a){var b=new Error(a);return b.__proto__=e,b}function f(a,c){b(a);return a}function g(b,c){var d=b[c];a(b,c,function(){return r(d.apply(b,arguments))})}function h(b){for(var c in w){var d=w[c];g(b,d)}a(b,"flatMap",i),a(b,"asObject",l),a(b,"asMutable",k);for(var e=0,h=b.length;h>e;e++)b[e]=r(b[e]);return f(b,v)}function i(a){if(0===arguments.length)return this;var b=[];for(var c in this){var d=a(this[c],c,this);d instanceof Array?b.push.apply(b,d):b.push(d)}return h(b)}function j(a){if(0===arguments.length)return this;a instanceof Array||(a=Array.prototype.slice.call(arguments));var b={};for(var c in this)-1===a.indexOf(c)&&(b[c]=this[c]);return q(b)}function k(a){var b,c,d=[];if(a&&a.deep)for(b=0,c=this.length;c>b;b++)d.push(m(this[b]));else for(b=0,c=this.length;c>b;b++)d.push(this[b]);return d}function l(a){"function"!=typeof a&&(a=function(a){return a});var b={};for(var c in this){var d=a(this[c],c,this),e=d[0],f=d[1];b[e]=f}return q(b)}function m(a){return a&&a.hasOwnProperty(s)?a.asMutable({deep:!0}):a}function n(a,b){for(var c in a)b[c]=a[c];return b}function o(a,b){function c(a,c,e){var h=r(c[e]),k=j&&j(a[e],h);f=f||void 0!==k||!a.hasOwnProperty(e)||h!==a[e]&&h===h,g[e]=k?k:i&&d(a[e])&&d(h)?a[e].merge(h,b):h}if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#merge can only be invoked with objects or arrays, not "+JSON.stringify(a));var e,f=!1,g=n(this,{}),h=a instanceof Array,i=b&&b.deep,j=b&&b.merger;if(h)for(var k in a){var l=a[k];for(e in l)c(this,l,e)}else for(e in a)c(this,a,e);return f?q(g):this}function p(a){var b,c={};if(a&&a.deep)for(b in this)c[b]=m(this[b]);else for(b in this)c[b]=this[b];return c}function q(b){return a(b,"merge",o),a(b,"without",j),a(b,"asMutable",p),f(b,t)}function r(a){if(arguments.length>1)return h(Array.prototype.slice.call(arguments));if(c(a))return a;if(a instanceof Array)return h(a.slice());var b={};for(var d in a)b[d]=r(a[d]);return q(b)}var s="__immutable_invariants_hold",t=["setPrototypeOf"],u=["keys"],v=t.concat(["push","pop","sort","splice","shift","unshift","reverse"]),w=u.concat(["map","filter","slice","concat","reduce","reduceRight"]);e.prototype=Error.prototype,r.isImmutable=c,r.ImmutableError=e,Object.freeze(r),"object"==typeof module?module.exports=r:"object"==typeof exports?exports.Immutable=r:"object"==typeof window?window.Immutable=r:"object"==typeof global&&(global.Immutable=r)}(); | ||
!function(){"use strict";function a(a,b,c){Object.defineProperty(a,b,{enumerable:!1,configurable:!1,writable:!1,value:c})}function b(b){a(b,s,!0)}function c(a){return"object"==typeof a?null===a||a.hasOwnProperty(s):!0}function d(a){return!(null===a||"object"!=typeof a||a instanceof Array||a instanceof Date)}function e(a){var b=new Error(a);return b.__proto__=e,b}function f(a,c){b(a);return a}function g(b,c){var d=b[c];a(b,c,function(){return r(d.apply(b,arguments))})}function h(b){for(var c in w){var d=w[c];g(b,d)}a(b,"flatMap",i),a(b,"asObject",l),a(b,"asMutable",k);for(var e=0,h=b.length;h>e;e++)b[e]=r(b[e]);return f(b,v)}function i(a){if(0===arguments.length)return this;var b=[];for(var c in this){var d=a(this[c],c,this);d instanceof Array?b.push.apply(b,d):b.push(d)}return h(b)}function j(a){if(0===arguments.length)return this;a instanceof Array||(a=Array.prototype.slice.call(arguments));var b={};for(var c in this)-1===a.indexOf(c)&&(b[c]=this[c]);return q(b)}function k(a){var b,c,d=[];if(a&&a.deep)for(b=0,c=this.length;c>b;b++)d.push(m(this[b]));else for(b=0,c=this.length;c>b;b++)d.push(this[b]);return d}function l(a){"function"!=typeof a&&(a=function(a){return a});var b={};for(var c in this){var d=a(this[c],c,this),e=d[0],f=d[1];b[e]=f}return q(b)}function m(a){return a&&a.hasOwnProperty(s)?a.asMutable({deep:!0}):a}function n(a,b){for(var c in a)b[c]=a[c];return b}function o(a,b){function c(a,c,e){var h=r(c[e]),k=j&&j(a[e],h);f=f||void 0!==k||!a.hasOwnProperty(e)||h!==a[e]&&h===h,g[e]=k?k:i&&d(a[e])&&d(h)?a[e].merge(h,b):h}if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#merge can only be invoked with objects or arrays, not "+JSON.stringify(a));var e,f=!1,g=n(this,{}),h=a instanceof Array,i=b&&b.deep,j=b&&b.merger;if(h)for(var k in a){var l=a[k];for(e in l)c(this,l,e)}else for(e in a)c(this,a,e);return f?q(g):this}function p(a){var b,c={};if(a&&a.deep)for(b in this)c[b]=m(this[b]);else for(b in this)c[b]=this[b];return c}function q(b){return a(b,"merge",o),a(b,"without",j),a(b,"asMutable",p),f(b,t)}function r(a){if(arguments.length>1)return h(Array.prototype.slice.call(arguments));if(c(a))return a;if(a instanceof Array)return h(a.slice());if(a instanceof Date)return f(new Date(a));var b={};for(var d in a)b[d]=r(a[d]);return q(b)}var s="__immutable_invariants_hold",t=["setPrototypeOf"],u=["keys"],v=t.concat(["push","pop","sort","splice","shift","unshift","reverse"]),w=u.concat(["map","filter","slice","concat","reduce","reduceRight"]);e.prototype=Error.prototype,r.isImmutable=c,r.ImmutableError=e,Object.freeze(r),"object"==typeof module?module.exports=r:"object"==typeof exports?exports.Immutable=r:"object"==typeof window?window.Immutable=r:"object"==typeof global&&(global.Immutable=r)}(); |
@@ -36,4 +36,4 @@ (function(){ | ||
function isObject(target) { | ||
return target !== null && typeof target === "object" && !(target instanceof Array); | ||
function isMergableObject(target) { | ||
return target !== null && typeof target === "object" && !(target instanceof Array) && !(target instanceof Date); | ||
} | ||
@@ -262,3 +262,3 @@ | ||
result[key] = mergerResult; | ||
} else if (deep && isObject(currentObj[key]) && isObject(immutableValue)) { | ||
} else if (deep && isMergableObject(currentObj[key]) && isMergableObject(immutableValue)) { | ||
result[key] = currentObj[key].merge(immutableValue, config); | ||
@@ -327,2 +327,4 @@ } else { | ||
return makeImmutableArray(obj.slice()); | ||
} else if (obj instanceof Date) { | ||
return makeImmutable(new Date(obj)); | ||
} else { | ||
@@ -329,0 +331,0 @@ // Don't freeze the object we were given; make a clone and use that. |
@@ -1,11 +0,14 @@ | ||
var JSC = require("jscheck"); | ||
var _ = require("lodash"); | ||
var assert = require("chai").assert; | ||
var JSC = require("jscheck"); | ||
var _ = require("lodash"); | ||
var assert = require("chai").assert; | ||
var devBuild = require("../seamless-immutable.development.js"); | ||
var prodBuild = require("../seamless-immutable.production.min.js"); | ||
var getTestUtils = require("./TestUtils.js"); | ||
[ | ||
{id: "dev", name: "Development build", src: "../seamless-immutable.development.js"}, | ||
{id: "prod", name: "Production build", src: "../seamless-immutable.production.min.js"} | ||
{id: "dev", name: "Development build", implementation: devBuild}, | ||
{id: "prod", name: "Production build", implementation: prodBuild} | ||
].forEach(function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("./TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
@@ -12,0 +15,0 @@ describe(config.name, function () { |
@@ -1,11 +0,14 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var devBuild = require("../seamless-immutable.development.js"); | ||
var prodBuild = require("../seamless-immutable.production.min.js"); | ||
var getTestUtils = require("./TestUtils.js"); | ||
[ | ||
{id: "dev", name: "Development build", src: "../seamless-immutable.development.js"}, | ||
{id: "prod", name: "Production build", src: "../seamless-immutable.production.min.js"} | ||
{id: "dev", name: "Development build", implementation: devBuild}, | ||
{id: "prod", name: "Production build", implementation: prodBuild} | ||
].forEach(function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("./TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
@@ -12,0 +15,0 @@ describe(config.name, function () { |
@@ -1,10 +0,16 @@ | ||
var testCompat = require("./ImmutableArray/test-compat.js"); | ||
var testFlatMap = require("./ImmutableArray/test-flatMap.js"); | ||
var testAsObject = require("./ImmutableArray/test-asObject.js"); | ||
var testToMutable = require("./ImmutableArray/test-asMutable.js"); | ||
var testCompat = require("./ImmutableArray/test-compat.js"); | ||
var testFlatMap = require("./ImmutableArray/test-flatMap.js"); | ||
var testAsObject = require("./ImmutableArray/test-asObject.js"); | ||
var testAsMutable = require("./ImmutableArray/test-asMutable.js"); | ||
var devBuild = require("../seamless-immutable.development.js"); | ||
var prodBuild = require("../seamless-immutable.production.min.js"); | ||
var getTestUtils = require("./TestUtils.js"); | ||
[ | ||
{id: "dev", name: "Development build", src: "../../seamless-immutable.development.js"}, | ||
{id: "prod", name: "Production build", src: "../../seamless-immutable.production.min.js"} | ||
{id: "dev", name: "Development build", implementation: devBuild}, | ||
{id: "prod", name: "Production build", implementation: prodBuild} | ||
].forEach(function(config) { | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
describe(config.name, function () { | ||
@@ -15,5 +21,5 @@ describe("ImmutableArray", function () { | ||
testAsObject(config); | ||
testToMutable(config); | ||
testAsMutable(config); | ||
}); | ||
}); | ||
}); |
@@ -1,8 +0,9 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -9,0 +10,0 @@ |
@@ -1,8 +0,9 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -9,0 +10,0 @@ |
@@ -1,10 +0,11 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
var identityFunction = function(arg) { return arg; }; | ||
var identityFunction = function(arg) { return arg; }; | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
@@ -11,0 +12,0 @@ var checkImmutableMutable = TestUtils.checkImmutableMutable(100, [JSC.array([TestUtils.ComplexObjectSpecifier()])]); |
@@ -1,8 +0,9 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -9,0 +10,0 @@ |
@@ -1,10 +0,16 @@ | ||
var testMerge = require("./ImmutableObject/test-merge.js"); | ||
var testCompat = require("./ImmutableObject/test-compat.js"); | ||
var testWithout = require("./ImmutableObject/test-without.js"); | ||
var testToMutable = require("./ImmutableObject/test-asMutable.js"); | ||
var testMerge = require("./ImmutableObject/test-merge.js"); | ||
var testCompat = require("./ImmutableObject/test-compat.js"); | ||
var testWithout = require("./ImmutableObject/test-without.js"); | ||
var testAsMutable = require("./ImmutableObject/test-asMutable.js"); | ||
var devBuild = require("../seamless-immutable.development.js"); | ||
var prodBuild = require("../seamless-immutable.production.min.js"); | ||
var getTestUtils = require("./TestUtils.js"); | ||
[ | ||
{id: "dev", name: "Development build", src: "../../seamless-immutable.development.js"}, | ||
{id: "prod", name: "Production build", src: "../../seamless-immutable.production.min.js"} | ||
{id: "dev", name: "Development build", implementation: devBuild}, | ||
{id: "prod", name: "Production build", implementation: prodBuild} | ||
].forEach(function(config) { | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
describe(config.name, function () { | ||
@@ -15,5 +21,5 @@ describe("ImmutableObject", function () { | ||
testWithout(config); | ||
testToMutable(config); | ||
testAsMutable(config); | ||
}); | ||
}); | ||
}); |
@@ -1,8 +0,9 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -9,0 +10,0 @@ |
@@ -1,6 +0,7 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
var identityFunction = function(arg) { return arg; }; | ||
var identityFunction = function(arg) { return arg; }; | ||
@@ -12,4 +13,4 @@ function notParseableAsInt(str) { | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
@@ -126,2 +127,22 @@ var checkImmutableMutable = TestUtils.checkImmutableMutable(100, [TestUtils.ComplexObjectSpecifier()]); | ||
it("supports handling dates without converting them to regular objects", function() { | ||
var date = new Date(); | ||
var immutableDate = Immutable(date); | ||
TestUtils.assertIsDeeplyImmutable(immutableDate); | ||
assert.isTrue(immutableDate instanceof Date); | ||
assert.notEqual(date, immutableDate); | ||
assert.equal(date.toISOString(), immutableDate.toISOString()); | ||
var objectWithDate = {date: new Date()}; | ||
var immutableObjectWithDate = Immutable(objectWithDate); | ||
TestUtils.assertIsDeeplyImmutable(immutableObjectWithDate); | ||
assert.isTrue(immutableObjectWithDate.date instanceof Date); | ||
assert.notEqual(objectWithDate, immutableObjectWithDate); | ||
assert.equal(objectWithDate.date.toISOString(), immutableObjectWithDate.date.toISOString()); | ||
}); | ||
it("makes nested content immutable as well", function() { | ||
@@ -128,0 +149,0 @@ checkImmutableMutable(function(immutable, mutable, innerArray, obj) { |
@@ -1,4 +0,5 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
@@ -15,4 +16,4 @@ // Anything but an object, array, or undefined. | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -19,0 +20,0 @@ |
@@ -1,4 +0,5 @@ | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var JSC = require("jscheck"); | ||
var assert = require("chai").assert; | ||
var _ = require("lodash"); | ||
var getTestUtils = require("../TestUtils.js"); | ||
@@ -10,4 +11,4 @@ function generateArrayOfStrings() { | ||
module.exports = function(config) { | ||
var Immutable = require(config.src); | ||
var TestUtils = require("../TestUtils.js")(Immutable); | ||
var Immutable = config.implementation; | ||
var TestUtils = getTestUtils(Immutable); | ||
var check = TestUtils.check; | ||
@@ -14,0 +15,0 @@ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
95801
1636
244
1