immutable-assign
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -5,5 +5,5 @@ | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
} | ||
@@ -16,3 +16,4 @@ | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
context?: TContext, | ||
option?: IIassignOption): TObj; | ||
} | ||
@@ -19,0 +20,0 @@ } |
@@ -14,4 +14,11 @@ "use strict"; | ||
setProp, // Function to set property. | ||
context) { | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
context, // (Optional) Context to be used in getProp(). | ||
option) { | ||
if (option) { | ||
option = extend({}, iassign, option); | ||
} | ||
else { | ||
option = iassign; | ||
} | ||
if (deepFreeze && (option.freeze || option.freezeInput)) { | ||
deepFreeze(obj); | ||
@@ -21,4 +28,4 @@ } | ||
var value = getProp(obj, context); | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp); | ||
var accessorText = getAccessorText(getPropFuncInfo.bodyText); | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp, option); | ||
var accessorText = getPropFuncInfo.accessorText; | ||
var propIndex = 0; | ||
@@ -79,4 +86,10 @@ var propValue = undefined; | ||
if (propNameSource == ePropNameSource.inBracket && isNaN(propName)) { | ||
var propNameInQuote = extractTextInQuote(propName); | ||
if (propNameInQuote == undefined) { | ||
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"; | ||
@@ -92,5 +105,2 @@ if (getPropFuncInfo.objParameterName) { | ||
} | ||
else { | ||
propName = propNameInQuote; | ||
} | ||
} | ||
@@ -107,3 +117,3 @@ propValue = propValue[propName]; | ||
} | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
if (deepFreeze && (option.freeze || option.freezeOutput)) { | ||
deepFreeze(obj); | ||
@@ -121,3 +131,3 @@ } | ||
})(ePropNameSource || (ePropNameSource = {})); | ||
function parseGetPropFuncInfo(func) { | ||
function parseGetPropFuncInfo(func, option) { | ||
var funcText = func.toString(); | ||
@@ -139,11 +149,15 @@ var matches = /\(([^\)]*)\)/.exec(funcText); | ||
} | ||
var bodyText = funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")); | ||
var accessorTextInfo = getAccessorTextInfo(bodyText, option); | ||
return { | ||
objParameterName: objParameterName, | ||
cxtParameterName: cxtParameterName, | ||
bodyText: funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")) | ||
bodyText: bodyText, | ||
accessorText: accessorTextInfo.accessorText, | ||
quotedTextInfos: accessorTextInfo.quotedTextInfos, | ||
}; | ||
} | ||
function getAccessorText(bodyText) { | ||
function getAccessorTextInfo(bodyText, option) { | ||
var returnIndex = bodyText.indexOf("return "); | ||
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) { | ||
if (!option.disableAllCheck && !option.disableHasReturnCheck) { | ||
if (returnIndex <= -1) { | ||
@@ -153,3 +167,3 @@ throw new Error("getProp() function has no 'return' keyword."); | ||
} | ||
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) { | ||
if (!option.disableAllCheck && !option.disableExtraStatementCheck) { | ||
var otherBodyText = bodyText.substr(0, returnIndex).trim(); | ||
@@ -165,4 +179,39 @@ if (otherBodyText != "") { | ||
accessorText = accessorText.trim(); | ||
return accessorText; | ||
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, | ||
}; | ||
} | ||
function quickCopy(value) { | ||
@@ -174,7 +223,3 @@ if (value != undefined && !(value instanceof Date)) { | ||
else if (typeof (value) === "object") { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
return extend({}, value); | ||
} | ||
@@ -184,28 +229,21 @@ } | ||
} | ||
function evalStatement() { | ||
return eval(arguments[0]); | ||
} | ||
// function isTextInQuote(text: string): boolean { | ||
// let quoteMarks = ["'", '"']; | ||
// for (let mark of quoteMarks) { | ||
// if (text[0] == mark && text[text.length-1] == mark) { | ||
// return true; | ||
// } | ||
// } | ||
// return false; | ||
// } | ||
function extractTextInQuote(text) { | ||
var quoteMarks = ["'", '"']; | ||
for (var _i = 0, quoteMarks_1 = quoteMarks; _i < quoteMarks_1.length; _i++) { | ||
var mark = quoteMarks_1[_i]; | ||
if (text[0] == mark) { | ||
var regex = new RegExp("^[" + mark + "]([^" + mark + "]*)[" + mark + "]$"); | ||
var match = regex.exec(text); | ||
if (match) { | ||
return match[1]; | ||
function extend(destination) { | ||
var sources = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sources[_i - 1] = arguments[_i]; | ||
} | ||
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) { | ||
var source = sources_1[_a]; | ||
for (var key in source) { | ||
var value = source[key]; | ||
if (value !== undefined) { | ||
destination[key] = value; | ||
} | ||
} | ||
} | ||
return undefined; | ||
return destination; | ||
} | ||
function evalStatement() { | ||
return eval(arguments[0]); | ||
} | ||
module.exports = iassign; |
{ | ||
"name": "immutable-assign", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/iassign.js", |
@@ -1,2 +0,2 @@ | ||
# ImmutableAssign | ||
# immutable-assign (iassign.js) | ||
@@ -27,5 +27,6 @@ Lightweight immutable helper that supports TypeScript type checking, and allows you to continue working with POJO (Plain Old JavaScript Object). | ||
setProp: (prop: TProp) => TProp, // Function to set the property. | ||
ctx?: TContext): TObj; // (Optional) Context to be used in getProp(). | ||
context?: TContext, // (Optional) Context to be used in getProp(). | ||
option?: IIassignOption): TObj; // (Optional) Options | ||
// Global options | ||
// Options, can be applied globally or individually | ||
interface IIassignOption { | ||
@@ -188,10 +189,8 @@ freeze: boolean; // Deep freeze both input and output | ||
##Limitations and Constraints | ||
##Constraints | ||
* getProp() function must be pure, it cannot access anything other than the input parameters. I.e., it must not access "this" or "window" objects. In addition, it must not modify the input parameters. It should only return a property that needs to be updated. | ||
* getProp() must be pure function; I.e., it cannot access anything other than the input parameters. e.g., it must not access "this" or "window" objects. In addition, it must not modify the input parameters. It should only return a property that needs to be updated. | ||
* Current version does not support following characters in the property name: | ||
* [].\ | ||
* e.g., { "propery [].\\\\": {} } is invalid | ||
@@ -509,2 +509,46 @@ "use strict"; | ||
}); | ||
it("Access object property using string 4", function () { | ||
var propBName = "p\\r\"o.p t[] e.s't\'B"; | ||
var propCName = "h\\e'llo w\'or\"ld"; | ||
var propDName = 'h\\e"llo w\"or\'ld'; | ||
var propEName = 'p\\r\'o.p t[] e.s"t\"B'; | ||
var o1 = { a: (_a = {}, _a[propBName] = (_b = {}, _b[propCName] = (_c = {}, _c[propDName] = (_d = {}, _d[propEName] = [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], _d), _c), _b), _a) }; | ||
deepFreeze(o1); | ||
var o2 = iassign(o1, function (o) { return o | ||
. | ||
a | ||
[ | ||
"p\\r\"o.p t[] e.s't\'B" | ||
] | ||
[ | ||
"h\\e'llo w\'or\"ld" | ||
] | ||
[ | ||
'h\\e"llo w\"or\'ld' | ||
] | ||
[ | ||
'p\\r\'o.p t[] e.s"t\"B' | ||
] | ||
[ | ||
"1" | ||
] | ||
[ | ||
0 | ||
] | ||
. | ||
d | ||
; }, function (d) { return d + 1; }); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][propDName]).not.toBe(o1.a[propBName][propCName][propDName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName]).not.toBe(o1.a[propBName][propCName][propDName][propEName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0].d); | ||
var _a, _b, _c, _d; | ||
}); | ||
}); |
@@ -137,2 +137,58 @@ "use strict"; | ||
}); | ||
it("Access object property using string 2", function () { | ||
var propBName = "p\\r\"o.p t[] e.s\'t'B"; | ||
var propCName = "h\\e'llo w\'or\"ld"; | ||
var o1 = { a: (_a = {}, _a[propBName] = (_b = {}, _b[propCName] = [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], _b), _a) }; | ||
deepFreeze(o1); | ||
var o2 = iassign(o1, function (o) { return o.a["p\\r\"o.p t[] e.s\'t'B"]["h\\e'llo w\'or\"ld"]["1"][0].d; }, function (d) { return d + 1; }); | ||
expect(o2.a[propBName][propCName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][1]).not.toBe(o1.a[propBName][propCName][1]); | ||
expect(o2.a[propBName][propCName][1][0]).not.toBe(o1.a[propBName][propCName][1][0]); | ||
expect(o2.a[propBName][propCName][1][0].d).not.toBe(o1.a[propBName][propCName][1][0].d); | ||
var _a, _b; | ||
}); | ||
it("Access object property using string 3", function () { | ||
var propBName = "p\\ro.p t[] e.stB"; | ||
var propCName = "h\\e'llo w\'or\"ld"; | ||
var propDName = 'h\\e"llo w\"or\'ld'; | ||
var propEName = 'p\\ro.p t[] e.stB'; | ||
var o1 = { a: (_a = {}, _a[propBName] = (_b = {}, _b[propCName] = (_c = {}, _c[propDName] = (_d = {}, _d[propEName] = [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], _d), _c), _b), _a) }; | ||
deepFreeze(o1); | ||
var o2 = iassign(o1, function (o) { return o.a["p\\ro.p t[] e.stB"]["h\\e'llo w\'or\"ld"]['h\\e"llo w\"or\'ld']['p\\ro.p t[] e.stB']["1"][0].d; }, function (d) { return d + 1; }); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][propDName]).not.toBe(o1.a[propBName][propCName][propDName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName]).not.toBe(o1.a[propBName][propCName][propDName][propEName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0].d); | ||
var _a, _b, _c, _d; | ||
}); | ||
it("Access object property using string 4", function () { | ||
var propBName = "p\\r\"o.p t[] e.s't\'B"; | ||
var propCName = "h\\e'llo w\'or\"ld"; | ||
var propDName = 'h\\e"llo w\"or\'ld'; | ||
var propEName = 'p\\r\'o.p t[] e.s"t\"B'; | ||
var o1 = { a: (_a = {}, _a[propBName] = (_b = {}, _b[propCName] = (_c = {}, _c[propDName] = (_d = {}, _d[propEName] = [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], _d), _c), _b), _a) }; | ||
deepFreeze(o1); | ||
var o2 = iassign(o1, function (o) { return o.a["p\\r\"o.p t[] e.s't\'B"]["h\\e'llo w\'or\"ld"]['h\\e"llo w\"or\'ld']['p\\r\'o.p t[] e.s"t\"B']["1"][0].d; }, function (d) { return d + 1; }); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][propDName]).not.toBe(o1.a[propBName][propCName][propDName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName]).not.toBe(o1.a[propBName][propCName][propDName][propEName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0].d); | ||
var _a, _b, _c, _d; | ||
}); | ||
it("Access array using context parameter", function () { | ||
@@ -139,0 +195,0 @@ var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; |
@@ -180,2 +180,67 @@ | ||
it("Access object property using string 2", function () { | ||
let propBName = "p\\r\"o.p t[] e.s\'t'B"; | ||
let propCName = "h\\e'llo w\'or\"ld"; | ||
var o1 = { a: { [propBName]: { [propCName]: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; | ||
deepFreeze(o1); | ||
let o2 = iassign(o1, (o) => o.a["p\\r\"o.p t[] e.s\'t'B"]["h\\e'llo w\'or\"ld"]["1"][0].d, (d) => { return d + 1; }); | ||
expect(o2.a[propBName][propCName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][1]).not.toBe(o1.a[propBName][propCName][1]); | ||
expect(o2.a[propBName][propCName][1][0]).not.toBe(o1.a[propBName][propCName][1][0]); | ||
expect(o2.a[propBName][propCName][1][0].d).not.toBe(o1.a[propBName][propCName][1][0].d); | ||
}); | ||
it("Access object property using string 3", function () { | ||
let propBName = "p\\ro.p t[] e.stB"; | ||
let propCName = "h\\e'llo w\'or\"ld"; | ||
let propDName = 'h\\e"llo w\"or\'ld'; | ||
let propEName = 'p\\ro.p t[] e.stB'; | ||
var o1 = { a: { [propBName]: { [propCName]: { [propDName]: { [propEName]: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } } } }; | ||
deepFreeze(o1); | ||
let o2 = iassign(o1, (o) => o.a["p\\ro.p t[] e.stB"]["h\\e'llo w\'or\"ld"]['h\\e"llo w\"or\'ld']['p\\ro.p t[] e.stB']["1"][0].d, (d) => { return d + 1; }); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][propDName]).not.toBe(o1.a[propBName][propCName][propDName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName]).not.toBe(o1.a[propBName][propCName][propDName][propEName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0].d); | ||
}); | ||
it("Access object property using string 4", function () { | ||
let propBName = "p\\r\"o.p t[] e.s't\'B"; | ||
let propCName = "h\\e'llo w\'or\"ld"; | ||
let propDName = 'h\\e"llo w\"or\'ld'; | ||
let propEName = 'p\\r\'o.p t[] e.s"t\"B'; | ||
var o1 = { a: { [propBName]: { [propCName]: { [propDName]: { [propEName]: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } } } }; | ||
deepFreeze(o1); | ||
let o2 = iassign(o1, (o) => o.a["p\\r\"o.p t[] e.s't\'B"]["h\\e'llo w\'or\"ld"]['h\\e"llo w\"or\'ld']['p\\r\'o.p t[] e.s"t\"B']["1"][0].d, (d) => { return d + 1; }); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).toBe(22); | ||
expect(o2).not.toBe(o1); | ||
expect(o2.a).not.toBe(o1.a); | ||
expect(o2.a[propBName]).not.toBe(o1.a[propBName]); | ||
expect(o2.a[propBName][propCName]).not.toBe(o1.a[propBName][propCName]); | ||
expect(o2.a[propBName][propCName][propDName]).not.toBe(o1.a[propBName][propCName][propDName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName]).not.toBe(o1.a[propBName][propCName][propDName][propEName]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0]).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0]); | ||
expect(o2.a[propBName][propCName][propDName][propEName][1][0].d).not.toBe(o1.a[propBName][propCName][propDName][propEName][1][0].d); | ||
}); | ||
it("Access array using context parameter", function () { | ||
@@ -182,0 +247,0 @@ var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } }; |
@@ -5,5 +5,5 @@ | ||
interface IIassignOption { | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
freeze: boolean; // Deep freeze both input and output | ||
freezeInput: boolean; // Deep freeze input | ||
freezeOutput: boolean; // Deep freeze output | ||
} | ||
@@ -16,3 +16,4 @@ | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
context?: TContext, | ||
option?: IIassignOption): TObj; | ||
} | ||
@@ -19,0 +20,0 @@ } |
@@ -14,4 +14,11 @@ "use strict"; | ||
setProp, // Function to set property. | ||
context) { | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
context, // (Optional) Context to be used in getProp(). | ||
option) { | ||
if (option) { | ||
option = extend({}, iassign, option); | ||
} | ||
else { | ||
option = iassign; | ||
} | ||
if (deepFreeze && (option.freeze || option.freezeInput)) { | ||
deepFreeze(obj); | ||
@@ -21,4 +28,4 @@ } | ||
var value = getProp(obj, context); | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp); | ||
var accessorText = getAccessorText(getPropFuncInfo.bodyText); | ||
var getPropFuncInfo = parseGetPropFuncInfo(getProp, option); | ||
var accessorText = getPropFuncInfo.accessorText; | ||
var propIndex = 0; | ||
@@ -79,4 +86,10 @@ var propValue = undefined; | ||
if (propNameSource == ePropNameSource.inBracket && isNaN(propName)) { | ||
var propNameInQuote = extractTextInQuote(propName); | ||
if (propNameInQuote == undefined) { | ||
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"; | ||
@@ -92,5 +105,2 @@ if (getPropFuncInfo.objParameterName) { | ||
} | ||
else { | ||
propName = propNameInQuote; | ||
} | ||
} | ||
@@ -107,3 +117,3 @@ propValue = propValue[propName]; | ||
} | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
if (deepFreeze && (option.freeze || option.freezeOutput)) { | ||
deepFreeze(obj); | ||
@@ -121,3 +131,3 @@ } | ||
})(ePropNameSource || (ePropNameSource = {})); | ||
function parseGetPropFuncInfo(func) { | ||
function parseGetPropFuncInfo(func, option) { | ||
var funcText = func.toString(); | ||
@@ -139,11 +149,15 @@ var matches = /\(([^\)]*)\)/.exec(funcText); | ||
} | ||
var bodyText = funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")); | ||
var accessorTextInfo = getAccessorTextInfo(bodyText, option); | ||
return { | ||
objParameterName: objParameterName, | ||
cxtParameterName: cxtParameterName, | ||
bodyText: funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")) | ||
bodyText: bodyText, | ||
accessorText: accessorTextInfo.accessorText, | ||
quotedTextInfos: accessorTextInfo.quotedTextInfos, | ||
}; | ||
} | ||
function getAccessorText(bodyText) { | ||
function getAccessorTextInfo(bodyText, option) { | ||
var returnIndex = bodyText.indexOf("return "); | ||
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) { | ||
if (!option.disableAllCheck && !option.disableHasReturnCheck) { | ||
if (returnIndex <= -1) { | ||
@@ -153,3 +167,3 @@ throw new Error("getProp() function has no 'return' keyword."); | ||
} | ||
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) { | ||
if (!option.disableAllCheck && !option.disableExtraStatementCheck) { | ||
var otherBodyText = bodyText.substr(0, returnIndex).trim(); | ||
@@ -165,4 +179,39 @@ if (otherBodyText != "") { | ||
accessorText = accessorText.trim(); | ||
return accessorText; | ||
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, | ||
}; | ||
} | ||
function quickCopy(value) { | ||
@@ -174,7 +223,3 @@ if (value != undefined && !(value instanceof Date)) { | ||
else if (typeof (value) === "object") { | ||
var copyValue = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
return extend({}, value); | ||
} | ||
@@ -184,28 +229,21 @@ } | ||
} | ||
function evalStatement() { | ||
return eval(arguments[0]); | ||
} | ||
// function isTextInQuote(text: string): boolean { | ||
// let quoteMarks = ["'", '"']; | ||
// for (let mark of quoteMarks) { | ||
// if (text[0] == mark && text[text.length-1] == mark) { | ||
// return true; | ||
// } | ||
// } | ||
// return false; | ||
// } | ||
function extractTextInQuote(text) { | ||
var quoteMarks = ["'", '"']; | ||
for (var _i = 0, quoteMarks_1 = quoteMarks; _i < quoteMarks_1.length; _i++) { | ||
var mark = quoteMarks_1[_i]; | ||
if (text[0] == mark) { | ||
var regex = new RegExp("^[" + mark + "]([^" + mark + "]*)[" + mark + "]$"); | ||
var match = regex.exec(text); | ||
if (match) { | ||
return match[1]; | ||
function extend(destination) { | ||
var sources = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sources[_i - 1] = arguments[_i]; | ||
} | ||
for (var _a = 0, sources_1 = sources; _a < sources_1.length; _a++) { | ||
var source = sources_1[_a]; | ||
for (var key in source) { | ||
var value = source[key]; | ||
if (value !== undefined) { | ||
destination[key] = value; | ||
} | ||
} | ||
} | ||
return undefined; | ||
return destination; | ||
} | ||
function evalStatement() { | ||
return eval(arguments[0]); | ||
} | ||
module.exports = iassign; |
@@ -25,3 +25,4 @@ "use strict"; | ||
setProp: (prop: TProp) => TProp, | ||
context?: TContext): TObj; | ||
context?: TContext, | ||
option?: IIassignOption): TObj; | ||
} | ||
@@ -36,5 +37,13 @@ | ||
setProp: (prop: TProp) => TProp, // Function to set property. | ||
context?: TContext): TObj { // (Optional) Context to be used in getProp() . | ||
context?: TContext, // (Optional) Context to be used in getProp(). | ||
option?: IIassignOption): TObj { | ||
if (deepFreeze && (iassign.freeze || iassign.freezeInput)) { | ||
if (option) { | ||
option = extend({}, iassign, option); | ||
} | ||
else { | ||
option = iassign; | ||
} | ||
if (deepFreeze && (option.freeze || option.freezeInput)) { | ||
deepFreeze(obj); | ||
@@ -46,4 +55,4 @@ } | ||
let getPropFuncInfo = parseGetPropFuncInfo(getProp); | ||
let accessorText = getAccessorText(getPropFuncInfo.bodyText); | ||
let getPropFuncInfo = parseGetPropFuncInfo(getProp, option); | ||
let accessorText = getPropFuncInfo.accessorText; | ||
@@ -101,2 +110,3 @@ let propIndex = 0; | ||
} | ||
//console.log(propName); | ||
@@ -117,4 +127,11 @@ | ||
if (propNameSource == ePropNameSource.inBracket && isNaN(<any>propName)) { | ||
let propNameInQuote = extractTextInQuote(propName); | ||
if (propNameInQuote == undefined) { | ||
if (propName[0] == "#") { | ||
let quotedPropName = getPropFuncInfo.quotedTextInfos[propName]; | ||
if (!quotedPropName) { | ||
throw new Error("Cannot find quoted text for " + quotedPropName); | ||
} | ||
propName = eval(quotedPropName); | ||
} | ||
else { | ||
let statement = `'use strict';\n`; | ||
@@ -130,5 +147,2 @@ if (getPropFuncInfo.objParameterName) { | ||
} | ||
else { | ||
propName = propNameInQuote; | ||
} | ||
} | ||
@@ -151,3 +165,3 @@ | ||
if (deepFreeze && (iassign.freeze || iassign.freezeOutput)) { | ||
if (deepFreeze && (option.freeze || option.freezeOutput)) { | ||
deepFreeze(obj); | ||
@@ -167,3 +181,3 @@ } | ||
function parseGetPropFuncInfo(func: Function) { | ||
function parseGetPropFuncInfo(func: Function, option: IIassignOption) { | ||
let funcText = func.toString(); | ||
@@ -189,14 +203,19 @@ | ||
let bodyText = funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")); | ||
let accessorTextInfo = getAccessorTextInfo(bodyText, option); | ||
return { | ||
objParameterName: objParameterName, | ||
cxtParameterName: cxtParameterName, | ||
bodyText: funcText.substring(funcText.indexOf("{") + 1, funcText.lastIndexOf("}")) | ||
bodyText: bodyText, | ||
accessorText: accessorTextInfo.accessorText, | ||
quotedTextInfos: accessorTextInfo.quotedTextInfos, | ||
} | ||
} | ||
function getAccessorText(bodyText: string) { | ||
function getAccessorTextInfo(bodyText: string, option: IIassignOption) { | ||
let returnIndex = bodyText.indexOf("return "); | ||
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) { | ||
if (!option.disableAllCheck && !option.disableHasReturnCheck) { | ||
if (returnIndex <= -1) { | ||
@@ -207,3 +226,3 @@ throw new Error("getProp() function has no 'return' keyword."); | ||
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) { | ||
if (!option.disableAllCheck && !option.disableExtraStatementCheck) { | ||
let otherBodyText = bodyText.substr(0, returnIndex).trim(); | ||
@@ -220,5 +239,48 @@ if (otherBodyText != "") { | ||
accessorText = accessorText.trim(); | ||
return accessorText; | ||
return parseTextInQuotes(accessorText, option); | ||
} | ||
function parseTextInQuotes(accessorText, option: IIassignOption) { | ||
let quotedTextInfos: { [key: string]: string } = {} | ||
let index = 0; | ||
while (true) { | ||
let singleQuoteIndex = accessorText.indexOf("'"); | ||
let doubleQuoteIndex = accessorText.indexOf('"'); | ||
let varName = "#" + index++; | ||
if (singleQuoteIndex <= -1 && doubleQuoteIndex <= -1) | ||
break; | ||
let matches: RegExpExecArray = undefined; | ||
let quoteIndex: number; | ||
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, | ||
quotedTextInfos, | ||
}; | ||
} | ||
function quickCopy<T>(value: T): T { | ||
@@ -231,7 +293,3 @@ | ||
else if (typeof (value) === "object") { | ||
let copyValue: any = {}; | ||
for (var key in value) { | ||
copyValue[key] = value[key]; | ||
} | ||
return copyValue; | ||
return extend({}, value); | ||
} | ||
@@ -243,2 +301,14 @@ } | ||
function extend(destination: any, ...sources) { | ||
for (var source of sources) { | ||
for (var key in source) { | ||
let value = source[key]; | ||
if (value !== undefined) { | ||
destination[key] = value; | ||
} | ||
} | ||
} | ||
return destination; | ||
} | ||
function evalStatement() { | ||
@@ -260,17 +330,17 @@ return eval(arguments[0]); | ||
function extractTextInQuote(text: string): string { | ||
let quoteMarks = ["'", '"']; | ||
// function extractTextInQuote(text: string): string { | ||
// let quoteMarks = ["'", '"']; | ||
for (let mark of quoteMarks) { | ||
if (text[0] == mark) { | ||
let regex = new RegExp(`^[${mark}]([^${mark}]*)[${mark}]$`); | ||
let match = regex.exec(text); | ||
if (match) { | ||
return match[1]; | ||
} | ||
} | ||
} | ||
// for (let mark of quoteMarks) { | ||
// if (text[0] == mark) { | ||
// let regex = new RegExp(`^[${mark}]([^${mark}]*)[${mark}]$`); | ||
// let match = regex.exec(text); | ||
// if (match) { | ||
// return match[1]; | ||
// } | ||
// } | ||
// } | ||
return undefined; | ||
} | ||
// return undefined; | ||
// } | ||
@@ -277,0 +347,0 @@ export = iassign; |
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
794617
23312
195
4