Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

removed eval use from deep.store.js #128

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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._);