Skip to content

Commit b5b7723

Browse files
committed
removed eval use from deep.store.js
1 parent 0216588 commit b5b7723

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/store.deep.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* store.set('foo', { is: { not: { quite: false }}});
1010
* console.log(store.get('foo.is.not.quite'));// logs false
1111
*
12-
* Status: ALPHA - currently only supports get, inefficient, uses eval
12+
* Status: ALPHA - currently only supports get
1313
*/
14-
;(function(_) {
14+
; (function (_) {
1515

1616
// save original core accessor
1717
var _get = _.get;
1818
// replace with enhanced version
19-
_.get = function(area, key, kid) {
19+
_.get = function (area, key, kid) {
2020
var s = _get(area, key);
2121
if (s == null) {
2222
var parts = _.split(key);
@@ -26,22 +26,31 @@
2626
return _.get(area, parts[0], kid);
2727
}
2828
} else if (kid) {
29-
var val = _.parse(s);
30-
/*jshint evil:true */
31-
val = eval("val."+kid);
32-
s = _.stringify(val);
29+
try {
30+
var val = _.parse(s);
31+
val = _.resolvePath(val, kid);
32+
s = _.stringify(val);
33+
} catch (e) {
34+
console.error("Error accessing nested property:", e);
35+
return null;
36+
}
3337
}
3438
return s;
3539
};
3640

41+
// Helper function to resolve nested paths safely
42+
_.resolvePath = function (obj, path) {
43+
return path.split('.').reduce((acc, key) => acc && acc[key], obj);
44+
};
45+
3746
// expose internals on the underscore to allow extensibility
38-
_.split = function(key) {
47+
_.split = function (key) {
3948
var dot = key.lastIndexOf('.');
4049
if (dot > 0) {
41-
var kid = key.substring(dot+1, key.length);
50+
var kid = key.substring(dot + 1, key.length);
4251
key = key.substring(0, dot);
4352
return [key, kid];
4453
}
4554
};
4655

47-
})(window.store._);
56+
})(window.store._);

0 commit comments

Comments
 (0)