New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.3 to 1.0.4

spec/ImmutableAssign2Spec.js

45

deploy/iassign.js
"use strict";
// Immutable Assign
function iassign(obj, getProp, setProp, ctx) {
// Quick check if getProp() is valid.
function iassign(obj, // Object to set property, it will not be modified
getProp, // Function to get property to be updated.
setProp, // Function to set property
ctx) {
// Check if getProp() is valid
var value = getProp(obj, ctx);
var getPropBodyText = getFuncBodyText(getProp);
var accessorText = getPropBodyText.substr(getPropBodyText.indexOf("return ") + 7).trim();
if (accessorText[accessorText.length - 1] == ";") {
accessorText = accessorText.substring(0, accessorText.length - 1);
}
accessorText = accessorText.trim();
var accessorText = getAccessorText(getPropBodyText);
var propIndex = 0;

@@ -20,6 +19,6 @@ var propValue = undefined;

var propNameSource = ePropNameSource.none;
if (dotIndex == 0) {
accessorText = accessorText.substr(dotIndex + 1);
continue;
}
// if (dotIndex == 0) {
// accessorText = accessorText.substr(dotIndex + 1);
// continue;
// }
if (openBracketIndex > -1 && closeBracketIndex <= -1) {

@@ -53,2 +52,6 @@ throw new Error("Found open bracket but not close bracket.");

}
propName = propName.trim();
if (propName == "") {
continue;
}
//console.log(propName);

@@ -101,2 +104,22 @@ if (propIndex <= 0) {

}
function getAccessorText(bodyText) {
var returnIndex = bodyText.indexOf("return ");
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) {
if (returnIndex <= -1) {
throw new Error("getProp() function has no 'return' keyword.");
}
}
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) {
var otherBodyText = bodyText.substr(0, returnIndex).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 accessorText;
}
function quickCopy(value) {

@@ -103,0 +126,0 @@ var copyValue = {};

{
"name": "immutable-assign",
"version": "1.0.3",
"version": "1.0.4",
"description": "",
"main": "src/iassign.js",
"scripts": {
"test": "node_modules\\.bin\\istanbul cover node_modules\\jasmine\\bin\\jasmine.js"
"test": "node_modules\\.bin\\jasmine",
"cover": "node_modules\\.bin\\istanbul cover node_modules\\jasmine\\bin\\jasmine.js",
"build": "gulp"
},

@@ -9,0 +11,0 @@ "author": {

# ImmutableAssign
Lightweight immutable helper that supports TypeScript type checking.
Lightweight immutable helper that supports TypeScript type checking, and allows you to continue working with POJO (Plain Old JavaScript Object).
This library is trying to solve following problems:
* Most immutable JavaScript libraries try to encapsulate the data, and provide proprietary APIs to work with the data. They are more verbose than normal JavaScript syntax. E.g., map1.get('b') vs map1.b, nested2.getIn(['a', 'b', 'd']) vs nested2.a.b.d, etc.
* Encapsulated data is no more POJO, therefore cannot be easily used with other libraries, e.g., lodash, underscore, etc.
* [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) address some of above issues when reading the properties, but still use verbose APIs to write properties.
* [Immutability Helpers](https://facebook.github.io/react/docs/update.html) allows us work with POJO, but it has still introduced some magic keywords, such as $set, $push, etc.
* Most importantly (in my opinion), we lost TypeScript type checking. E.g., when calling nested2.getIn(['a', 'b', 'd']), TypeScript won't be able to warn me if I changed property 'd' to 'e'.
This library has only one method **iassign()**, which accept a POJO object and return you a new POJO object with specific property updated.
##Install with npm

@@ -8,11 +19,43 @@

#### Function Signature
```javascript
// Return a new POJO object with property updated.
function iassign<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.
ctx?: TContext): TObj; // (Optional) Context to be used in getProp().
```
####Example 1: Update array
```javascript
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
var iassign = require("iassign");
var deepFreeze = require("deep-freeze");
// Calling iassign()
var o2 = iassign(o1, function (o) { return o.a.b.c[1]; }, function (c) { c.push(101); return c; });
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
// Verify original object has not been changed.
//
// Calling iassign() to increment o1.a.b.c[0][0].d
//
var o2 = iassign(
o1,
function (o) { return o.a.b.c[0][0]; },
function (ci) { ci.d++; return ci; }
);
```
```javascript
//
// 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.a.b.c[0][0].d).toBe(12);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -22,6 +65,12 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c).not.toBe(o1.a.b.c);
expect(o2.a.b.c[0]).toBe(o1.a.b.c[0]);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
expect(o2.a.b.c[0]).not.toBe(o1.a.b.c[0]);
expect(o2.a.b.c[0][0]).not.toBe(o1.a.b.c[0][0]);
expect(o2.a.b.c[0][0].d).not.toBe(o1.a.b.c[0][0].d);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0][0].e).toBe(o1.a.b.c[0][0].e);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
expect(o2.a.b.c[1][1]).toBe(101);
```

@@ -32,8 +81,26 @@

```javascript
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
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
// Calling iassign()
var o2 = iassign(o1, function (o) { return o.a.b.c[0][0].d; }, function (d) { return d + 1; });
//
// Calling iassign() to push new item to o1.a.b.c[1]
//
var o2 = iassign(
o1,
function (o) { return o.a.b.c[1]; },
function (c) { c.push(101); return c; }
);
```
```javascript
//
// Jasmine Tests
//
// Verify original object has not been changed.
// 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.a.b.c[1][1]).toBe(101);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -43,6 +110,11 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c).not.toBe(o1.a.b.c);
expect(o2.a.b.c[0]).not.toBe(o1.a.b.c[0]);
expect(o2.a.b.c[0][0]).not.toBe(o1.a.b.c[0][0]);
expect(o2.a.b.c[0][0].d).not.toBe(o1.a.b.c[0][0].d);
expect(o2.a.b.c[0][0].d).toBe(12);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0]).toBe(o1.a.b.c[0]);
expect(o2.a.b.c[0][0]).toBe(o1.a.b.c[0][0]);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
```

@@ -8,6 +8,13 @@ "use strict";

it("Access array item", function () {
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} };
deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c[0][0]; }, function (ci) { ci.d++; return ci; });
//
// Jasmine Tests
//
// expect o1 has not been changed
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} });
// expect o2 inner property has been updated.
expect(o2.a.b.c[0][0].d).toBe(12);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -20,9 +27,22 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c[0][0].d).not.toBe(o1.a.b.c[0][0].d);
expect(o2.a.b.c[0][0].d).toBe(12);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0][0].e).toBe(o1.a.b.c[0][0].e);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
expect(o2.a.b.c[2][0]).toBe(o1.a.b.c[2][0]);
});
it("Access array 1", function () {
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} };
deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c[1]; }, function (c) { c.push(101); return c; });
//
// Jasmine Tests
//
// expect o1 has not been changed
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} });
// expect o2 inner property has been updated.
expect(o2.a.b.c[1][1]).toBe(101);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -32,6 +52,12 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c).not.toBe(o1.a.b.c);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0]).toBe(o1.a.b.c[0]);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
expect(o2.a.b.c[0][0]).toBe(o1.a.b.c[0][0]);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
expect(o2.a.b.c[1][1]).toBe(101);
expect(o2.a.b.c[2]).toBe(o1.a.b.c[2]);
expect(o2.a.b.c[2][0]).toBe(o1.a.b.c[2][0]);
});

