Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

immutable-assign

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immutable-assign - npm Package Compare versions

Comparing version 1.0.25 to 1.0.26

292

deploy/iassign.js
"use strict";
(function (root, factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports);
try {
var deepFreeze = require("deep-freeze");
}
catch (ex) {
console.warn("Cannot load deep-freeze module, however you can still use iassign() function.");
}
var v = factory(deepFreeze, exports);
if (v !== undefined)

@@ -9,23 +15,17 @@ module.exports = v;

else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
define(["deep-freeze", "exports"], factory);
}
else {
// Browser globals (root is window)
var browserRequire = function (name) {
if (name == "deep-freeze" && root.deepFreeze) {
return root.deepFreeze;
}
throw new Error("Unable to require: " + name);
};
root.iassign = factory(browserRequire, {});
root.iassign = factory(root.deepFreeze, {});
}
})(this, function (require, exports) {
})(this, function (deepFreeze, exports) {
//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.");
}
// 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 iassign = _iassign;
iassign.maxGetPropCacheSize = 100;
// Immutable Assign

@@ -47,8 +47,3 @@ function _iassign(obj, // Object to set property, it will not be modified.

}
if (option) {
option = extend({}, iassign, option);
}
else {
option = iassign;
}
option = copyOption(option);
if (deepFreeze && (option.freeze || option.freezeInput)) {

@@ -65,91 +60,69 @@ deepFreeze(obj);

var getPropFuncInfo = parseGetPropFuncInfo(getProp, option);
var accessorText = getPropFuncInfo.accessorText;
var propIndex = 0;
var propValue = undefined;
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.");
obj = updateProperty(obj, setProp, context, getPropFuncInfo);
}
if (deepFreeze && (option.freeze || option.freezeOutput)) {
deepFreeze(obj);
}
return obj;
}
// For performance
function copyOption(option) {
var newOption = {};
newOption.freeze = iassign.freeze;
newOption.freezeInput = iassign.freezeInput;
newOption.freezeOutput = iassign.freezeOutput;
newOption.disableAllCheck = iassign.disableAllCheck;
newOption.disableHasReturnCheck = iassign.disableHasReturnCheck;
newOption.disableExtraStatementCheck = iassign.disableExtraStatementCheck;
newOption.maxGetPropCacheSize = iassign.maxGetPropCacheSize;
if (option) {
if (option.freeze != undefined) {
newOption.freeze = option.freeze;
}
if (option.freezeInput != undefined) {
newOption.freezeInput = option.freezeInput;
}
if (option.freezeOutput != undefined) {
newOption.freezeOutput = option.freezeOutput;
}
if (option.disableAllCheck != undefined) {
newOption.disableAllCheck = option.disableAllCheck;
}
if (option.disableHasReturnCheck != undefined) {
newOption.disableHasReturnCheck = option.disableHasReturnCheck;
}
if (option.disableExtraStatementCheck != undefined) {
newOption.disableExtraStatementCheck = option.disableExtraStatementCheck;
}
if (option.maxGetPropCacheSize != undefined) {
newOption.maxGetPropCacheSize = option.maxGetPropCacheSize;
}
}
return newOption;
}
function updateProperty(obj, setProp, context, getPropFuncInfo) {
var propValue = undefined;
for (var propIndex = 0; propIndex < getPropFuncInfo.funcTokens.length; ++propIndex) {
var _a = getPropFuncInfo.funcTokens[propIndex], propName = _a.propName, propNameSource = _a.propNameSource, subAccessorText = _a.subAccessorText, getPropName = _a.getPropName;
//console.log(propName);
if (propIndex <= 0) {
propValue = quickCopy(obj);
if (!subAccessorText) {
propValue = setProp(propValue);
}
if (openBracketIndex <= -1 && closeBracketIndex > -1) {
throw new Error("Found close bracket but not open bracket.");
obj = propValue;
}
else {
var prevPropValue = propValue;
if (propName == undefined) {
propName = getPropName(obj, context);
}
if (dotIndex > -1 && (dotIndex < openBracketIndex || openBracketIndex <= -1)) {
propName = accessorText.substr(0, dotIndex);
accessorText = accessorText.substr(dotIndex + 1);
propNameSource = ePropNameSource.beforeDot;
propValue = propValue[propName];
propValue = quickCopy(propValue);
if (!subAccessorText) {
propValue = setProp(propValue);
}
else if (openBracketIndex > -1 && (openBracketIndex < dotIndex || dotIndex <= -1)) {
if (openBracketIndex > 0) {
propName = accessorText.substr(0, openBracketIndex);
accessorText = accessorText.substr(openBracketIndex);
propNameSource = ePropNameSource.beforeBracket;
}
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);
if (propIndex <= 0) {
propValue = quickCopy(obj);
if (!accessorText) {
propValue = setProp(propValue);
}
obj = propValue;
}
else {
var prevPropValue = propValue;
if (propNameSource == ePropNameSource.inBracket && isNaN(propName)) {
if (propName[0] == "#") {
var quotedPropName = getPropFuncInfo.quotedTextInfos[propName];
if (!quotedPropName) {
throw new Error("Cannot find quoted text for " + quotedPropName);
}
propName = eval(quotedPropName);
}
else {
var statement = "'use strict';\n";
if (getPropFuncInfo.objParameterName) {
statement += "var " + getPropFuncInfo.objParameterName + " = arguments[1];\n";
}
if (getPropFuncInfo.cxtParameterName) {
statement += "var " + getPropFuncInfo.cxtParameterName + " = arguments[2];\n";
}
statement += "" + propName;
propName = evalStatement(statement, obj, context);
}
}
propValue = propValue[propName];
propValue = quickCopy(propValue);
if (!accessorText) {
propValue = setProp(propValue);
}
prevPropValue[propName] = propValue;
}
//console.log(propValue);
propIndex++;
prevPropValue[propName] = propValue;
}
}
if (deepFreeze && (option.freeze || option.freezeOutput)) {
deepFreeze(obj);
}
return obj;

@@ -165,4 +138,11 @@ }

})(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);

