diff --git a/src/cards/gist-card.js b/src/cards/gist-card.js index 37e11184e4ce3..9e889e74424cd 100644 --- a/src/cards/gist-card.js +++ b/src/cards/gist-card.js @@ -8,6 +8,8 @@ import { kFormatter, measureText, flexLayout, + iconWithLabel, + createLanguageNode, } from "../common/utils.js"; import Card from "../common/Card.js"; import { icons } from "../common/icons.js"; @@ -27,48 +29,6 @@ const ICON_SIZE = 16; const CARD_DEFAULT_WIDTH = 400; const HEADER_MAX_LENGTH = 35; -/** - * Creates a node to display the primary programming language of the gist. - * - * @param {string} langName Language name. - * @param {string} langColor Language color. - * @returns {string} Language display SVG object. - */ -const createLanguageNode = (langName, langColor) => { - return ` - - - ${langName} - - `; -}; - -/** - * Creates an icon with label to display gist stats like forks, stars, etc. - * - * @param {string} icon The icon to display. - * @param {number|string} label The label to display. - * @param {string} testid The testid to assign to the label. - * @returns {string} Icon with label SVG object. - */ -const iconWithLabel = (icon, label, testid) => { - if (typeof label === "number" && label <= 0) return ""; - const iconSvg = ` - - ${icon} - - `; - const text = `${label}`; - return flexLayout({ items: [iconSvg, text], gap: 20 }).join(""); -}; - /** * @typedef {import('./types').GistCardOptions} GistCardOptions Gist card options. * @typedef {import('../fetchers/types').GistData} GistData Gist data. @@ -122,8 +82,18 @@ const renderGistCard = (gistData, options = {}) => { const totalStars = kFormatter(starsCount); const totalForks = kFormatter(forksCount); - const svgStars = iconWithLabel(icons.star, totalStars, "starsCount"); - const svgForks = iconWithLabel(icons.fork, totalForks, "forksCount"); + const svgStars = iconWithLabel( + icons.star, + totalStars, + "starsCount", + ICON_SIZE, + ); + const svgForks = iconWithLabel( + icons.fork, + totalForks, + "forksCount", + ICON_SIZE, + ); const languageName = language || "Unspecified"; const languageColor = languageColors[languageName] || "#858585"; diff --git a/src/cards/repo-card.js b/src/cards/repo-card.js index 3706c96dbfb10..09b5841880a97 100644 --- a/src/cards/repo-card.js +++ b/src/cards/repo-card.js @@ -10,9 +10,13 @@ import { measureText, parseEmojis, wrapTextMultiline, + iconWithLabel, + createLanguageNode, } from "../common/utils.js"; import { repoCardLocales } from "../translations.js"; +const ICON_SIZE = 16; + /** * Retrieves the repository description and wraps it to fit the card width. * @@ -35,50 +39,6 @@ const getBadgeSVG = (label, textColor) => ` `; -/** - * Creates a node to display the primary programming language of the repository. - * - * @param {string} langName Language name. - * @param {string} langColor Language color. - * @returns {string} Language display SVG object. - */ -const createLanguageNode = (langName, langColor) => { - return ` - - - ${langName} - - `; -}; - -const ICON_SIZE = 16; - -/** - * Creates an icon with label to display repository stats like forks, stars, etc. - * - * @param {string} icon The icon to display. - * @param {number|string} label The label to display. - * @param {string} testid The testid to assign to the label. - * @returns {string} Icon with label SVG object. - */ -const iconWithLabel = (icon, label, testid) => { - if (typeof label === "number" && label <= 0) return ""; - const iconSvg = ` - - ${icon} - - `; - const text = `${label}`; - return flexLayout({ items: [iconSvg, text], gap: 20 }).join(""); -}; - /** * @typedef {import("../fetchers/types").RepositoryData} RepositoryData Repository data. * @typedef {import("./types").RepoCardOptions} RepoCardOptions Repo card options. @@ -151,8 +111,18 @@ const renderRepoCard = (repo, options = {}) => { const totalStars = kFormatter(starCount); const totalForks = kFormatter(forkCount); - const svgStars = iconWithLabel(icons.star, totalStars, "stargazers"); - const svgForks = iconWithLabel(icons.fork, totalForks, "forkcount"); + const svgStars = iconWithLabel( + icons.star, + totalStars, + "stargazers", + ICON_SIZE, + ); + const svgForks = iconWithLabel( + icons.fork, + totalForks, + "forkcount", + ICON_SIZE, + ); const starAndForkCount = flexLayout({ items: [svgLanguage, svgStars, svgForks], diff --git a/src/common/utils.js b/src/common/utils.js index 4b5dced487453..ccccbe74da1d3 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -34,6 +34,49 @@ const renderError = (message, secondaryMessage = "") => { `; }; +/** + * Creates a node to display the primary programming language of the repository/gist. + * + * @param {string} langName Language name. + * @param {string} langColor Language color. + * @returns {string} Language display SVG object. + */ +const createLanguageNode = (langName, langColor) => { + return ` + + + ${langName} + + `; +}; + +/** + * Creates an icon with label to display repository/gist stats like forks, stars, etc. + * + * @param {string} icon The icon to display. + * @param {number|string} label The label to display. + * @param {string} testid The testid to assign to the label. + * @param {number} iconSize The size of the icon. + * @returns {string} Icon with label SVG object. + */ +const iconWithLabel = (icon, label, testid, iconSize) => { + if (typeof label === "number" && label <= 0) return ""; + const iconSvg = ` + + ${icon} + + `; + const text = `${label}`; + return flexLayout({ items: [iconSvg, text], gap: 20 }).join(""); +}; + /** * Encode string as HTML. * @@ -469,6 +512,8 @@ const dateDiff = (d1, d2) => { export { ERROR_CARD_LENGTH, renderError, + createLanguageNode, + iconWithLabel, encodeHTML, kFormatter, isValidHexColor,