Skip to content

Commit 6a6e9fa

Browse files
committed
feat(core utils): Add is_option_truthy to check Pattern options for a truthy value.
A values "undefined", "null", "false", "none" or "" are considered falsy and can be used to disable some functionality. Other values including "0" are considered to be true.
1 parent 15090e3 commit 6a6e9fa

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/core/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,36 @@ const threshold_list = (num_steps = 0) => {
756756
return thresholds.sort();
757757
};
758758

759+
/**
760+
* is_option_truthy - Check if an Pattern option is set.
761+
*
762+
* An option is set if it is not one of:
763+
* - undefined
764+
* - null
765+
* - "none"
766+
* - ""
767+
*
768+
* @param {String} option - The option to check.
769+
*
770+
* @returns {Boolean} - Returns true if the option is set, false otherwise.
771+
*
772+
* @example
773+
*
774+
* is_option_truthy() // false
775+
* is_option_truthy(undefined) // false
776+
* is_option_truthy(null) // false
777+
* is_option_truthy("") // false
778+
* is_option_truthy("none") // false
779+
* is_option_truthy("false") // false
780+
* is_option_truthy("foo") // true
781+
* is_option_truthy(true) // true
782+
* is_option_truthy(0) // true
783+
*
784+
*/
785+
const is_option_truthy = (option) => {
786+
return ![undefined, null, "none", false, "false", ""].includes(option);
787+
};
788+
759789
var utils = {
760790
jqueryPlugin: jqueryPlugin,
761791
escapeRegExp: escapeRegExp,
@@ -789,6 +819,7 @@ var utils = {
789819
is_iso_date: is_iso_date,
790820
date_diff: date_diff,
791821
threshold_list: threshold_list,
822+
is_option_truthy: is_option_truthy,
792823
getCSSValue: dom.get_css_value, // BBB: moved to dom. TODO: Remove in upcoming version.
793824
};
794825

src/core/utils.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,24 @@ describe("threshold_list ...", function () {
899899
]);
900900
});
901901
});
902+
903+
describe("is_option_truthy ...", function () {
904+
it("checks if an option is set", () => {
905+
// These options are considered truthy
906+
expect(utils.is_option_truthy("a")).toBe(true);
907+
expect(utils.is_option_truthy(true)).toBe(true);
908+
expect(utils.is_option_truthy("true")).toBe(true);
909+
expect(utils.is_option_truthy(1)).toBe(true);
910+
// Also "0" is considered truthy because it can be a valid option for
911+
// length, time and so forth.
912+
expect(utils.is_option_truthy(0)).toBe(true);
913+
914+
// These options are considered falsy
915+
expect(utils.is_option_truthy(undefined)).toBe(false);
916+
expect(utils.is_option_truthy(null)).toBe(false);
917+
expect(utils.is_option_truthy(false)).toBe(false);
918+
expect(utils.is_option_truthy("false")).toBe(false);
919+
expect(utils.is_option_truthy("none")).toBe(false);
920+
expect(utils.is_option_truthy("")).toBe(false);
921+
});
922+
});

0 commit comments

Comments
 (0)