Skip to content

Commit 5df25a6

Browse files
authored
fix: improve boolean parse behaviour (#2029)
1 parent 4b656eb commit 5df25a6

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/common/utils.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ const isValidHexColor = (hexColor) => {
7777
/**
7878
* Returns boolean if value is either "true" or "false" else the value as it is.
7979
*
80-
* @param {string} value The value to parse.
81-
* @returns {boolean | string} The parsed value.
80+
* @param {string | boolean} value The value to parse.
81+
* @returns {boolean | undefined } The parsed value.
8282
*/
8383
const parseBoolean = (value) => {
84-
if (value === "true") {
85-
return true;
86-
} else if (value === "false") {
87-
return false;
88-
} else {
89-
return value;
84+
if (typeof value === "boolean") return value;
85+
86+
if (typeof value === "string") {
87+
if (value.toLowerCase() === "true") {
88+
return true;
89+
} else if (value.toLowerCase() === "false") {
90+
return false;
91+
}
9092
}
93+
return undefined;
9194
};
9295

9396
/**

tests/utils.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
encodeHTML,
55
getCardColors,
66
kFormatter,
7+
parseBoolean,
78
renderError,
89
wrapTextMultiline,
910
} from "../src/common/utils.js";
@@ -19,6 +20,23 @@ describe("Test utils.js", () => {
1920
expect(kFormatter(9900000)).toBe("9900k");
2021
});
2122

23+
it("should test parseBoolean", () => {
24+
expect(parseBoolean(true)).toBe(true);
25+
expect(parseBoolean(false)).toBe(false);
26+
27+
expect(parseBoolean("true")).toBe(true);
28+
expect(parseBoolean("false")).toBe(false);
29+
expect(parseBoolean("True")).toBe(true);
30+
expect(parseBoolean("False")).toBe(false);
31+
expect(parseBoolean("TRUE")).toBe(true);
32+
expect(parseBoolean("FALSE")).toBe(false);
33+
34+
expect(parseBoolean("1")).toBe(undefined);
35+
expect(parseBoolean("0")).toBe(undefined);
36+
expect(parseBoolean("")).toBe(undefined);
37+
expect(parseBoolean(undefined)).toBe(undefined);
38+
});
39+
2240
it("should test encodeHTML", () => {
2341
expect(encodeHTML(`<html>hello world<,.#4^&^@%!))`)).toBe(
2442
"&#60;html&#62;hello world&#60;,.#4^&#38;^@%!))",

0 commit comments

Comments
 (0)