immutable-assign
Advanced tools
Comparing version 1.0.7 to 1.0.8
declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
export = iassign; | ||
//export = iassign; | ||
declare module "immutable-assign" { | ||
export = iassign; | ||
} |
@@ -57,4 +57,7 @@ "use strict"; | ||
if (propIndex <= 0) { | ||
obj = quickCopy(obj); | ||
propValue = obj; | ||
propValue = quickCopy(obj); | ||
if (!accessorText) { | ||
propValue = setProp(propValue); | ||
} | ||
obj = propValue; | ||
} | ||
@@ -81,10 +84,3 @@ else { | ||
propValue = propValue[propName]; | ||
if (propValue != undefined && !(propValue instanceof Date)) { | ||
if (propValue instanceof Array) { | ||
propValue = propValue.slice(); | ||
} | ||
else if (typeof (propValue) === "object") { | ||
propValue = quickCopy(propValue); | ||
} | ||
} | ||
propValue = quickCopy(propValue); | ||
if (!accessorText) { | ||
@@ -152,7 +148,15 @@ propValue = setProp(propValue); | ||
function quickCopy(value) { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
if (value != undefined && !(value instanceof Date)) { | ||
if (value instanceof Array) { | ||
return value.slice(); | ||
} | ||
else if (typeof (value) === "object") { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
} | ||
} | ||
return copyValue; | ||
return value; | ||
} | ||
@@ -159,0 +163,0 @@ function evalStatement() { |
{ | ||
"name": "immutable-assign", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/iassign.js", |
@@ -190,5 +190,5 @@ "use strict"; | ||
o | ||
, | ||
, | ||
b | ||
) { | ||
) { | ||
return o | ||
@@ -235,3 +235,3 @@ . | ||
o | ||
) { | ||
) { | ||
return o | ||
@@ -246,4 +246,4 @@ . | ||
o | ||
. | ||
a2 | ||
. | ||
a2 | ||
] | ||
@@ -452,2 +452,62 @@ [ | ||
}); | ||
it("Test root is an array, and try to update root array", function () { | ||
var o1 = [{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// | ||
// Calling iassign() and _.map() to increment to every item in "c" array | ||
// | ||
var o2 = iassign( | ||
o1, | ||
function (o) { return o | ||
; }, | ||
function (o) { | ||
return _.map(o, function (item, index) { | ||
if (index < 2) { | ||
item = _.cloneDeep(item); | ||
item.d++; | ||
} | ||
return item; | ||
}); | ||
}); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual([{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]); | ||
// expect o2.a.b.c has been updated. | ||
expect(o2).toEqual([{ d: 12, e: 12 }, { d: 14, e: 14 }, { d: 21, e: 22 }]); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2[0]).not.toBe(o1[0]); | ||
expect(o2[1]).not.toBe(o1[1]); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2[2]).toBe(o1[2]); | ||
}); | ||
it("Test root is an object, and try to update root object", function () { | ||
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 | ||
var o2 = iassign( | ||
o1, | ||
function (o) { return o }, | ||
function (o) { o.a = { b: 1 }; return o; }); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }); | ||
// expect o2 inner property has been updated. | ||
expect(o2).toEqual({ a: { b: 1 }, a2: {} }); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a.b).not.toBe(o1.a.b); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2.a2).toBe(o1.a2); | ||
}); | ||
}); |
@@ -206,2 +206,73 @@ "use strict"; | ||
}); | ||
it("Test root object is an array", function () { | ||
var o1 = [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]]; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// | ||
// Calling iassign() and _.map() to increment to every item in "c" array | ||
// | ||
var o2 = iassign(o1, function (o) { return o[0]; }, function (o) { | ||
return _.map(o, function (item, index) { | ||
if (index < 2) { | ||
item = _.cloneDeep(item); | ||
item.d++; | ||
} | ||
return item; | ||
}); | ||
}); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual([[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]]); | ||
// expect o2.a.b.c has been updated. | ||
expect(o2).toEqual([[{ d: 12, e: 12 }, { d: 14, e: 14 }, { d: 21, e: 22 }]]); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2[0]).not.toBe(o1[0]); | ||
expect(o2[0][0]).not.toBe(o1[0][0]); | ||
expect(o2[0][1]).not.toBe(o1[0][1]); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2[0][2]).toBe(o1[0][2]); | ||
}); | ||
it("Test root is an array, and try to update root array", function () { | ||
var o1 = [{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// | ||
// Calling iassign() and _.map() to increment to every item in "c" array | ||
// | ||
var o2 = iassign(o1, function (o) { return o; }, function (o) { | ||
return _.map(o, function (item, index) { | ||
if (index < 2) { | ||
item = _.cloneDeep(item); | ||
item.d++; | ||
} | ||
return item; | ||
}); | ||
}); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual([{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]); | ||
// expect o2.a.b.c has been updated. | ||
expect(o2).toEqual([{ d: 12, e: 12 }, { d: 14, e: 14 }, { d: 21, e: 22 }]); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2[0]).not.toBe(o1[0]); | ||
expect(o2[1]).not.toBe(o1[1]); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2[2]).toBe(o1[2]); | ||
}); | ||
it("Test root is an object, and try to update root object", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }; | ||
deepFreeze(o1); | ||
var o2 = iassign(o1, function (o) { return o; }, function (o) { o.a = { b: 1 }; return o; }); | ||
// | ||
// Jasmine Tests | ||
// | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }); | ||
// expect o2 inner property has been updated. | ||
expect(o2).toEqual({ a: { b: 1 }, a2: {} }); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a.b).not.toBe(o1.a.b); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2.a2).toBe(o1.a2); | ||
}); | ||
}); |
@@ -276,2 +276,106 @@ | ||
it("Test root object is an array", function () { | ||
var o1 = [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]]; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// | ||
// Calling iassign() and _.map() to increment to every item in "c" array | ||
// | ||
let o2 = iassign( | ||
o1, | ||
(o) => o[0], | ||
(o) => { | ||
return _.map(o, (item, index) => { | ||
if (index < 2) { | ||
item = _.cloneDeep(item); | ||
item.d++; | ||
} | ||
return item; | ||
}); | ||
} | ||
); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual([[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]]) | ||
// expect o2.a.b.c has been updated. | ||
expect(o2).toEqual([[{ d: 12, e: 12 }, { d: 14, e: 14 }, { d: 21, e: 22 }]]); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2[0]).not.toBe(o1[0]); | ||
expect(o2[0][0]).not.toBe(o1[0][0]); | ||
expect(o2[0][1]).not.toBe(o1[0][1]); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2[0][2]).toBe(o1[0][2]); | ||
}); | ||
it("Test root is an array, and try to update root array", function () { | ||
var o1 = [{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]; | ||
deepFreeze(o1); // Ensure o1 is not changed, for testing only | ||
// | ||
// Calling iassign() and _.map() to increment to every item in "c" array | ||
// | ||
let o2 = iassign( | ||
o1, | ||
(o) => o, | ||
(o) => { | ||
return _.map(o, (item, index) => { | ||
if (index < 2) { | ||
item = _.cloneDeep(item); | ||
item.d++; | ||
} | ||
return item; | ||
}); | ||
} | ||
); | ||
// expect o1 has not been changed | ||
expect(o1).toEqual([{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]) | ||
// expect o2.a.b.c has been updated. | ||
expect(o2).toEqual([{ d: 12, e: 12 }, { d: 14, e: 14 }, { d: 21, e: 22 }]); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2[0]).not.toBe(o1[0]); | ||
expect(o2[1]).not.toBe(o1[1]); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2[2]).toBe(o1[2]); | ||
}); | ||
it("Test root is an object, and try to update root object", function () { | ||
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }; | ||
deepFreeze(o1); | ||
var o2 = iassign( | ||
o1, | ||
(o) => o, | ||
(o) => { o.a = <any>{ b: 1 }; return o; } | ||
); | ||
// | ||
// Jasmine Tests | ||
// | ||
// expect o1 has not been changed | ||
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }]], c2: {} }, b2: {} }, a2: {} }); | ||
// expect o2 inner property has been updated. | ||
expect(o2).toEqual({ a: { b: 1}, a2: {} }); | ||
// expect object graph for changed property in o2 is now different from (!==) o1. | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a.b).not.toBe(o1.a.b); | ||
// expect object graph for unchanged property in o2 is still equal to (===) o1. | ||
expect(o2.a2).toBe(o1.a2); | ||
}); | ||
}); |
declare function iassign<TObj, TProp, TContext>(obj: TObj, getProp: (obj: TObj, context: TContext) => TProp, setProp: (prop: TProp) => TProp, context?: TContext): TObj; | ||
export = iassign; | ||
//export = iassign; | ||
declare module "immutable-assign" { | ||
export = iassign; | ||
} |
@@ -57,4 +57,7 @@ "use strict"; | ||
if (propIndex <= 0) { | ||
obj = quickCopy(obj); | ||
propValue = obj; | ||
propValue = quickCopy(obj); | ||
if (!accessorText) { | ||
propValue = setProp(propValue); | ||
} | ||
obj = propValue; | ||
} | ||
@@ -81,10 +84,3 @@ else { | ||
propValue = propValue[propName]; | ||
if (propValue != undefined && !(propValue instanceof Date)) { | ||
if (propValue instanceof Array) { | ||
propValue = propValue.slice(); | ||
} | ||
else if (typeof (propValue) === "object") { | ||
propValue = quickCopy(propValue); | ||
} | ||
} | ||
propValue = quickCopy(propValue); | ||
if (!accessorText) { | ||
@@ -152,7 +148,15 @@ propValue = setProp(propValue); | ||
function quickCopy(value) { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
if (value != undefined && !(value instanceof Date)) { | ||
if (value instanceof Array) { | ||
return value.slice(); | ||
} | ||
else if (typeof (value) === "object") { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
} | ||
} | ||
return copyValue; | ||
return value; | ||
} | ||
@@ -159,0 +163,0 @@ function evalStatement() { |
@@ -70,4 +70,9 @@ "use strict"; | ||
if (propIndex <= 0) { | ||
obj = quickCopy(obj); | ||
propValue = obj; | ||
propValue = quickCopy(obj); | ||
if (!accessorText) { | ||
propValue = setProp(propValue); | ||
} | ||
obj = propValue; | ||
} | ||
@@ -96,10 +101,3 @@ else { | ||
propValue = propValue[propName]; | ||
if (propValue != undefined && !(propValue instanceof Date)) { | ||
if (propValue instanceof Array) { | ||
propValue = propValue.slice(); | ||
} | ||
else if (typeof (propValue) === "object") { | ||
propValue = quickCopy(propValue); | ||
} | ||
} | ||
propValue = quickCopy(propValue) | ||
@@ -183,7 +181,17 @@ if (!accessorText) { | ||
function quickCopy<T>(value: T): T { | ||
let copyValue: any = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
if (value != undefined && !(value instanceof Date)) { | ||
if (value instanceof Array) { | ||
return (<any>value).slice(); | ||
} | ||
else if (typeof (value) === "object") { | ||
let copyValue: any = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
} | ||
} | ||
return copyValue; | ||
return value; | ||
} | ||
@@ -190,0 +198,0 @@ |
@@ -5,10 +5,10 @@ { | ||
"module": "commonjs", | ||
"jsx": "preserve", | ||
"declaration": true | ||
"jsx": "preserve" /*, | ||
"declaration": true */ | ||
}, | ||
"compileOnSave": true, | ||
"exclude": [ | ||
"node_modules", | ||
"bower_components" | ||
] | ||
"node_modules", | ||
"bower_components" | ||
] | ||
} |
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
655179
20427
21