@@ -41,3 +67,2 @@ it("Access array 2", function () {

deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c; }, function (c) { c.pop(); return c; });

@@ -55,3 +80,2 @@ expect(o2).not.toBe(o1);

deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c; }, function (c) { c.d++; return c; });

@@ -69,3 +93,2 @@ expect(o2).not.toBe(o1);

deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c[0][0].d; }, function (d) { return d + 1; });

@@ -84,3 +107,2 @@ expect(o2).not.toBe(o1);

deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c.f; }, function (f) { return new Date(2016, 1, 1); });

@@ -138,16 +160,15 @@ expect(o2).not.toBe(o1);

deepFreeze(o1);
var p1 = { a: 0 };
expect(function () {
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(3); return ci; }, { p1: p1 });
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(3); return ci; });
}).toThrowError(TypeError, /Can't add property/);
expect(function () {
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; }, { p1: p1 });
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; });
}).toThrowError(TypeError, /Cannot assign to read only property/);
expect(function () {
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].g = 1; return ci; }, { p1: p1 });
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].g = 1; return ci; });
}).toThrowError(TypeError, /Can't add property/);
expect(function () {
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; }, { p1: p1 });
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; });
}).toThrowError(TypeError, /object is not extensible/);
});
});

@@ -11,8 +11,22 @@

