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

luci-base: add uci.get_bool to allow cleanup of app code #7612

Open
wants to merge 1 commit into
base: master
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
34 changes: 34 additions & 0 deletions modules/luci-base/htdocs/luci-static/resources/uci.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,40 @@ return baseclass.extend(/** @lends LuCI.uci.prototype */ {
return this.get(conf, sid, opt);
},

/**
* A special case of `get` that always returns either `true` or
* `false`.
*
* Many configuration files contain boolean settings, such as
* `enabled` or `advanced_mode`, where there is no consistent
* definition for the values. This function allows users to
* enter any of the values `"yes"`, `"on"`, `"true"` or `1` in
* their config files and we return the expected boolean result.
*
* Character case is not significant, so for example, any of
* "YES", "Yes" or "yes" will be interpreted as a `true` value.
*
* @param {string} conf
* The name of the configuration to read.
*
* @param {string} sid
* The name or ID of the section to read.
*
* @param {string} [opt]
* The option name from which to read the value. If the option
* name is omitted or `null`, the value `false` is returned.
*
* @returns {boolean}
* - Returns boolean `true` if the configuration value is defined
* and looks like a true value, otherwise returns `false`.
*/
get_bool(conf, type, opt) {
let value = this.get(conf, type, opt);
if (typeof(value) == 'string')
return ['1', 'on', 'true', 'yes'].includes(value.toLowerCase());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing 'enabled'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enabled is a parameter name. I've not seen any instances that use = "enabled", and I don't think its use should be encouraged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be some feature = 'enabled'. I agree that this should be avoided, just want to have it consistent with UCI. Still not a problem, we may merge the PR.

return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a Number 1 will be converted to false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But get guarantees null, string or stuff we don't care about, so should never see an int.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

},

/**
* Sets the value of the given option within the first found section
* of the given configuration matching the specified type or within
Expand Down