immutable-assign
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -1,6 +0,25 @@ | ||
declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
declare namespace ImmutableAssign { | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
} | ||
interface IIassign extends IIassignOption { | ||
<TObj, TProp, TContext>( | ||
obj: TObj, | ||
getProp: (obj: TObj, context: TContext) => TProp, | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
} | ||
} | ||
//declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
//export = iassign; | ||
declare module "immutable-assign" { | ||
let iassign: ImmutableAssign.IIassign; | ||
export = iassign; | ||
} |
"use strict"; | ||
//import deepFreeze = require("deep-freeze"); | ||
try { | ||
var deepFreeze = require("deep-freeze"); | ||
} | ||
catch (ex) { | ||
console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
} | ||
var iassign = _iassign; | ||
// Immutable Assign | ||
function iassign(obj, // Object to set property, it will not be modified | ||
getProp, // Function to get property to be updated. | ||
setProp, // Function to set property | ||
function _iassign(obj, // Object to set property, it will not be modified. | ||
getProp, // Function to get property to be updated. Must be pure function. | ||
setProp, // Function to set property. | ||
context) { | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
deepFreeze(obj); | ||
} | ||
// Check if getProp() is valid | ||
@@ -92,2 +103,5 @@ var value = getProp(obj, context); | ||
} | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
deepFreeze(obj); | ||
} | ||
return obj; | ||
@@ -94,0 +108,0 @@ } |
{ | ||
"name": "immutable-assign", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "", | ||
@@ -29,2 +29,5 @@ "main": "src/iassign.js", | ||
"dependencies": {}, | ||
"optionalDependencies":{ | ||
"deep-freeze": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
@@ -31,0 +34,0 @@ "deep-freeze": "0.0.1", |
@@ -13,3 +13,3 @@ # ImmutableAssign | ||
This library has only one method **iassign()**, which accept a POJO object and return you a new POJO object with specific property updated. | ||
This library has only one method **iassign()**, which accept a POJO object and return you a new POJO object with specific property updated. I have added some options to freeze input and output using [deep-freeze](https://github.com/substack/deep-freeze), which can be used in development to make sure they don't change unintentionally by us or the 3rd party libraries. | ||
@@ -29,2 +29,9 @@ ##Install with npm | ||
ctx?: TContext): TObj; // (Optional) Context to be used in getProp(). | ||
// Global options | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
} | ||
``` | ||
@@ -36,6 +43,7 @@ | ||
var iassign = require("immutable-assign"); | ||
var deepFreeze = require("deep-freeze"); | ||
// Deep freeze both input and output, can be used in development to make sure they don't change. | ||
iassign.freeze = true; | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
@@ -83,3 +91,2 @@ // | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
@@ -127,3 +134,2 @@ // | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]] } } }; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
@@ -146,8 +152,11 @@ // | ||
var iassign = require("immutable-assign"); | ||
var deepFreeze = require("deep-freeze"); | ||
var _ = require("lodash"); | ||
// Deep freeze both input and output, can be used in development to make sure they don't change. | ||
iassign.freeze = true; | ||
var o1 = { a: { b: { c: [1, 2, 3] } } }; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// Deep freeze both input and output, can be used in development to make sure they don't change. | ||
iassign.freeze = true; | ||
@@ -154,0 +163,0 @@ // |
@@ -168,3 +168,3 @@ "use strict"; | ||
it("Update array using lodash", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } } }; | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
@@ -178,5 +178,4 @@ // | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } } }); | ||
expect(o2.a.b.c[0][0].d).toBe(12); | ||
expect(o2.a.b.c[0][1].d).toBe(14); | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }); | ||
expect(o2).toEqual({ a: { b: { c: [[{ d: 12, e: 12 }, { d: 14, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }); | ||
expect(o2).not.toBe(o1); | ||
@@ -186,6 +185,6 @@ expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a.b.c).not.toBe(o1.a.b.c); | ||
// expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]); | ||
// expect(o2.a.b.c[1][0]).not.toBe(o1.a.b.c[1][0]); | ||
// expect(o2.a.b.c[1][0].d).not.toBe(o1.a.b.c[1][0].d); | ||
// expect(o2.a.b.c[1][0].d).toBe(22); | ||
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][1]).not.toBe(o1.a.b.c[0][1]); | ||
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]); | ||
}); | ||
@@ -280,2 +279,52 @@ it("Update array using lodash 2", function () { | ||
}); | ||
it("Use built-in deep freeze to protect input", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; | ||
iassign.freezeInput = true; | ||
expect(function () { | ||
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(3); return ci; }); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(function () { | ||
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; }); | ||
}).toThrowError(TypeError, /Cannot assign to read only property/); | ||
expect(function () { | ||
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].g = 1; return ci; }); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(function () { | ||
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; }); | ||
}).toThrowError(TypeError, /object is not extensible/); | ||
iassign.freezeInput = undefined; | ||
}); | ||
it("Use built-in deep freeze to protect output", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } }; | ||
iassign.freezeOutput = true; | ||
var o2 = iassign(o1, function (o) { return o.a.b.c[0]; }, function (c) { | ||
return _.map(c, function (item) { return iassign(item, function (o) { return o.d; }, function (d) { return d + 1; }); }); | ||
}); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } }); | ||
expect(o2.a.b.c[0][0].d).toBe(12); | ||
expect(o2.a.b.c[0][1].d).toBe(14); | ||
expect(o2.a.b.c[0][2].d).toBe(22); | ||
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][1]).not.toBe(o1.a.b.c[0][1]); | ||
expect(o2.a.b.c[0][2]).not.toBe(o1.a.b.c[0][2]); | ||
expect(function () { | ||
o2.a.b.c[0].push(3); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(function () { | ||
o2.a.b.c[0][0].d++; | ||
}).toThrowError(TypeError, /Cannot assign to read only property/); | ||
expect(function () { | ||
o2.a.b.c[0][0].g = 1; | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(function () { | ||
o2.a.b.c[0].pop(); | ||
}).toThrowError(TypeError, /object is not extensible/); | ||
iassign.freezeOutput = undefined; | ||
}); | ||
}); |
@@ -219,3 +219,3 @@ | ||
it("Update array using lodash", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } } }; | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }; | ||
@@ -235,6 +235,5 @@ deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } } }) | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }) | ||
expect(o2.a.b.c[0][0].d).toBe(12); | ||
expect(o2.a.b.c[0][1].d).toBe(14); | ||
expect(o2).toEqual({ a: { b: { c: [[{ d: 12, e: 12 }, { d: 14, e: 14 }], [{ d: 21, e: 22 }]] } }, a2: {} }); | ||
@@ -245,6 +244,7 @@ expect(o2).not.toBe(o1); | ||
expect(o2.a.b.c).not.toBe(o1.a.b.c); | ||
// expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]); | ||
// expect(o2.a.b.c[1][0]).not.toBe(o1.a.b.c[1][0]); | ||
// expect(o2.a.b.c[1][0].d).not.toBe(o1.a.b.c[1][0].d); | ||
// expect(o2.a.b.c[1][0].d).toBe(22); | ||
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][1]).not.toBe(o1.a.b.c[0][1]); | ||
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]); | ||
}); | ||
@@ -372,3 +372,3 @@ | ||
// expect o2 inner property has been updated. | ||
expect(o2).toEqual({ a: { b: 1}, a2: {} }); | ||
expect(o2).toEqual({ a: { b: 1 }, a2: {} }); | ||
@@ -384,2 +384,71 @@ // expect object graph for changed property in o2 is now different from (!==) o1. | ||
it("Use built-in deep freeze to protect input", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; | ||
iassign.freezeInput = true; | ||
expect(() => { | ||
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(<any>3); return ci; }); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(() => { | ||
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; }); | ||
}).toThrowError(TypeError, /Cannot assign to read only property/); | ||
expect(() => { | ||
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { (<any>ci[0]).g = 1; return ci; }); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(() => { | ||
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; }); | ||
}).toThrowError(TypeError, /object is not extensible/); | ||
iassign.freezeInput = undefined; | ||
}); | ||
it("Use built-in deep freeze to protect output", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } }; | ||
iassign.freezeOutput = true; | ||
let o2 = iassign( | ||
o1, | ||
(o) => o.a.b.c[0], | ||
(c) => { | ||
return _.map(c, (item) => { return iassign(item, (o) => o.d, (d) => d + 1); }); | ||
}); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } }) | ||
expect(o2.a.b.c[0][0].d).toBe(12); | ||
expect(o2.a.b.c[0][1].d).toBe(14); | ||
expect(o2.a.b.c[0][2].d).toBe(22); | ||
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][1]).not.toBe(o1.a.b.c[0][1]); | ||
expect(o2.a.b.c[0][2]).not.toBe(o1.a.b.c[0][2]); | ||
expect(() => { | ||
o2.a.b.c[0].push(<any>3); | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(() => { | ||
o2.a.b.c[0][0].d++; | ||
}).toThrowError(TypeError, /Cannot assign to read only property/); | ||
expect(() => { | ||
(<any>o2.a.b.c[0][0]).g = 1; | ||
}).toThrowError(TypeError, /Can't add property/); | ||
expect(() => { | ||
o2.a.b.c[0].pop(); | ||
}).toThrowError(TypeError, /object is not extensible/); | ||
iassign.freezeOutput = undefined; | ||
}); | ||
}); |
@@ -1,6 +0,25 @@ | ||
declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
declare namespace ImmutableAssign { | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
} | ||
interface IIassign extends IIassignOption { | ||
<TObj, TProp, TContext>( | ||
obj: TObj, | ||
getProp: (obj: TObj, context: TContext) => TProp, | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
} | ||
} | ||
//declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
//export = iassign; | ||
declare module "immutable-assign" { | ||
let iassign: ImmutableAssign.IIassign; | ||
export = iassign; | ||
} |
"use strict"; | ||
//import deepFreeze = require("deep-freeze"); | ||
try { | ||
var deepFreeze = require("deep-freeze"); | ||
} | ||
catch (ex) { | ||
console.warn("Cannot load deep-freeze module, however you can still use iassign() function."); | ||
} | ||
var iassign = _iassign; | ||
// Immutable Assign | ||
function iassign(obj, // Object to set property, it will not be modified | ||
getProp, // Function to get property to be updated. | ||
setProp, // Function to set property | ||
function _iassign(obj, // Object to set property, it will not be modified. | ||
getProp, // Function to get property to be updated. Must be pure function. | ||
setProp, // Function to set property. | ||
context) { | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
deepFreeze(obj); | ||
} | ||
// Check if getProp() is valid | ||
@@ -92,2 +103,5 @@ var value = getProp(obj, context); | ||
} | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
deepFreeze(obj); | ||
} | ||
return obj; | ||
@@ -94,0 +108,0 @@ } |
"use strict"; | ||
//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."); | ||
} | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
disableAllCheck: boolean; | ||
disableHasReturnCheck: boolean; | ||
disableExtraStatementCheck: boolean; | ||
} | ||
interface IIassign extends IIassignOption { | ||
<TObj, TProp, TContext>( | ||
obj: TObj, | ||
getProp: (obj: TObj, context: TContext) => TProp, | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
} | ||
var iassign: IIassign = <any>_iassign; | ||
// Immutable Assign | ||
function iassign<TObj, TProp, TContext>( | ||
obj: TObj, // Object to set property, it will not be modified | ||
getProp: (obj: TObj, context: TContext) => TProp, // Function to get property to be updated. | ||
setProp: (prop: TProp) => TProp, // Function to set property | ||
context?: TContext): TObj { // Context to be used in getProp() | ||
function _iassign<TObj, TProp, TContext>( | ||
obj: TObj, // Object to set property, it will not be modified. | ||
getProp: (obj: TObj, context: TContext) => TProp, // Function to get property to be updated. Must be pure function. | ||
setProp: (prop: TProp) => TProp, // Function to set property. | ||
context?: TContext): TObj { // (Optional) Context to be used in getProp() . | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
deepFreeze(obj); | ||
} | ||
// Check if getProp() is valid | ||
@@ -114,2 +145,6 @@ let value = getProp(obj, context); | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
deepFreeze(obj); | ||
} | ||
return obj; | ||
@@ -158,3 +193,3 @@ } | ||
if (!(<any>iassign).disableAllCheck && !(<any>iassign).disableHasReturnCheck) { | ||
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) { | ||
if (returnIndex <= -1) { | ||
@@ -165,3 +200,3 @@ throw new Error("getProp() function has no 'return' keyword."); | ||
if (!(<any>iassign).disableAllCheck && !(<any>iassign).disableExtraStatementCheck) { | ||
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) { | ||
let otherBodyText = bodyText.substr(0, returnIndex).trim(); | ||
@@ -168,0 +203,0 @@ if (otherBodyText != "") { |
@@ -11,4 +11,5 @@ { | ||
"node_modules", | ||
"bower_components" | ||
"bower_components", | ||
"deploy" | ||
] | ||
} |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
773368
22
23025
196
1
2