Skip to content

Commit e387df2

Browse files
committed
parseString: fix to address nullish check
1 parent 87d9c0d commit e387df2

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

client/utils/parseStringToType.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ describe('parseNumber', () => {
1818
it('returns 0 if input is undefined and nullishNumber is true', () => {
1919
const warnSpy = jest.spyOn(console, 'warn');
2020
expect(parseNumber(undefined, true)).toBe(0);
21-
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got undefined input');
21+
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got nullish input');
22+
});
23+
24+
it('returns 0 if input is an empty string and nullishNumber is true', () => {
25+
const warnSpy = jest.spyOn(console, 'warn');
26+
expect(parseNumber('', true)).toBe(0);
27+
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got nullish input');
2228
});
2329

2430
it('returns undefined and warns if input is undefined and nullishNumber is false', () => {
2531
const warnSpy = jest.spyOn(console, 'warn');
2632
expect(parseNumber(undefined, false)).toBeUndefined();
27-
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got undefined input');
33+
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got nullish input');
34+
});
35+
36+
it('returns undefined and warns if input is an empty string and nullishNumber is false', () => {
37+
const warnSpy = jest.spyOn(console, 'warn');
38+
expect(parseNumber('', false)).toBeUndefined();
39+
expect(warnSpy).toHaveBeenCalledWith('parseNumber: got nullish input');
2840
});
2941

3042
it('returns undefined and warns if parsing fails', () => {
@@ -56,13 +68,25 @@ describe('parseBoolean', () => {
5668
it('returns false if input is undefined and nullishBool is true', () => {
5769
const warnSpy = jest.spyOn(console, 'warn');
5870
expect(parseBoolean(undefined, true)).toBe(false);
59-
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got undefined input');
71+
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got nullish input');
72+
});
73+
74+
it('returns false if input is empty string and nullishBool is true', () => {
75+
const warnSpy = jest.spyOn(console, 'warn');
76+
expect(parseBoolean('', true)).toBe(false);
77+
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got nullish input');
6078
});
6179

6280
it('returns undefined and warns if input is undefined and nullishBool is false', () => {
6381
const warnSpy = jest.spyOn(console, 'warn');
6482
expect(parseBoolean(undefined, false)).toBeUndefined();
65-
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got undefined input');
83+
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got nullish input');
84+
});
85+
86+
it('returns undefined and warns if input is empty string and nullishBool is false', () => {
87+
const warnSpy = jest.spyOn(console, 'warn');
88+
expect(parseBoolean('', false)).toBeUndefined();
89+
expect(warnSpy).toHaveBeenCalledWith('parseBoolean: got nullish input');
6690
});
6791

6892
it('returns undefined and warns if parsing fails', () => {

client/utils/parseStringToType.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export function parseNumber(
88
str?: string,
99
nullishNumber = false
1010
): number | undefined {
11-
if (str == null) {
12-
console.warn(`parseNumber: got undefined input`);
11+
if (!str) {
12+
console.warn(`parseNumber: got nullish input`);
1313
return nullishNumber ? 0 : undefined;
1414
}
1515

@@ -31,8 +31,8 @@ export function parseBoolean(
3131
str?: string,
3232
nullishBool = false
3333
): boolean | undefined {
34-
if (str == null) {
35-
console.warn('parseBoolean: got undefined input');
34+
if (!str) {
35+
console.warn('parseBoolean: got nullish input');
3636
return nullishBool ? false : undefined;
3737
}
3838

0 commit comments

Comments
 (0)