From 91ebfb1c7dc2ef63be02f614585fdf9fd9ef6773 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Tue, 31 Aug 2021 16:29:31 +0530 Subject: refactor: expose safeParseInt so that all recipes can use the same for consistent error handling --- CHANGELOG.md | 8 ++++++++ recipes | 2 +- src/webview/badge.ts | 13 +++++++++---- src/webview/lib/RecipeWebview.js | 9 +++++++++ src/webview/recipe.js | 2 ++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8be8b52..af75cb27b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# [v5.6.1-nightly.45](https://github.com/getferdi/ferdi/compare/v5.6.1-nightly.43...v5.6.1-nightly.45) (2021-09-01) + +- Services: Add `odoo` and `wakatime` 💖 @oda-alexandre + +### Internal + +- Use `Ferdi.safeParseInt` to ensure that parsing for unread count is done consistently in all recipes 💖 @vraravam + # [v5.6.1-nightly.43](https://github.com/getferdi/ferdi/compare/v5.6.1-nightly.42...v5.6.1-nightly.43) (2021-08-30) - Services: Remove duplicated recipes for `Outlook Web App` and `Enterprise OWA` (getferdi/recipes#523 getferdi/recipes#1808) 💖 @vraravam diff --git a/recipes b/recipes index b63978d00..2486fbbd7 160000 --- a/recipes +++ b/recipes @@ -1 +1 @@ -Subproject commit b63978d0038c4774f820843cb4b45052918f943e +Subproject commit 2486fbbd72f08b759a2781913f388e91149e79ea 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 { }; } - _normalizeNumber(count: string | number) { + // TODO: Need to extract this into a utility class and reuse outside of the recipes + safeParseInt(text: string | number | undefined | null) { + if (text === undefined || text === null) { + return 0; + } + // Parse number to integer // This will correct errors that recipes may introduce, e.g. // by sending a String instead of an integer - const parsedNumber = parseInt(count.toString(), 10); + const parsedNumber = parseInt(text.toString(), 10); const adjustedNumber = Number.isNaN(parsedNumber) ? 0 : parsedNumber; return Math.max(adjustedNumber, 0); } @@ -28,8 +33,8 @@ export class BadgeHandler { } const count = { - direct: this._normalizeNumber(direct), - indirect: this._normalizeNumber(indirect), + direct: this.safeParseInt(direct), + indirect: this.safeParseInt(indirect), }; debug('Sending badge count to host', count); diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js index 96caa125e..2bd6bad8d 100644 --- a/src/webview/lib/RecipeWebview.js +++ b/src/webview/lib/RecipeWebview.js @@ -46,6 +46,15 @@ class RecipeWebview { this.badgeHandler.setBadge(direct, indirect); } + /** + * Safely parse the given text into an integer + * + * @param {string | number | undefined | null} text to be parsed + */ + safeParseInt(text) { + return this.badgeHandler.safeParseInt(text); + } + /** * Injects the contents of a CSS file into the current webview * diff --git a/src/webview/recipe.js b/src/webview/recipe.js index f88b94cfe..1981c8731 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -102,6 +102,8 @@ contextBridge.exposeInMainWorld('ferdi', { open: window.open, setBadge: (direct, indirect) => badgeHandler.setBadge(direct, indirect), + safeParseInt: (text) => + badgeHandler.safeParseInt(text), displayNotification: (title, options) => notificationsHandler.displayNotification(title, options), getDisplayMediaSelector, -- cgit v1.2.3-54-g00ecf