-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a Number 1 will be converted to false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing 'enabled'
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.