fix: improve boolean parse behaviour (#2029)
This commit is contained in:
1 parent
4b656ebabb
commit
5df25a6472
2 files changed
+29
-8
No files matched your search
+11
-8
@@ -77,17 +77,20 @@ const isValidHexColor = (hexColor) => {
|
|||||||
/**
|
/**
|
||||||
* Returns boolean if value is either "true" or "false" else the value as it is.
|
* Returns boolean if value is either "true" or "false" else the value as it is.
|
||||||
*
|
*
|
||||||
* @param {string} value The value to parse.
|
* @param {string | boolean} value The value to parse.
|
||||||
* @returns {boolean | string} The parsed value.
|
* @returns {boolean | undefined } The parsed value.
|
||||||
*/
|
*/
|
||||||
const parseBoolean = (value) => {
|
const parseBoolean = (value) => {
|
||||||
if (value === "true") {
|
if (typeof value === "boolean") return value;
|
||||||
return true;
|
|
||||||
} else if (value === "false") {
|
if (typeof value === "string") {
|
||||||
return false;
|
if (value.toLowerCase() === "true") {
|
||||||
} else {
|
return true;
|
||||||
return value;
|
} else if (value.toLowerCase() === "false") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
encodeHTML,
|
encodeHTML,
|
||||||
getCardColors,
|
getCardColors,
|
||||||
kFormatter,
|
kFormatter,
|
||||||
|
parseBoolean,
|
||||||
renderError,
|
renderError,
|
||||||
wrapTextMultiline,
|
wrapTextMultiline,
|
||||||
} from "../src/common/utils.js";
|
} from "../src/common/utils.js";
|
||||||
@@ -19,6 +20,23 @@ describe("Test utils.js", () => {
|
|||||||
expect(kFormatter(9900000)).toBe("9900k");
|
expect(kFormatter(9900000)).toBe("9900k");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should test parseBoolean", () => {
|
||||||
|
expect(parseBoolean(true)).toBe(true);
|
||||||
|
expect(parseBoolean(false)).toBe(false);
|
||||||
|
|
||||||
|
expect(parseBoolean("true")).toBe(true);
|
||||||
|
expect(parseBoolean("false")).toBe(false);
|
||||||
|
expect(parseBoolean("True")).toBe(true);
|
||||||
|
expect(parseBoolean("False")).toBe(false);
|
||||||
|
expect(parseBoolean("TRUE")).toBe(true);
|
||||||
|
expect(parseBoolean("FALSE")).toBe(false);
|
||||||
|
|
||||||
|
expect(parseBoolean("1")).toBe(undefined);
|
||||||
|
expect(parseBoolean("0")).toBe(undefined);
|
||||||
|
expect(parseBoolean("")).toBe(undefined);
|
||||||
|
expect(parseBoolean(undefined)).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
it("should test encodeHTML", () => {
|
it("should test encodeHTML", () => {
|
||||||
expect(encodeHTML(`<html>hello world<,.#4^&^@%!))`)).toBe(
|
expect(encodeHTML(`<html>hello world<,.#4^&^@%!))`)).toBe(
|
||||||
"<html>hello world<,.#4^&^@%!))",
|
"<html>hello world<,.#4^&^@%!))",
|
||||||
|
|||||||
Reference in new issue
Block a user