immutable-assign
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -117,7 +117,12 @@ "use strict"; | ||
var paths = []; | ||
_getPropPath(obj, objCopy, paths); | ||
if (typeof Proxy === "undefined") { | ||
_getPropPathViaProperty(obj, objCopy, paths); | ||
} | ||
else { | ||
objCopy = _getPropPathViaProxy(obj, objCopy, paths); | ||
} | ||
getProp(objCopy, context); | ||
return paths; | ||
} | ||
function _getPropPath(obj, objCopy, paths, level) { | ||
function _getPropPathViaProperty(obj, objCopy, paths, level) { | ||
if (level === void 0) { level = 0; } | ||
@@ -137,3 +142,3 @@ var propertyNames = Object.getOwnPropertyNames(obj); | ||
if (propValue != undefined) { | ||
_getPropPath(propValue, propValueCopy, paths, level + 1); | ||
_getPropPathViaProperty(propValue, propValueCopy, paths, level + 1); | ||
} | ||
@@ -149,2 +154,22 @@ return propValueCopy; | ||
} | ||
function _getPropPathViaProxy(obj, objCopy, paths, level) { | ||
if (level === void 0) { level = 0; } | ||
var handlers = { | ||
get: function (target, propKey) { | ||
if (level == paths.length) { | ||
paths.push(propKey); | ||
var propValue = obj[propKey]; | ||
if (typeof propValue === "object") { | ||
var propValueCopy = quickCopy(propValue); | ||
if (propValue != undefined) { | ||
propValueCopy = _getPropPathViaProxy(propValue, propValueCopy, paths, level + 1); | ||
} | ||
return propValueCopy; | ||
} | ||
} | ||
return obj[propKey]; | ||
}, | ||
}; | ||
return new Proxy(objCopy, handlers); | ||
} | ||
// For performance | ||
@@ -151,0 +176,0 @@ function copyOption(target, option, defaultOption) { |
{ | ||
"name": "immutable-assign", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": | ||
@@ -5,0 +5,0 @@ "Lightweight immutable helper that allows you to continue working with Plain JavaScript Objects", |
107
README.md
@@ -51,54 +51,2 @@ # immutable-assign (iassign.js) | ||
### Function Signature (TypeScript syntax) | ||
```javascript | ||
// Return a new POJO object with property updated. | ||
// function overload 1: | ||
iassign = function<TObj, TProp, TContext>( | ||
obj: TObj, // POJO object to be getting the property from, it will not be modified. | ||
getProp: (obj: TObj, context: TContext) => TProp, // Function to get the property that needs to be updated. | ||
setProp: (prop: TProp) => TProp, // Function to set the property. | ||
context?: TContext, // (Optional) Context to be used in getProp(). | ||
option?: IIassignOption): TObj; // (Optional) Options | ||
// function overload 2: you can skip getProp() if you trying to update the root object, refer to example 1 and 2 | ||
iassign = function<TObj>( | ||
obj: TObj, // POJO object to be getting the property from, it will not be modified. | ||
setProp: setPropFunc<TObj>, // Function to set the property. | ||
option?: IIassignOption): TObj; // (Optional) Options | ||
// functional programming friendly style, moved obj to the last parameter and supports currying, refer to example 8 | ||
iassign.fp = function <TObj, TProp, TContext>( | ||
option: IIassignOption, | ||
getProp: getPropFunc<TObj, TProp, TContext>, | ||
setProp: setPropFunc<TProp>, | ||
context?: TContext, | ||
obj?: TObj): TObj; // POJO object to be getting the property from, it will not be modified. | ||
// In ES6, you cannot set property on imported module directly, because they are default | ||
// to readonly, in this case you need to use this method. | ||
iassign.setOption(option: IIassignOption): void; | ||
// Options, can be applied globally or individually | ||
interface IIassignOption { | ||
freeze?: boolean; // Deep freeze both input and output | ||
freezeInput?: boolean; // Deep freeze input | ||
freezeOutput?: boolean; // Deep freeze output | ||
useConstructor?: boolean; // Uses the constructor to create new instances | ||
// Custom copy function, can be used to handle special types, e.g., Map, Set; refer to example 9 | ||
copyFunc?: <T>(value: T, propName: string): T; | ||
// Disable validation for extra statements in the getProp() function, | ||
// which is needed when running the coverage, e.g., istanbul.js does add | ||
// instrument statements in our getProp() function, which can be safely ignored. | ||
disableExtraStatementCheck?: boolean; | ||
// Return the same object if setProp() returns its parameter (i.e., reference pointer not changed). | ||
ignoreIfNoChange?: boolean; | ||
} | ||
``` | ||
<br /> | ||
@@ -475,3 +423,56 @@ | ||
<br /> | ||
### Function Signature (TypeScript syntax) | ||
```javascript | ||
// Return a new POJO object with property updated. | ||
// function overload 1: | ||
iassign = function<TObj, TProp, TContext>( | ||
obj: TObj, // POJO object to be getting the property from, it will not be modified. | ||
getProp: (obj: TObj, context: TContext) => TProp, // Function to get the property that needs to be updated. | ||
setProp: (prop: TProp) => TProp, // Function to set the property. | ||
context?: TContext, // (Optional) Context to be used in getProp(). | ||
option?: IIassignOption): TObj; // (Optional) Options | ||
// function overload 2: you can skip getProp() if you trying to update the root object, refer to example 1 and 2 | ||
iassign = function<TObj>( | ||
obj: TObj, // POJO object to be getting the property from, it will not be modified. | ||
setProp: setPropFunc<TObj>, // Function to set the property. | ||
option?: IIassignOption): TObj; // (Optional) Options | ||
// functional programming friendly style, moved obj to the last parameter and supports currying, refer to example 8 | ||
iassign.fp = function <TObj, TProp, TContext>( | ||
option: IIassignOption, | ||
getProp: getPropFunc<TObj, TProp, TContext>, | ||
setProp: setPropFunc<TProp>, | ||
context?: TContext, | ||
obj?: TObj): TObj; // POJO object to be getting the property from, it will not be modified. | ||
// In ES6, you cannot set property on imported module directly, because they are default | ||
// to readonly, in this case you need to use this method. | ||
iassign.setOption(option: IIassignOption): void; | ||
// Options, can be applied globally or individually | ||
interface IIassignOption { | ||
freeze?: boolean; // Deep freeze both input and output | ||
freezeInput?: boolean; // Deep freeze input | ||
freezeOutput?: boolean; // Deep freeze output | ||
useConstructor?: boolean; // Uses the constructor to create new instances | ||
// Custom copy function, can be used to handle special types, e.g., Map, Set; refer to example 9 | ||
copyFunc?: <T>(value: T, propName: string): T; | ||
// Disable validation for extra statements in the getProp() function, | ||
// which is needed when running the coverage, e.g., istanbul.js does add | ||
// instrument statements in our getProp() function, which can be safely ignored. | ||
disableExtraStatementCheck?: boolean; | ||
// Return the same object if setProp() returns its parameter (i.e., reference pointer not changed). | ||
ignoreIfNoChange?: boolean; | ||
} | ||
``` | ||
## Constraints | ||
@@ -484,3 +485,3 @@ | ||
* 2.0.2 - Removed dependency on ES6 Proxy (including the proxy-polyfill), uses Object.defineProperty() instead which has much better browser support. | ||
* 2.0.3 - Replaced the proxy-polyfill with Object.defineProperty(), which has much better browser support. | ||
* 2.0.1 - Minor bug fixes. | ||
@@ -487,0 +488,0 @@ * 2.0.0 - |
@@ -5,4 +5,8 @@ { | ||
"module": "commonjs", | ||
"lib": ["es2015", "dom"], | ||
"lib": [ | ||
"es2015", | ||
"dom" | ||
], | ||
"removeComments": false, | ||
"noUnusedLocals": true, | ||
"jsx": "preserve" /*, | ||
@@ -9,0 +13,0 @@ "declaration": true */ |
1518507
7255
515