immutable-assign
Advanced tools
Comparing version 2.0.6 to 2.0.8
@@ -1,9 +0,9 @@ | ||
"use strict"; | ||
'use strict'; | ||
(function (root, factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
try { | ||
var deepFreeze = require("deep-freeze-strict"); | ||
var deepFreeze = require('deep-freeze-strict'); | ||
} | ||
catch (ex) { | ||
console.warn("Cannot load deep-freeze-strict module, however you can still use iassign() function."); | ||
console.warn('Cannot load deep-freeze-strict module, however you can still use iassign() function.'); | ||
} | ||
@@ -14,4 +14,4 @@ var v = factory(deepFreeze, exports); | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["deep-freeze-strict", "exports"], factory); | ||
else if (typeof define === 'function' && define.amd) { | ||
define(['deep-freeze-strict', 'exports'], factory); | ||
} | ||
@@ -49,3 +49,5 @@ else { | ||
iassign.fp = autoCurry(_iassignFp); | ||
iassign.freeze = process.env.NODE_ENV !== "production"; | ||
iassign.maxGetPropCacheSize = 100; | ||
iassign.freeze = | ||
typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'; | ||
iassign.setOption = function (option) { | ||
@@ -56,6 +58,6 @@ copyOption(iassign, option); | ||
function _iassign(obj, // Object to set property, it will not be modified. | ||
getPropOrSetProp, // Function to get property to be updated. Must be pure function. | ||
setPropOrOption, // Function to set property. | ||
contextOrUndefined, // (Optional) Context to be used in getProp(). | ||
optionOrUndefined) { | ||
getPropOrSetProp, // Function to get property to be updated. Must be pure function. | ||
setPropOrOption, // Function to set property. | ||
contextOrUndefined, // (Optional) Context to be used in getProp(). | ||
optionOrUndefined) { | ||
var getProp = getPropOrSetProp; | ||
@@ -65,3 +67,3 @@ var setProp = setPropOrOption; | ||
var option = optionOrUndefined; | ||
if (typeof setPropOrOption !== "function") { | ||
if (typeof setPropOrOption !== 'function') { | ||
getProp = undefined; | ||
@@ -98,7 +100,7 @@ setProp = getPropOrSetProp; | ||
var funcText = getProp.toString(); | ||
var arrowIndex = funcText.indexOf("=>"); | ||
var arrowIndex = funcText.indexOf('=>'); | ||
if (arrowIndex <= -1) { | ||
var returnIndex = funcText.indexOf("return "); | ||
var returnIndex = funcText.indexOf('return '); | ||
if (returnIndex <= -1) { | ||
throw new Error("getProp() function does not return a part of obj"); | ||
throw new Error('getProp() function does not return a part of obj'); | ||
} | ||
@@ -108,3 +110,3 @@ } | ||
if (!propPaths) { | ||
throw new Error("getProp() function does not return a part of obj"); | ||
throw new Error('getProp() function does not return a part of obj'); | ||
} | ||
@@ -124,3 +126,5 @@ obj = updateProperty(obj, setProp, newValue, context, propPaths, option); | ||
var objCopy; | ||
if (typeof Proxy === "undefined") { | ||
var propValue; | ||
if (typeof Proxy === 'undefined') { | ||
propValue = getProp(obj, context); | ||
objCopy = _getPropPathViaProperty(obj, paths); | ||
@@ -132,2 +136,17 @@ } | ||
getProp(objCopy, context); | ||
// Check propValue === undefined for performance | ||
if (typeof Proxy === 'undefined' && propValue === undefined) { | ||
var functionInfo = parseGetPropFuncInfo(getProp, option); | ||
if (paths.length != functionInfo.funcTokens.length - 1) { | ||
var remainingFunctionTokens = functionInfo.funcTokens.slice(paths.length + 1); | ||
for (var _i = 0, remainingFunctionTokens_1 = remainingFunctionTokens; _i < remainingFunctionTokens_1.length; _i++) { | ||
var token = remainingFunctionTokens_1[_i]; | ||
if (token.propNameSource == ePropNameSource.inBracket && | ||
isNaN(token.propName)) { | ||
throw new Error("Cannot handle " + token.propName + " when the property it point to is undefined, which require unsafe feature of eval."); | ||
} | ||
} | ||
paths = paths.concat(remainingFunctionTokens.map(function (s) { return s.propName; })); | ||
} | ||
} | ||
return paths; | ||
@@ -141,3 +160,3 @@ } | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, propKey); | ||
if (descriptor && (!(obj instanceof Array) || propKey != "length")) { | ||
if (descriptor && (!(obj instanceof Array) || propKey != 'length')) { | ||
var copyDescriptor = { | ||
@@ -169,3 +188,3 @@ enumerable: descriptor.enumerable, | ||
paths.push(propKey); | ||
if (typeof propValue === "object" && propValue != null) { | ||
if (typeof propValue === 'object' && propValue != null) { | ||
return _getPropPathViaProxy(propValue, paths, level + 1); | ||
@@ -188,2 +207,7 @@ } | ||
target.copyFunc = defaultOption.copyFunc; | ||
target.disableAllCheck = defaultOption.disableAllCheck; | ||
target.disableHasReturnCheck = defaultOption.disableHasReturnCheck; | ||
target.disableExtraStatementCheck = | ||
defaultOption.disableExtraStatementCheck; | ||
target.maxGetPropCacheSize = defaultOption.maxGetPropCacheSize; | ||
target.ignoreIfNoChange = defaultOption.ignoreIfNoChange; | ||
@@ -207,2 +231,14 @@ } | ||
} | ||
if (option.disableAllCheck != undefined) { | ||
target.disableAllCheck = option.disableAllCheck; | ||
} | ||
if (option.disableHasReturnCheck != undefined) { | ||
target.disableHasReturnCheck = option.disableHasReturnCheck; | ||
} | ||
if (option.disableExtraStatementCheck != undefined) { | ||
target.disableExtraStatementCheck = option.disableExtraStatementCheck; | ||
} | ||
if (option.maxGetPropCacheSize != undefined) { | ||
target.maxGetPropCacheSize = option.maxGetPropCacheSize; | ||
} | ||
if (option.ignoreIfNoChange != undefined) { | ||
@@ -238,3 +274,3 @@ target.ignoreIfNoChange = option.ignoreIfNoChange; | ||
} | ||
else if (typeof value === "object") { | ||
else if (typeof value === 'object') { | ||
if (useConstructor) { | ||
@@ -254,21 +290,194 @@ var target = new value.constructor(); | ||
} | ||
function extend(destination) { | ||
var sources = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sources[_i - 1] = arguments[_i]; | ||
function extend(destination, source) { | ||
for (var key in source) { | ||
if (!Object.prototype.hasOwnProperty.call(source, key)) { | ||
continue; | ||
} | ||
var value = source[key]; | ||
destination[key] = value; | ||
} | ||
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) { | ||
var source = sources_1[_a]; | ||
for (var key in source) { | ||
if (!Object.prototype.hasOwnProperty.call(source, key)) { | ||
continue; | ||
return destination; | ||
} | ||
var ePropNameSource; | ||
(function (ePropNameSource) { | ||
ePropNameSource[ePropNameSource["none"] = 0] = "none"; | ||
ePropNameSource[ePropNameSource["beforeDot"] = 1] = "beforeDot"; | ||
ePropNameSource[ePropNameSource["beforeBracket"] = 2] = "beforeBracket"; | ||
ePropNameSource[ePropNameSource["inBracket"] = 3] = "inBracket"; | ||
ePropNameSource[ePropNameSource["last"] = 4] = "last"; | ||
})(ePropNameSource || (ePropNameSource = {})); | ||
var getPropCaches = {}; | ||
var getPropCacheKeys = []; | ||
function parseGetPropFuncInfo(func, option) { | ||
var funcText = func.toString(); | ||
var cacheKey = funcText + JSON.stringify(option); | ||
var info = getPropCaches[cacheKey]; | ||
if (getPropCaches[cacheKey]) { | ||
return info; | ||
} | ||
var matches = /\(([^\)]*)\)/.exec(funcText); | ||
var objParameterName = undefined; | ||
var cxtParameterName = undefined; | ||
if (matches) { | ||
var parametersText = matches[1]; | ||
var parameters = parametersText.split(','); | ||
objParameterName = parameters[0]; | ||
cxtParameterName = parameters[1]; | ||
} | ||
if (objParameterName) { | ||
objParameterName = objParameterName.trim(); | ||
} | ||
if (cxtParameterName) { | ||
cxtParameterName = cxtParameterName.trim(); | ||
} | ||
var bodyStartIndex = funcText.indexOf('{'); | ||
var bodyEndIndex = funcText.lastIndexOf('}'); | ||
var bodyText = ''; | ||
if (bodyStartIndex > -1 && bodyEndIndex > -1) { | ||
bodyText = funcText.substring(bodyStartIndex + 1, bodyEndIndex); | ||
} | ||
else { | ||
var arrowIndex = funcText.indexOf('=>'); | ||
if (arrowIndex > -1) { | ||
//console.log("Handle arrow function."); | ||
bodyText = 'return ' + funcText.substring(arrowIndex + 3); | ||
} | ||
else { | ||
throw new Error("Cannot parse function: " + funcText); | ||
} | ||
} | ||
var accessorTextInfo = getAccessorTextInfo(bodyText, option); | ||
info = { | ||
objParameterName: objParameterName, | ||
cxtParameterName: cxtParameterName, | ||
bodyText: bodyText, | ||
accessorText: accessorTextInfo.accessorText, | ||
quotedTextInfos: accessorTextInfo.quotedTextInfos, | ||
funcTokens: parseGetPropFuncTokens(accessorTextInfo.accessorText) | ||
}; | ||
if (option.maxGetPropCacheSize > 0) { | ||
getPropCaches[cacheKey] = info; | ||
getPropCacheKeys.push(cacheKey); | ||
if (getPropCacheKeys.length > option.maxGetPropCacheSize) { | ||
var cacheKeyToRemove = getPropCacheKeys.shift(); | ||
delete getPropCaches[cacheKeyToRemove]; | ||
} | ||
} | ||
return info; | ||
} | ||
function parseGetPropFuncTokens(accessorText) { | ||
var tokens = []; | ||
while (accessorText) { | ||
var openBracketIndex = accessorText.indexOf('['); | ||
var closeBracketIndex = accessorText.indexOf(']'); | ||
var dotIndex = accessorText.indexOf('.'); | ||
var propName = ''; | ||
var propNameSource = ePropNameSource.none; | ||
// if (dotIndex == 0) { | ||
// accessorText = accessorText.substr(dotIndex + 1); | ||
// continue; | ||
// } | ||
if (openBracketIndex > -1 && closeBracketIndex <= -1) { | ||
throw new Error('Found open bracket but not close bracket.'); | ||
} | ||
if (openBracketIndex <= -1 && closeBracketIndex > -1) { | ||
throw new Error('Found close bracket but not open bracket.'); | ||
} | ||
if (dotIndex > -1 && | ||
(dotIndex < openBracketIndex || openBracketIndex <= -1)) { | ||
propName = accessorText.substr(0, dotIndex); | ||
accessorText = accessorText.substr(dotIndex + 1); | ||
propNameSource = ePropNameSource.beforeDot; | ||
} | ||
else if (openBracketIndex > -1 && | ||
(openBracketIndex < dotIndex || dotIndex <= -1)) { | ||
if (openBracketIndex > 0) { | ||
propName = accessorText.substr(0, openBracketIndex); | ||
accessorText = accessorText.substr(openBracketIndex); | ||
propNameSource = ePropNameSource.beforeBracket; | ||
} | ||
var value = source[key]; | ||
if (value !== undefined) { | ||
destination[key] = value; | ||
else { | ||
propName = accessorText.substr(openBracketIndex + 1, closeBracketIndex - 1); | ||
accessorText = accessorText.substr(closeBracketIndex + 1); | ||
propNameSource = ePropNameSource.inBracket; | ||
} | ||
} | ||
else { | ||
propName = accessorText; | ||
accessorText = ''; | ||
propNameSource = ePropNameSource.last; | ||
} | ||
propName = propName.trim(); | ||
if (propName == '') { | ||
continue; | ||
} | ||
//console.log(propName); | ||
tokens.push({ | ||
propName: propName, | ||
propNameSource: propNameSource, | ||
subAccessorText: accessorText | ||
}); | ||
} | ||
return destination; | ||
return tokens; | ||
} | ||
function getAccessorTextInfo(bodyText, option) { | ||
var returnIndex = bodyText.indexOf('return '); | ||
if (!option.disableAllCheck && !option.disableHasReturnCheck) { | ||
if (returnIndex <= -1) { | ||
throw new Error("getProp() function has no 'return' keyword."); | ||
} | ||
} | ||
if (!option.disableAllCheck && !option.disableExtraStatementCheck) { | ||
var otherBodyText = bodyText.substr(0, returnIndex); | ||
otherBodyText = otherBodyText.replace(/['"]use strict['"];*/g, ''); | ||
otherBodyText = otherBodyText.trim(); | ||
if (otherBodyText != '') { | ||
throw new Error("getProp() function has statements other than 'return': " + | ||
otherBodyText); | ||
} | ||
} | ||
var accessorText = bodyText.substr(returnIndex + 7).trim(); | ||
if (accessorText[accessorText.length - 1] == ';') { | ||
accessorText = accessorText.substring(0, accessorText.length - 1); | ||
} | ||
accessorText = accessorText.trim(); | ||
return parseTextInQuotes(accessorText, option); | ||
} | ||
function parseTextInQuotes(accessorText, option) { | ||
var quotedTextInfos = {}; | ||
var index = 0; | ||
while (true) { | ||
var singleQuoteIndex = accessorText.indexOf("'"); | ||
var doubleQuoteIndex = accessorText.indexOf('"'); | ||
var varName = '#' + index++; | ||
if (singleQuoteIndex <= -1 && doubleQuoteIndex <= -1) | ||
break; | ||
var matches = undefined; | ||
var quoteIndex = void 0; | ||
if (doubleQuoteIndex > -1 && | ||
(doubleQuoteIndex < singleQuoteIndex || singleQuoteIndex <= -1)) { | ||
matches = /("[^"\\]*(?:\\.[^"\\]*)*")/.exec(accessorText); | ||
quoteIndex = doubleQuoteIndex; | ||
} | ||
else if (singleQuoteIndex > -1 && | ||
(singleQuoteIndex < doubleQuoteIndex || doubleQuoteIndex <= -1)) { | ||
matches = /('[^'\\]*(?:\\.[^'\\]*)*')/.exec(accessorText); | ||
quoteIndex = singleQuoteIndex; | ||
} | ||
if (matches) { | ||
quotedTextInfos[varName] = matches[1]; | ||
accessorText = | ||
accessorText.substr(0, quoteIndex) + | ||
varName + | ||
accessorText.substr(matches.index + matches[1].length); | ||
} | ||
else { | ||
throw new Error('Invalid text in quotes: ' + accessorText); | ||
} | ||
} | ||
return { | ||
accessorText: accessorText, | ||
quotedTextInfos: quotedTextInfos | ||
}; | ||
} | ||
iassign.default = iassign; | ||
@@ -275,0 +484,0 @@ iassign.deepFreeze = function (obj) { return (iassign.freeze ? deepFreeze(obj) : obj); }; |
{ | ||
"name": "immutable-assign", | ||
"version": "2.0.6", | ||
"description": | ||
"Lightweight immutable helper that allows you to continue working with Plain JavaScript Objects", | ||
"version": "2.0.8", | ||
"description": "Lightweight immutable helper that allows you to continue working with Plain JavaScript Objects", | ||
"main": "deploy/iassign.js", | ||
"types": "deploy/iassign.d.ts", | ||
"scripts": { | ||
"test": | ||
"node node_modules/istanbul/lib/cli.js cover node_modules/jasmine/bin/jasmine.js", | ||
"test-backup": "node node_modules/jasmine/bin/jasmine.js", | ||
"test": "node node_modules/istanbul/lib/cli.js cover node_modules/jasmine/bin/jasmine.js", | ||
"test-karma": "node_modules/.bin/karma start", | ||
"test-karma-win": "node_modules/.bin/karma start --browsers Chrome,Edge,IE", | ||
"test-karma-win10": "node_modules/.bin/karma start --browsers Chrome,Fireforx,Edge,IE", | ||
"test-karma-win7": "node_modules/.bin/karma start --browsers Chrome,Fireforx,IE", | ||
"test-karma-mac": "node_modules/.bin/karma start --browsers Safari,Chrome", | ||
"test-karma-mac-no-proxy": "NO_PROXY='true' yarn run test-karma-mac", | ||
"cover": | ||
"node node_modules/istanbul/lib/cli.js cover node_modules/jasmine/bin/jasmine.js", | ||
"debug": "node --inspect --inspect-brk node_modules/jasmine/bin/jasmine.js", | ||
@@ -33,3 +29,9 @@ "build": "gulp", | ||
"license": "MIT", | ||
"keywords": ["immutable", "typescript", "javascript", "data", "stateless"], | ||
"keywords": [ | ||
"immutable", | ||
"typescript", | ||
"javascript", | ||
"data", | ||
"stateless" | ||
], | ||
"dependencies": {}, | ||
@@ -40,30 +42,31 @@ "optionalDependencies": { | ||
"devDependencies": { | ||
"@types/lodash": "^4.14.74", | ||
"chalk": "^1.1.3", | ||
"core-js": "^2.4.1", | ||
"coveralls": "^2.11.15", | ||
"deep-freeze": "0.0.1", | ||
"edge-launcher": "*", | ||
"@types/lodash": "^4.14.116", | ||
"chalk": "^2.4.1", | ||
"core-js": "^2.5.7", | ||
"coveralls": "^3.0.2", | ||
"deep-freeze": "^0.0.1", | ||
"edge-launcher": "^1.2.2", | ||
"gulp": "^3.9.1", | ||
"gulp-less": "^3.1.0", | ||
"gulp-typescript": "^3.1.6", | ||
"immutable": "^3.8.1", | ||
"istanbul": "^0.4.3", | ||
"jasmine": "^2.4.1", | ||
"karma": "^1.1.2", | ||
"karma-chrome-launcher": "^1.0.1", | ||
"gulp-less": "^4.0.1", | ||
"gulp-typescript": "^5.0.0-alpha.3", | ||
"immer": "^1.5.0", | ||
"immutable": "^3.8.2", | ||
"istanbul": "^0.4.5", | ||
"jasmine": "^3.2.0", | ||
"karma": "^3.0.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-edge-launcher": "^0.4.2", | ||
"karma-firefox-launcher": "^1.0.0", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-ie-launcher": "^1.0.0", | ||
"karma-jasmine": "^1.0.2", | ||
"karma-phantomjs-launcher": "^1.0.1", | ||
"karma-jasmine": "^1.1.2", | ||
"karma-phantomjs-launcher": "^1.0.4", | ||
"karma-safari-launcher": "^1.0.0", | ||
"karma-sauce-launcher": "^1.1.0", | ||
"lodash": "^4.13.1", | ||
"merge2": "^1.0.2", | ||
"seamless-immutable": "^7.0.1", | ||
"timm": "^1.2.3", | ||
"typescript": "^2.3.2", | ||
"vinyl-source-stream": "^1.1.0" | ||
"karma-sauce-launcher": "^1.2.0", | ||
"lodash": "^4.17.10", | ||
"merge2": "^1.2.2", | ||
"seamless-immutable": "^7.1.3", | ||
"timm": "^1.6.1", | ||
"typescript": "^3.0.1", | ||
"vinyl-source-stream": "^2.0.0" | ||
} | ||
} | ||
} |
@@ -404,2 +404,3 @@ # immutable-assign (iassign.js) | ||
* 2.0.8 - Fixed bug for undefined properties. | ||
* 2.0.4 - Replaced the proxy-polyfill with Object.defineProperty(), which has much better browser support. | ||
@@ -406,0 +407,0 @@ * 2.0.1 - Minor bug fixes. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
7469
437
1545721
28
19