immutable-assign
Advanced tools
Comparing version 1.0.29 to 1.0.30
@@ -8,2 +8,3 @@ | ||
freezeOutput?: boolean; // Deep freeze output | ||
useConstructor?: boolean; // Uses the constructor to create new instances | ||
disableAllCheck?: boolean; | ||
@@ -10,0 +11,0 @@ disableHasReturnCheck?: boolean; |
@@ -5,6 +5,6 @@ "use strict"; | ||
try { | ||
var deepFreeze = require("deep-freeze"); | ||
var deepFreeze = require("deep-freeze-strict"); | ||
} | ||
catch (ex) { | ||
console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
console.warn("Cannot load deep-freeze-strict module, however you can still use iassign() function."); | ||
} | ||
@@ -16,3 +16,3 @@ var v = factory(deepFreeze, exports); | ||
else if (typeof define === 'function' && define.amd) { | ||
define(["deep-freeze", "exports"], factory); | ||
define(["deep-freeze-strict", "exports"], factory); | ||
} | ||
@@ -24,8 +24,2 @@ else { | ||
})(this, function (deepFreeze, exports) { | ||
//import deepFreeze = require("deep-freeze"); | ||
// try { | ||
// var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze"); | ||
// } catch (ex) { | ||
// console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
// } | ||
var autoCurry = (function () { | ||
@@ -79,3 +73,3 @@ var toArray = function toArray(arr, from) { | ||
if (!getProp) { | ||
obj = quickCopy(obj); | ||
obj = quickCopy(obj, option.useConstructor); | ||
obj = setProp(obj); | ||
@@ -87,3 +81,3 @@ } | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp, option); | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo); | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo, option); | ||
} | ||
@@ -104,2 +98,3 @@ if (deepFreeze && (option.freeze || option.freezeOutput)) { | ||
newOption.freezeOutput = iassign.freezeOutput; | ||
newOption.useConstructor = iassign.useConstructor; | ||
newOption.disableAllCheck = iassign.disableAllCheck; | ||
@@ -119,2 +114,5 @@ newOption.disableHasReturnCheck = iassign.disableHasReturnCheck; | ||
} | ||
if (option.useConstructor != undefined) { | ||
newOption.useConstructor = option.useConstructor; | ||
} | ||
if (option.disableAllCheck != undefined) { | ||
@@ -135,3 +133,3 @@ newOption.disableAllCheck = option.disableAllCheck; | ||
} | ||
function updateProperty(obj, setProp, context, getPropFuncInfo) { | ||
function updateProperty(obj, setProp, context, getPropFuncInfo, option) { | ||
var propValue = undefined; | ||
@@ -142,3 +140,3 @@ for (var propIndex = 0; propIndex < getPropFuncInfo.funcTokens.length; ++propIndex) { | ||
if (propIndex <= 0) { | ||
propValue = quickCopy(obj); | ||
propValue = quickCopy(obj, option.useConstructor); | ||
if (!subAccessorText) { | ||
@@ -155,3 +153,3 @@ propValue = setProp(propValue); | ||
propValue = propValue[propName]; | ||
propValue = quickCopy(propValue); | ||
propValue = quickCopy(propValue, option.useConstructor); | ||
if (!subAccessorText) { | ||
@@ -377,3 +375,3 @@ propValue = setProp(propValue); | ||
} | ||
function quickCopy(value) { | ||
function quickCopy(value, useConstructor) { | ||
if (value != undefined && !(value instanceof Date)) { | ||
@@ -384,2 +382,6 @@ if (value instanceof Array) { | ||
else if (typeof (value) === "object") { | ||
if (useConstructor) { | ||
var target = new value.constructor(); | ||
return extend(target, value); | ||
} | ||
return extend({}, value); | ||
@@ -386,0 +388,0 @@ } |
function deepFreeze (o) { | ||
Object.freeze(o); | ||
var oIsFunction = typeof o === "function"; | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
Object.getOwnPropertyNames(o).forEach(function (prop) { | ||
if (o.hasOwnProperty(prop) | ||
if (hasOwnProp.call(o, prop) | ||
&& (oIsFunction ? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' : true ) | ||
&& o[prop] !== null | ||
@@ -7,0 +11,0 @@ && (typeof o[prop] === "object" || typeof o[prop] === "function") |
{ | ||
"name": "immutable-assign", | ||
"version": "1.0.29", | ||
"version": "1.0.30", | ||
"description": "Lightweight immutable helper that allows you to continue working with Plain JavaScript Objects", | ||
@@ -36,3 +36,3 @@ "main": "src/iassign.js", | ||
"optionalDependencies": { | ||
"deep-freeze": "0.0.1" | ||
"deep-freeze-strict": "^1.1.1" | ||
}, | ||
@@ -43,2 +43,3 @@ "devDependencies": { | ||
"deep-freeze": "0.0.1", | ||
"deep-freeze-strict": "^1.1.1", | ||
"edge-launcher": "*", | ||
@@ -45,0 +46,0 @@ "gulp": "^3.9.1", |
@@ -422,2 +422,3 @@ # immutable-assign (iassign.js) | ||
* 1.0.30 - [Support classes](https://github.com/engineforce/ImmutableAssign/issues/4) | ||
* 1.0.29 - Supported ES6 [Arrow Functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) | ||
@@ -424,0 +425,0 @@ * 1.0.27 - Added iassign.fp() that support [currying](https://www.sitepoint.com/currying-in-functional-javascript), refer to [example 8](#example-8-update-nested-structures-using-iassignfp-and-currying) |
@@ -77,3 +77,3 @@ "use strict"; | ||
var o2; | ||
eval("o2 = iassign(o1, (o) => { return o.a.b.c[0][0] }, function (ci) { ci.d++; return ci; });") | ||
eval("o2 = iassign(o1, (o) => { return o.a.b.c[0][0] }, (ci) => { ci.d++; return ci; });") | ||
@@ -96,3 +96,3 @@ expect(o2).not.toBe(o1); | ||
var o2; | ||
eval("o2 = iassign(o1, (o) => o.a.b.c[0][0], function (ci) { ci.d++; return ci; });"); | ||
eval("o2 = iassign(o1, (o) => o.a.b.c[0][0], ci => { ci.d++; return ci; });"); | ||
@@ -119,3 +119,3 @@ expect(o2).not.toBe(o1); | ||
var o2; | ||
eval("o2 = iassign(o1, (o, ctx) => o.a.b.c[ctx.p1.a][0], function (ci) { ci.d++; return ci; }, { p1: p1 });"); | ||
eval("o2 = iassign(o1, (o, ctx) => o.a.b.c[ctx.p1.a][0], (ci) => { ci.d++; return ci; }, { p1: p1 });"); | ||
@@ -137,3 +137,3 @@ expect(o2).not.toBe(o1); | ||
var o2; | ||
eval("o2 = iassign(o1, o => o.a.b.c[0][0], function (ci) { ci.d++; return ci; });"); | ||
eval("o2 = iassign(o1, o => o.a.b.c[0][0], (ci) => { ci.d++; return ci; });"); | ||
@@ -150,3 +150,21 @@ expect(o2).not.toBe(o1); | ||
}); | ||
it("Arrow function 5: arrow function for set without ()), {} and return", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; | ||
deepFreeze(o1); | ||
var o2; | ||
eval("o2 = iassign(o1, o => o.a.b.c[0][0].d, d => d+1);"); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a.b).not.toBe(o1.a.b); | ||
expect(o2.a.b.c).not.toBe(o1.a.b.c); | ||
expect(o2.a.b.c[0]).not.toBe(o1.a.b.c[0]); | ||
expect(o2.a.b.c[0][0]).not.toBe(o1.a.b.c[0][0]); | ||
expect(o2.a.b.c[0][0].d).not.toBe(o1.a.b.c[0][0].d); | ||
expect(o2.a.b.c[0][0].d).toBe(12); | ||
}); | ||
}); | ||
}); |
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
(function (root, factory) { | ||
@@ -573,2 +578,49 @@ if (typeof module === 'object' && typeof module.exports === 'object') { | ||
}); | ||
it("Issue 4: Support classes", function () { | ||
iassign.freeze = true; | ||
var option = { | ||
useConstructor: true | ||
}; | ||
var Klass = (function () { | ||
function Klass() { | ||
this.prop = 11; | ||
} | ||
Klass.prototype.func = function () { return "Klass" + this.prop; }; | ||
return Klass; | ||
}()); | ||
var ChildKlass = (function (_super) { | ||
__extends(ChildKlass, _super); | ||
function ChildKlass() { | ||
_super.apply(this, arguments); | ||
this.prop = 101; | ||
} | ||
ChildKlass.prototype.func2 = function () { return "ChildKlass" + this.prop; }; | ||
return ChildKlass; | ||
}(Klass)); | ||
var s = { | ||
arr: [1], | ||
obj: { prop: 1, func: function () { return "Klass" + this.prop; } }, | ||
inst: new Klass(), | ||
inst2: new ChildKlass(), | ||
}; | ||
var t1 = iassign(s, function (x) { return x.arr; }, function (arr) { arr.push(2); return arr; }, null, option); | ||
expect(s.arr.length).toEqual(1); | ||
expect(t1.arr.length).toEqual(2); | ||
var t2 = iassign(s, function (x) { return x.obj.prop; }, function (y) { return y + 1; }, null, option); | ||
expect(s.obj.prop).toEqual(1); | ||
expect(t2.obj.prop).toEqual(2); | ||
expect(t2.obj.func()).toEqual("Klass2"); | ||
var t3 = iassign(s, function (x) { return x.inst.prop; }, function (v) { return v + 1; }, null, option); | ||
expect(s.inst.prop).toEqual(11); | ||
expect(t3.inst.prop).toEqual(12); | ||
expect(t3.inst.func()).toEqual("Klass12"); | ||
var t4 = iassign(s, function (x) { return x.inst2.prop; }, function (v) { return v + 1; }, null, option); | ||
expect(s.inst2.prop).toEqual(101); | ||
expect(t4.inst2.prop).toEqual(102); | ||
expect(t4.inst2.func()).toEqual("Klass102"); | ||
expect(t4.inst2.func2()).toEqual("ChildKlass102"); | ||
expect(t4.inst2 instanceof Klass).toEqual(true); | ||
expect(t4.inst2 instanceof ChildKlass).toEqual(true); | ||
expect(t4.inst2.constructor.name == undefined || t4.inst2.constructor.name == "ChildKlass").toEqual(true); | ||
}); | ||
it("iassign.fp", function () { | ||
@@ -575,0 +627,0 @@ //var iassign = require("immutable-assign"); |
@@ -856,2 +856,51 @@ | ||
it("Issue 4: Support classes", function () { | ||
iassign.freeze = true; | ||
const option: IIassignOption = { | ||
useConstructor: true | ||
}; | ||
class Klass { | ||
prop: number = 11; | ||
func() { return "Klass" + this.prop; } | ||
} | ||
class ChildKlass extends Klass { | ||
prop: number = 101; | ||
func2() { return "ChildKlass" + this.prop; } | ||
} | ||
const s = { | ||
arr: [1], | ||
obj: { prop: 1, func: function() { return "Klass" + this.prop; } }, | ||
inst: new Klass(), | ||
inst2: new ChildKlass(), | ||
}; | ||
const t1 = iassign(s, (x) => x.arr, (arr) => { arr.push(2); return arr; }, null, option); | ||
expect(s.arr.length).toEqual(1); | ||
expect(t1.arr.length).toEqual(2); | ||
const t2 = iassign(s, (x) => x.obj.prop, (y) => y + 1, null, option); | ||
expect(s.obj.prop).toEqual(1); | ||
expect(t2.obj.prop).toEqual(2); | ||
expect(t2.obj.func()).toEqual("Klass2"); | ||
const t3 = iassign(s, (x) => x.inst.prop, (v) => v + 1, null, option); | ||
expect(s.inst.prop).toEqual(11); | ||
expect(t3.inst.prop).toEqual(12); | ||
expect(t3.inst.func()).toEqual("Klass12"); | ||
const t4 = iassign(s, (x) => x.inst2.prop, (v) => v + 1, null, option); | ||
expect(s.inst2.prop).toEqual(101); | ||
expect(t4.inst2.prop).toEqual(102); | ||
expect(t4.inst2.func()).toEqual("Klass102"); | ||
expect(t4.inst2.func2()).toEqual("ChildKlass102"); | ||
expect(t4.inst2 instanceof Klass).toEqual(true); | ||
expect(t4.inst2 instanceof ChildKlass).toEqual(true); | ||
expect((<any>t4.inst2.constructor).name == undefined || (<any>t4.inst2.constructor).name == "ChildKlass").toEqual(true); | ||
}); | ||
it("iassign.fp", function () { | ||
@@ -858,0 +907,0 @@ |
@@ -8,2 +8,3 @@ | ||
freezeOutput?: boolean; // Deep freeze output | ||
useConstructor?: boolean; // Uses the constructor to create new instances | ||
disableAllCheck?: boolean; | ||
@@ -10,0 +11,0 @@ disableHasReturnCheck?: boolean; |
@@ -5,6 +5,6 @@ "use strict"; | ||
try { | ||
var deepFreeze = require("deep-freeze"); | ||
var deepFreeze = require("deep-freeze-strict"); | ||
} | ||
catch (ex) { | ||
console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
console.warn("Cannot load deep-freeze-strict module, however you can still use iassign() function."); | ||
} | ||
@@ -16,3 +16,3 @@ var v = factory(deepFreeze, exports); | ||
else if (typeof define === 'function' && define.amd) { | ||
define(["deep-freeze", "exports"], factory); | ||
define(["deep-freeze-strict", "exports"], factory); | ||
} | ||
@@ -24,8 +24,2 @@ else { | ||
})(this, function (deepFreeze, exports) { | ||
//import deepFreeze = require("deep-freeze"); | ||
// try { | ||
// var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze"); | ||
// } catch (ex) { | ||
// console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
// } | ||
var autoCurry = (function () { | ||
@@ -79,3 +73,3 @@ var toArray = function toArray(arr, from) { | ||
if (!getProp) { | ||
obj = quickCopy(obj); | ||
obj = quickCopy(obj, option.useConstructor); | ||
obj = setProp(obj); | ||
@@ -87,3 +81,3 @@ } | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp, option); | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo); | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo, option); | ||
} | ||
@@ -104,2 +98,3 @@ if (deepFreeze && (option.freeze || option.freezeOutput)) { | ||
newOption.freezeOutput = iassign.freezeOutput; | ||
newOption.useConstructor = iassign.useConstructor; | ||
newOption.disableAllCheck = iassign.disableAllCheck; | ||
@@ -119,2 +114,5 @@ newOption.disableHasReturnCheck = iassign.disableHasReturnCheck; | ||
} | ||
if (option.useConstructor != undefined) { | ||
newOption.useConstructor = option.useConstructor; | ||
} | ||
if (option.disableAllCheck != undefined) { | ||
@@ -135,3 +133,3 @@ newOption.disableAllCheck = option.disableAllCheck; | ||
} | ||
function updateProperty(obj, setProp, context, getPropFuncInfo) { | ||
function updateProperty(obj, setProp, context, getPropFuncInfo, option) { | ||
var propValue = undefined; | ||
@@ -142,3 +140,3 @@ for (var propIndex = 0; propIndex < getPropFuncInfo.funcTokens.length; ++propIndex) { | ||
if (propIndex <= 0) { | ||
propValue = quickCopy(obj); | ||
propValue = quickCopy(obj, option.useConstructor); | ||
if (!subAccessorText) { | ||
@@ -155,3 +153,3 @@ propValue = setProp(propValue); | ||
propValue = propValue[propName]; | ||
propValue = quickCopy(propValue); | ||
propValue = quickCopy(propValue, option.useConstructor); | ||
if (!subAccessorText) { | ||
@@ -377,3 +375,3 @@ propValue = setProp(propValue); | ||
} | ||
function quickCopy(value) { | ||
function quickCopy(value, useConstructor) { | ||
if (value != undefined && !(value instanceof Date)) { | ||
@@ -384,2 +382,6 @@ if (value instanceof Array) { | ||
else if (typeof (value) === "object") { | ||
if (useConstructor) { | ||
var target = new value.constructor(); | ||
return extend(target, value); | ||
} | ||
return extend({}, value); | ||
@@ -386,0 +388,0 @@ } |
@@ -11,2 +11,3 @@ "use strict"; | ||
freezeOutput?: boolean; // Deep freeze output | ||
useConstructor?: boolean; // Uses the constructor to create new instances | ||
disableAllCheck?: boolean; | ||
@@ -61,5 +62,5 @@ disableHasReturnCheck?: boolean; | ||
try { | ||
var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze"); | ||
var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze-strict"); | ||
} catch (ex) { | ||
console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
console.warn("Cannot load deep-freeze-strict module, however you can still use iassign() function."); | ||
} | ||
@@ -70,3 +71,3 @@ | ||
else if (typeof define === 'function' && define.amd) { | ||
define(["deep-freeze", "exports"], factory); | ||
define(["deep-freeze-strict", "exports"], factory); | ||
} else { | ||
@@ -78,10 +79,2 @@ // Browser globals (root is window) | ||
//import deepFreeze = require("deep-freeze"); | ||
// try { | ||
// var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze"); | ||
// } catch (ex) { | ||
// console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
// } | ||
var autoCurry = (function () { | ||
@@ -148,3 +141,3 @@ | ||
if (!getProp) { | ||
obj = quickCopy(obj); | ||
obj = quickCopy(obj, option.useConstructor); | ||
obj = <any>setProp(<any>obj); | ||
@@ -158,3 +151,3 @@ } | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo); | ||
obj = updateProperty(obj, setProp, context, getPropFuncInfo, option); | ||
} | ||
@@ -187,2 +180,3 @@ | ||
newOption.freezeOutput = iassign.freezeOutput; | ||
newOption.useConstructor = iassign.useConstructor; | ||
newOption.disableAllCheck = iassign.disableAllCheck; | ||
@@ -197,2 +191,3 @@ newOption.disableHasReturnCheck = iassign.disableHasReturnCheck; | ||
if (option.freezeOutput != undefined) { newOption.freezeOutput = option.freezeOutput; } | ||
if (option.useConstructor != undefined) { newOption.useConstructor = option.useConstructor; } | ||
if (option.disableAllCheck != undefined) { newOption.disableAllCheck = option.disableAllCheck; } | ||
@@ -211,3 +206,4 @@ if (option.disableHasReturnCheck != undefined) { newOption.disableHasReturnCheck = option.disableHasReturnCheck; } | ||
context: TContext, | ||
getPropFuncInfo: IGetPropFuncInfo): TObj { | ||
getPropFuncInfo: IGetPropFuncInfo, | ||
option: IIassignOption): TObj { | ||
@@ -222,3 +218,3 @@ let propValue = undefined; | ||
if (propIndex <= 0) { | ||
propValue = quickCopy(obj); | ||
propValue = quickCopy(obj, option.useConstructor); | ||
@@ -238,3 +234,3 @@ if (!subAccessorText) { | ||
propValue = propValue[propName]; | ||
propValue = quickCopy(propValue) | ||
propValue = quickCopy(propValue, option.useConstructor); | ||
@@ -521,3 +517,3 @@ if (!subAccessorText) { | ||
function quickCopy<T>(value: T): T { | ||
function quickCopy<T>(value: T, useConstructor: boolean): T { | ||
@@ -529,2 +525,7 @@ if (value != undefined && !(value instanceof Date)) { | ||
else if (typeof (value) === "object") { | ||
if (useConstructor) | ||
{ | ||
const target = new (value as any).constructor(); | ||
return extend(target, value); | ||
} | ||
return extend({}, value); | ||
@@ -531,0 +532,0 @@ } |
function deepFreeze (o) { | ||
Object.freeze(o); | ||
var oIsFunction = typeof o === "function"; | ||
var hasOwnProp = Object.prototype.hasOwnProperty; | ||
Object.getOwnPropertyNames(o).forEach(function (prop) { | ||
if (o.hasOwnProperty(prop) | ||
if (hasOwnProp.call(o, prop) | ||
&& (oIsFunction ? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' : true ) | ||
&& o[prop] !== null | ||
@@ -7,0 +11,0 @@ && (typeof o[prop] === "object" || typeof o[prop] === "function") |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
973863
25754
442
24
11