Skip to content

Commit c98ad97

Browse files
committed
parseStringToType: handle when original input is already a bool
1 parent ed2114c commit c98ad97

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

client/utils/parseStringToType.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,32 @@ export function parseNumber(
2626
}
2727

2828
/**
29-
* Parses a case-insensitive string into a boolean.
29+
* Parses a case-insensitive string into a boolean, or returns the original boolean.
3030
* - Returns `false` for nullish input if `nullishBool` is true.
3131
* - Returns `undefined` otherwise for nullish or unrecognized input.
3232
*/
3333
export function parseBoolean(
34-
str?: string,
34+
input?: string | boolean,
3535
nullishBool = false
3636
): boolean | undefined {
37-
if (!str) {
37+
// Handle nullish
38+
if (input == null || input === '') {
3839
if (!isTestEnvironment) {
3940
console.warn('parseBoolean: got nullish input');
4041
}
4142
return nullishBool ? false : undefined;
4243
}
4344

44-
const lower = str.toLowerCase();
45+
// Handle actual boolean
46+
if (typeof input === 'boolean') {
47+
return input;
48+
}
49+
50+
// Handle string case
51+
const lower = input.toLowerCase();
4552
if (lower === 'true') return true;
4653
if (lower === 'false') return false;
4754

48-
console.warn(`parseBoolean: expected 'true' or 'false', got "${str}"`);
55+
console.warn(`parseBoolean: expected 'true' or 'false', got "${input}"`);
4956
return undefined;
5057
}

0 commit comments

Comments
 (0)