Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5df25a6

Browse files
authored
fix: improve boolean parse behaviour (anuraghazra#2029)
1 parent4b656eb commit5df25a6

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
constparseBoolean=(value)=>{
84-
if(value==="true"){
85-
returntrue;
86-
}elseif(value==="false"){
87-
returnfalse;
88-
}else{
89-
returnvalue;
84+
if(typeofvalue==="boolean")returnvalue;
85+
86+
if(typeofvalue==="string"){
87+
if(value.toLowerCase()==="true"){
88+
returntrue;
89+
}elseif(value.toLowerCase()==="false"){
90+
returnfalse;
91+
}
9092
}
93+
returnundefined;
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)

[8]ページ先頭

©2009-2025 Movatter.jp