@@ -185,3 +165,3 @@ var objParameterName = undefined;

var accessorTextInfo = getAccessorTextInfo(bodyText, option);
return {
info = {
objParameterName: objParameterName,

@@ -192,4 +172,102 @@ cxtParameterName: cxtParameterName,

quotedTextInfos: accessorTextInfo.quotedTextInfos,
funcTokens: parseGetPropFuncTokens(accessorTextInfo.accessorText),
};
postProcessTokens(info);
if (option.maxGetPropCacheSize > 0) {
getPropCaches[cacheKey] = info;
getPropCacheKeys.push(cacheKey);
if (getPropCacheKeys.length > option.maxGetPropCacheSize) {
debugger;
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;
}
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 tokens;
}
function postProcessTokens(getPropFuncInfo) {
var _loop_1 = function() {
var token = getPropFuncInfo.funcTokens[propIndex];
var propName = token.propName, propNameSource = token.propNameSource, subAccessorText = token.subAccessorText;
if (propNameSource == ePropNameSource.inBracket && isNaN(propName)) {
if (propName[0] == "#") {
var quotedPropName = getPropFuncInfo.quotedTextInfos[propName];
if (!quotedPropName) {
throw new Error("Cannot find quoted text for " + quotedPropName);
}
propName = eval(quotedPropName);
token.propName = propName;
}
else {
var statement_1 = "'use strict';\n";
if (getPropFuncInfo.objParameterName) {
statement_1 += "var " + getPropFuncInfo.objParameterName + " = arguments[1];\n";
}
if (getPropFuncInfo.cxtParameterName) {
statement_1 += "var " + getPropFuncInfo.cxtParameterName + " = arguments[2];\n";
}
statement_1 += "" + propName;
token.propName = undefined;
token.getPropName = function (obj, context) {
return evalStatement(statement_1, obj, context);
};
}
}
};
for (var propIndex = 0; propIndex < getPropFuncInfo.funcTokens.length; ++propIndex) {
_loop_1();
}
}
function getAccessorTextInfo(bodyText, option) {

@@ -196,0 +274,0 @@ var returnIndex = bodyText.indexOf("return ");

@@ -161,4 +161,4 @@ // Karma configuration

},
customLaunchers: customLaunchers,
browsers: Object.keys(customLaunchers),
// customLaunchers: customLaunchers,
// browsers: Object.keys(customLaunchers),
reporters: ['dots', 'saucelabs'],

@@ -172,3 +172,3 @@

// Refer to http://karma-runner.github.io/1.0/config/browsers.html
//browsers: ["Chrome", "Firefox", "IE", "PhantomJS"],
browsers: ["Chrome", "Firefox", "IE", "PhantomJS"],

@@ -175,0 +175,0 @@ // To Run Edge, please install https://github.com/nicolasmccurdy/karma-edge-launcher manually.

{
"name": "immutable-assign",
"version": "1.0.25",
"version": "1.0.26",
"description": "Lightweight immutable helper that allows you to continue working with Plain JavaScript Objects",

@@ -5,0 +5,0 @@ "main": "src/iassign.js",

@@ -364,2 +364,3 @@ # immutable-assign (iassign.js)

* 1.0.26 - Works with webpack, please refer to [ImmutableAssignTest](https://github.com/engineforce/ImmutableAssignTest)
* 1.0.23 - Greatly improved performance.

@@ -366,0 +367,0 @@ * 1.0.21 - Added new function overload to skip getProp() if you trying to update the root object, refer to [example 1](#example-1-update-object) and [example 2](#example-2-update-listarray)

@@ -33,3 +33,11 @@ "use strict";

var iassign = require("../src/iassign");
var deepFreeze = require("deep-freeze");
var noDeepFreeze = false;
try {
var deepFreeze = require("deep-freeze");
}
catch(ex) {
deepFreeze = function() {};
noDeepFreeze = true;
console.warn("Cannot load deep-freeze module.");
}
var _ = require("lodash");

@@ -310,2 +318,5 @@

xit("extra '[' should throw exception", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };

@@ -312,0 +323,0 @@ deepFreeze(o1);

@@ -29,3 +29,11 @@ "use strict";

var iassign = require("../src/iassign");
var deepFreeze = require("deep-freeze");
var noDeepFreeze = false;
try {
var deepFreeze = require("deep-freeze");
}
catch (ex) {
deepFreeze = function () { };
noDeepFreeze = true;
console.warn("Cannot load deep-freeze module.");
}
var _ = require("lodash");

@@ -235,2 +243,4 @@ describe("Test", function () {

it("Try to modify freezed object should throw error.", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };

@@ -361,2 +371,4 @@ deepFreeze(o1);

it("Use built-in deep freeze to protect input", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };

@@ -379,2 +391,4 @@ iassign.freezeInput = true;

it("Use built-in deep freeze to protect output", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } };

@@ -424,2 +438,4 @@ iassign.freezeOutput = true;

it("Example 1b: update object with option", function () {
if (noDeepFreeze)
return;
//var iassign = require("immutable-assign");

@@ -453,2 +469,4 @@ // Deep freeze both input and output, can be used in development to make sure they don't change.

it("Example 1d: update object with option that pass undefined to getProp()", function () {
if (noDeepFreeze)
return;
//var iassign = require("immutable-assign");

@@ -455,0 +473,0 @@ // Deep freeze both input and output, can be used in development to make sure they don't change.

@@ -32,3 +32,11 @@

var iassign: IIassign = require("../src/iassign");
var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze");
var noDeepFreeze = false;
try {
var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze");
} catch (ex) {
deepFreeze = function () { };
noDeepFreeze = true;
console.warn("Cannot load deep-freeze module.");
}
var _: _.LoDashStatic = require("lodash");

@@ -293,2 +301,5 @@

it("Try to modify freezed object should throw error.", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };

@@ -477,2 +488,5 @@ deepFreeze(o1);

it("Use built-in deep freeze to protect input", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };

@@ -501,2 +515,5 @@ iassign.freezeInput = true;

it("Use built-in deep freeze to protect output", function () {
if (noDeepFreeze)
return;
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }, { d: 13, e: 14 }, { d: 21, e: 22 }]] } } };

@@ -568,2 +585,5 @@ iassign.freezeOutput = true;

it("Example 1b: update object with option", function () {
if (noDeepFreeze)
return;
//var iassign = require("immutable-assign");

@@ -624,2 +644,5 @@

it("Example 1d: update object with option that pass undefined to getProp()", function () {
if (noDeepFreeze)
return;
//var iassign = require("immutable-assign");

@@ -633,3 +656,3 @@

// 1: Calling iassign() to update map1.b
var map2 = iassign<any, {b: number}, any>(
var map2 = iassign<any, { b: number }, any>(
map1,

@@ -646,3 +669,3 @@ undefined,

var map3 = iassign<any, {c: number}, any>(
var map3 = iassign<any, { c: number }, any>(
map2,

@@ -649,0 +672,0 @@ undefined,

"use strict";
(function (root, factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports);
try {
var deepFreeze = require("deep-freeze");
}
catch (ex) {
console.warn("Cannot load deep-freeze module, however you can still use iassign() function.");
}
var v = factory(deepFreeze, exports);
if (v !== undefined)

@@ -9,22 +15,15 @@ module.exports = v;

else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
define(["deep-freeze", "exports"], factory);
}
else {
// Browser globals (root is window)
var browserRequire = function (name) {
if (name == "deep-freeze" && root.deepFreeze) {
return root.deepFreeze;
}
throw new Error("Unable to require: " + name);
};
root.iassign = factory(browserRequire, {});
root.iassign = factory(root.deepFreeze, {});
}
})(this, function (require, exports) {
})(this, function (deepFreeze, exports) {
//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.");
}
// 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 iassign = _iassign;

@@ -237,3 +236,3 @@ iassign.maxGetPropCacheSize = 100;

function postProcessTokens(getPropFuncInfo) {
var _loop_1 = function () {
var _loop_1 = function() {
var token = getPropFuncInfo.funcTokens[propIndex];

@@ -240,0 +239,0 @@ var propName = token.propName, propNameSource = token.propNameSource, subAccessorText = token.subAccessorText;

@@ -51,26 +51,25 @@ "use strict";

if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
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 v = factory(deepFreeze, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
define(["deep-freeze", "exports"], factory);
} else {
// Browser globals (root is window)
let browserRequire = (name) => {
if (name == "deep-freeze" && root.deepFreeze) {
return root.deepFreeze;
}
throw new Error("Unable to require: " + name);
}
root.iassign = factory(browserRequire, {});
root.iassign = factory(root.deepFreeze, {});
}
})(this, function (require, exports) {
})(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.");
}
// try {
// var deepFreeze: DeepFreeze.DeepFreezeInterface = require("deep-freeze");
// } catch (ex) {
// console.warn("Cannot load deep-freeze module, however you can still use iassign() function.");
// }

@@ -77,0 +76,0 @@ var iassign: IIassign = <any>_iassign;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc