Skip to content

Commit

Permalink
Merge pull request #128 from TasosY2K/master
Browse files Browse the repository at this point in the history
removed eval use from deep.store.js
  • Loading branch information
nbubna authored Dec 26, 2024
2 parents 0216588 + b5b7723 commit 0ef2405
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/store.deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* store.set('foo', { is: { not: { quite: false }}});
* console.log(store.get('foo.is.not.quite'));// logs false
*
* Status: ALPHA - currently only supports get, inefficient, uses eval
* Status: ALPHA - currently only supports get
*/
;(function(_) {
; (function (_) {

// save original core accessor
var _get = _.get;
// replace with enhanced version
_.get = function(area, key, kid) {
_.get = function (area, key, kid) {
var s = _get(area, key);
if (s == null) {
var parts = _.split(key);
Expand All @@ -26,22 +26,31 @@
return _.get(area, parts[0], kid);
}
} else if (kid) {
var val = _.parse(s);
/*jshint evil:true */
val = eval("val."+kid);
s = _.stringify(val);
try {
var val = _.parse(s);
val = _.resolvePath(val, kid);
s = _.stringify(val);
} catch (e) {
console.error("Error accessing nested property:", e);
return null;
}
}
return s;
};

// Helper function to resolve nested paths safely
_.resolvePath = function (obj, path) {
return path.split('.').reduce((acc, key) => acc && acc[key], obj);
};

// expose internals on the underscore to allow extensibility
_.split = function(key) {
_.split = function (key) {
var dot = key.lastIndexOf('.');
if (dot > 0) {
var kid = key.substring(dot+1, key.length);
var kid = key.substring(dot + 1, key.length);
key = key.substring(0, dot);
return [key, kid];
}
};

})(window.store._);
})(window.store._);

0 comments on commit 0ef2405

Please sign in to comment.