From 59367dc6b0b664ade40cbb96b16db3a3936a740a Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Tue, 26 Dec 2023 14:57:22 +0200 Subject: [PATCH] refactor(wakatime card): move duplicate value format logic into separate function (#3512) --- src/cards/wakatime-card.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/cards/wakatime-card.js b/src/cards/wakatime-card.js index fd55b365e9cf0..65e1d54d779ea 100644 --- a/src/cards/wakatime-card.js +++ b/src/cards/wakatime-card.js @@ -39,6 +39,20 @@ const noCodingActivityNode = ({ color, text }) => { * @typedef {import('../fetchers/types').WakaTimeLang} WakaTimeLang */ +/** + * Format language value. + * + * @param {Object} args The function arguments. + * @param {WakaTimeLang} args.lang The language object. + * @param {"time" | "percent"} args.display_format The display format of the language node. + * @returns {string} The formatted language value. + */ +const formatLanguageValue = ({ display_format, lang }) => { + return display_format === "percent" + ? `${lang.percent.toFixed(2).toString()} %` + : lang.text; +}; + /** * Create compact WakaTime layout. * @@ -51,10 +65,7 @@ const noCodingActivityNode = ({ color, text }) => { */ const createCompactLangNode = ({ lang, x, y, display_format }) => { const color = languageColors[lang.name] || "#858585"; - const value = - display_format === "percent" - ? `${lang.percent.toFixed(2).toString()} %` - : lang.text; + const value = formatLanguageValue({ display_format, lang }); return ` @@ -341,10 +352,7 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => { return createTextNode({ id: language.name, label: language.name, - value: - display_format === "percent" - ? `${language.percent.toFixed(2).toString()} %` - : language.text, + value: formatLanguageValue({ display_format, lang: language }), index, percent: language.percent, // @ts-ignore