Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactored code in src/settings.js #16

Open
wants to merge 4 commits into
base: f24
Choose a base branch
from
Open
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
33 changes: 18 additions & 15 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,34 @@ Settings.prototype.persist = function (callback) {
@param def The default value, if not set global default value gets used.
@returns Object The setting to be used.
*/

// Used ChatGPT for refactoring
Settings.prototype.get = function (key, def) {
let obj = this.cfg._;
const parts = (key || '').split('.');
let part;
const obj = this._getValue(this.cfg._, parts);

if (obj === undefined) {
def = def !== undefined ? def : this._getValue(this.defCfg, parts);
return def;
}

return obj;
};

Settings.prototype._getValue = function (obj, parts) {
for (let i = 0; i < parts.length; i += 1) {
part = parts[i];
const part = parts[i];
if (part && obj != null) {
obj = obj[part];
} else {
return undefined;
}
}
if (obj === undefined) {
if (def === undefined) {
def = this.defCfg;
for (let j = 0; j < parts.length; j += 1) {
part = parts[j];
if (part && def != null) {
def = def[part];
}
}
}
return def;
}
return obj;
};



/**
Returns the settings-wrapper object.
@returns Object The settings-wrapper.
Expand Down