it("Access array item", function () {
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} };
deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c[0][0]; }, function (ci) { ci.d++; return ci; });
var o2 = iassign(
o1,
(o) => o.a.b.c[0][0],
(ci) => { ci.d++; return ci; }
);
//
// Jasmine Tests
//
// expect o1 has not been changed
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} })
// expect o2 inner property has been updated.
expect(o2.a.b.c[0][0].d).toBe(12);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -25,12 +39,33 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c[0][0].d).not.toBe(o1.a.b.c[0][0].d);
expect(o2.a.b.c[0][0].d).toBe(12);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0][0].e).toBe(o1.a.b.c[0][0].e);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
expect(o2.a.b.c[2][0]).toBe(o1.a.b.c[2][0]);
});
it("Access array 1", function () {
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]] } } };
var o1 = { a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} };
deepFreeze(o1);
var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c[1]; }, function (c) { c.push(<any>101); return c; });
var o2 = iassign(
o1,
(o) => o.a.b.c[1],
(c) => { c.push(<any>101); return c; }
);
//
// Jasmine Tests
//
// expect o1 has not been changed
expect(o1).toEqual({ a: { b: { c: [[{ d: 11, e: 12 }], [{ d: 21, e: 22 }], [{ d: 31, e: 32 }]], c2: {} }, b2: {} }, a2: {} })
// expect o2 inner property has been updated.
expect(o2.a.b.c[1][1]).toBe(101);
// expect object graph for changed property in o2 is now different from (!==) o1.
expect(o2).not.toBe(o1);

@@ -40,6 +75,13 @@ expect(o2.a).not.toBe(o1.a);

expect(o2.a.b.c).not.toBe(o1.a.b.c);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
// expect object graph for unchanged property in o2 is still equal to (===) o1.
expect(o2.a2).toBe(o1.a2);
expect(o2.a.b2).toBe(o1.a.b2);
expect(o2.a.b.c2).toBe(o1.a.b.c2);
expect(o2.a.b.c[0]).toBe(o1.a.b.c[0]);
expect(o2.a.b.c[1]).not.toBe(o1.a.b.c[1]);
expect(o2.a.b.c[0][0]).toBe(o1.a.b.c[0][0]);
expect(o2.a.b.c[1][0]).toBe(o1.a.b.c[1][0]);
expect(o2.a.b.c[1][1]).toBe(101);
expect(o2.a.b.c[2]).toBe(o1.a.b.c[2]);
expect(o2.a.b.c[2][0]).toBe(o1.a.b.c[2][0]);
});

@@ -51,3 +93,2 @@

var p1 = { a: 0 };
var o2 = iassign(o1, function (o) { return o.a.b.c; }, function (c) { c.pop(); return c; });

@@ -68,3 +109,2 @@

var p1 = { a: 0 };
let o2 = iassign(o1, (o) => o.a.b.c, (c) => { c.d++; return c });

@@ -85,3 +125,2 @@

var p1 = { a: 0 };
let o2 = iassign(o1, (o) => o.a.b.c[0][0].d, (d) => { return d + 1; });

@@ -103,3 +142,2 @@

var p1 = { a: 0 };
let o2 = iassign(o1, (o) => o.a.b.c.f, (f) => { return new Date(2016, 1, 1) });

@@ -169,18 +207,16 @@

var p1 = { a: 0 };
expect(() => {
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(<any>3); return ci; }, { p1 });
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].push(<any>3); return ci; });
}).toThrowError(TypeError, /Can't add property/);
expect(() => {
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; }, { p1 });
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { ci[0].d++; return ci; });
}).toThrowError(TypeError, /Cannot assign to read only property/);
expect(() => {
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { (<any>ci[0]).g = 1; return ci; }, { p1 });
iassign(o1, function (o) { return o.a.b.c[0]; }, function (ci) { (<any>ci[0]).g = 1; return ci; });
}).toThrowError(TypeError, /Can't add property/);
expect(() => {
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; }, { p1 });
iassign(o1, function (o) { return o.a.b.c; }, function (ci) { ci[0].pop(); return ci; });
}).toThrowError(TypeError, /object is not extensible/);

@@ -187,0 +223,0 @@ });

