diff options
author | 2021-08-31 16:29:31 +0530 | |
---|---|---|
committer | 2021-08-31 16:49:36 +0530 | |
commit | 91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773 (patch) | |
tree | 510505dbf4f5997f46dc6e0d9cf6ddd61553b548 /src/webview/badge.ts | |
parent | 5.6.1-nightly.44 [skip ci] (diff) | |
download | ferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.tar.gz ferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.tar.zst ferdium-app-91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773.zip |
refactor: expose safeParseInt so that all recipes can use the same for consistent error handling
Diffstat (limited to 'src/webview/badge.ts')
-rw-r--r-- | src/webview/badge.ts | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/webview/badge.ts b/src/webview/badge.ts index b1da36271..753e90fef 100644 --- a/src/webview/badge.ts +++ b/src/webview/badge.ts | |||
@@ -12,11 +12,16 @@ export class BadgeHandler { | |||
12 | }; | 12 | }; |
13 | } | 13 | } |
14 | 14 | ||
15 | _normalizeNumber(count: string | number) { | 15 | // TODO: Need to extract this into a utility class and reuse outside of the recipes |
16 | safeParseInt(text: string | number | undefined | null) { | ||
17 | if (text === undefined || text === null) { | ||
18 | return 0; | ||
19 | } | ||
20 | |||
16 | // Parse number to integer | 21 | // Parse number to integer |
17 | // This will correct errors that recipes may introduce, e.g. | 22 | // This will correct errors that recipes may introduce, e.g. |
18 | // by sending a String instead of an integer | 23 | // by sending a String instead of an integer |
19 | const parsedNumber = parseInt(count.toString(), 10); | 24 | const parsedNumber = parseInt(text.toString(), 10); |
20 | const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; | 25 | const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; |
21 | return Math.max(adjustedNumber, 0); | 26 | return Math.max(adjustedNumber, 0); |
22 | } | 27 | } |
@@ -28,8 +33,8 @@ export class BadgeHandler { | |||
28 | } | 33 | } |
29 | 34 | ||
30 | const count = { | 35 | const count = { |
31 | direct: this._normalizeNumber(direct), | 36 | direct: this.safeParseInt(direct), |
32 | indirect: this._normalizeNumber(indirect), | 37 | indirect: this.safeParseInt(indirect), |
33 | }; | 38 | }; |
34 | 39 | ||
35 | debug('Sending badge count to host', count); | 40 | debug('Sending badge count to host', count); |