"use strict";
// Immutable Assign
function iassign(obj, getProp, setProp, ctx) {
// Quick check if getProp() is valid.
function iassign(obj, // Object to set property, it will not be modified
getProp, // Function to get property to be updated.
setProp, // Function to set property
ctx) {
// Check if getProp() is valid
var value = getProp(obj, ctx);
var getPropBodyText = getFuncBodyText(getProp);
var accessorText = getPropBodyText.substr(getPropBodyText.indexOf("return ") + 7).trim();
if (accessorText[accessorText.length - 1] == ";") {
accessorText = accessorText.substring(0, accessorText.length - 1);
}
accessorText = accessorText.trim();
var accessorText = getAccessorText(getPropBodyText);
var propIndex = 0;

@@ -20,6 +19,6 @@ var propValue = undefined;

var propNameSource = ePropNameSource.none;
if (dotIndex == 0) {
accessorText = accessorText.substr(dotIndex + 1);
continue;
}
// if (dotIndex == 0) {
// accessorText = accessorText.substr(dotIndex + 1);
// continue;
// }
if (openBracketIndex > -1 && closeBracketIndex <= -1) {

@@ -53,2 +52,6 @@ throw new Error("Found open bracket but not close bracket.");

}
propName = propName.trim();
if (propName == "") {
continue;
}
//console.log(propName);

@@ -101,2 +104,22 @@ if (propIndex <= 0) {

}
function getAccessorText(bodyText) {
var returnIndex = bodyText.indexOf("return ");
if (!iassign.disableAllCheck && !iassign.disableHasReturnCheck) {
if (returnIndex <= -1) {
throw new Error("getProp() function has no 'return' keyword.");
}
}
if (!iassign.disableAllCheck && !iassign.disableExtraStatementCheck) {
var otherBodyText = bodyText.substr(0, returnIndex).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 accessorText;
}
function quickCopy(value) {

@@ -103,0 +126,0 @@ var copyValue = {};

// Immutable Assign
function iassign<TObj, TProp, TContext>(
obj: TObj,
getProp: (obj: TObj, context: TContext) => TProp,
setProp: (prop: TProp) => TProp,
ctx?: TContext): TObj {
obj: TObj, // Object to set property, it will not be modified
getProp: (obj: TObj, context: TContext) => TProp, // Function to get property to be updated.
setProp: (prop: TProp) => TProp, // Function to set property
ctx?: TContext): TObj { // Context to be used in getProp()
// Quick check if getProp() is valid.
// Check if getProp() is valid
let value = getProp(obj, ctx);
var getPropBodyText = getFuncBodyText(getProp);
let getPropBodyText = getFuncBodyText(getProp);
let accessorText = getAccessorText(getPropBodyText);
let accessorText = getPropBodyText.substr(getPropBodyText.indexOf("return ") + 7).trim();
if (accessorText[accessorText.length - 1] == ";") {
accessorText = accessorText.substring(0, accessorText.length - 1);
}
accessorText = accessorText.trim();
let propIndex = 0;

@@ -29,7 +24,8 @@ let propValue = undefined;

let propNameSource = ePropNameSource.none;
if (dotIndex == 0) {
accessorText = accessorText.substr(dotIndex + 1);
continue;
}
// if (dotIndex == 0) {
// accessorText = accessorText.substr(dotIndex + 1);
// continue;
// }
if (openBracketIndex > -1 && closeBracketIndex <= -1) {

@@ -67,2 +63,6 @@ throw new Error("Found open bracket but not close bracket.");

propName = propName.trim();
if (propName == "") {
continue;
}
//console.log(propName);

@@ -125,2 +125,27 @@

function getAccessorText(bodyText: string) {
let returnIndex = bodyText.indexOf("return ");
if (!(<any>iassign).disableAllCheck && !(<any>iassign).disableHasReturnCheck) {
if (returnIndex <= -1) {
throw new Error("getProp() function has no 'return' keyword.");
}
}
if (!(<any>iassign).disableAllCheck && !(<any>iassign).disableExtraStatementCheck) {
let otherBodyText = bodyText.substr(0, returnIndex).trim();
if (otherBodyText != "") {
throw new Error("getProp() function has statements other than 'return': " + otherBodyText);
}
}
let accessorText = bodyText.substr(returnIndex + 7).trim();
if (accessorText[accessorText.length - 1] == ";") {
accessorText = accessorText.substring(0, accessorText.length - 1);
}
accessorText = accessorText.trim();
return accessorText;
}
function quickCopy<T>(value: T): T {

@@ -127,0 +152,0 @@ let copyValue: any = {};

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