refactor: minor refactors

This commit is contained in:
Anurag 2021-05-07 00:49:43 +05:30
parent f8b0c8767e
commit fef8bc3a4a
11 changed files with 76 additions and 55 deletions

View File

@ -3,7 +3,7 @@ const {
kFormatter, kFormatter,
encodeHTML, encodeHTML,
getCardColors, getCardColors,
FlexLayout, flexLayout,
wrapTextMultiline, wrapTextMultiline,
} = require("../common/utils"); } = require("../common/utils");
const I18n = require("../common/I18n"); const I18n = require("../common/I18n");
@ -61,7 +61,13 @@ const renderRepoCard = (repo, options = {}) => {
}); });
// returns theme based colors with proper overrides and defaults // returns theme based colors with proper overrides and defaults
const { titleColor, textColor, iconColor, bgColor, borderColor } = getCardColors({ const {
titleColor,
textColor,
iconColor,
bgColor,
borderColor,
} = getCardColors({
title_color, title_color,
icon_color, icon_color,
text_color, text_color,
@ -111,7 +117,7 @@ const renderRepoCard = (repo, options = {}) => {
const svgForks = const svgForks =
forkCount > 0 && iconWithLabel(icons.fork, totalForks, "forkcount"); forkCount > 0 && iconWithLabel(icons.fork, totalForks, "forkcount");
const starAndForkCount = FlexLayout({ const starAndForkCount = flexLayout({
items: [svgStars, svgForks], items: [svgStars, svgForks],
gap: 65, gap: 65,
}).join(""); }).join("");

View File

@ -5,7 +5,7 @@ const { getStyles } = require("../getStyles");
const { statCardLocales } = require("../translations"); const { statCardLocales } = require("../translations");
const { const {
kFormatter, kFormatter,
FlexLayout, flexLayout,
clampValue, clampValue,
measureText, measureText,
getCardColors, getCardColors,
@ -78,7 +78,13 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
const lheight = parseInt(line_height, 10); const lheight = parseInt(line_height, 10);
// returns theme based colors with proper overrides and defaults // returns theme based colors with proper overrides and defaults
const { titleColor, textColor, iconColor, bgColor, borderColor } = getCardColors({ const {
titleColor,
textColor,
iconColor,
bgColor,
borderColor,
} = getCardColors({
title_color, title_color,
icon_color, icon_color,
text_color, text_color,
@ -131,7 +137,17 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
}, },
}; };
const longLocales = ["cn", "es", "fr", "pt-br", "ru", "uk-ua", "id", "my", "pl"]; const longLocales = [
"cn",
"es",
"fr",
"pt-br",
"ru",
"uk-ua",
"id",
"my",
"pl",
];
const isLongLocale = longLocales.includes(locale) === true; const isLongLocale = longLocales.includes(locale) === true;
// filter out hidden stats defined by user & create the text nodes // filter out hidden stats defined by user & create the text nodes
@ -223,7 +239,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
${rankCircle} ${rankCircle}
<svg x="0" y="0"> <svg x="0" y="0">
${FlexLayout({ ${flexLayout({
items: statItems, items: statItems,
gap: lheight, gap: lheight,
direction: "column", direction: "column",

View File

@ -1,8 +1,8 @@
const Card = require("../common/Card"); const Card = require("../common/Card");
const { clampValue, getCardColors, FlexLayout } = require("../common/utils");
const { createProgressNode } = require("../common/createProgressNode");
const { langCardLocales } = require("../translations");
const I18n = require("../common/I18n"); const I18n = require("../common/I18n");
const { langCardLocales } = require("../translations");
const { createProgressNode } = require("../common/createProgressNode");
const { clampValue, getCardColors, flexLayout } = require("../common/utils");
const createProgressTextNode = ({ width, color, name, progress }) => { const createProgressTextNode = ({ width, color, name, progress }) => {
const paddingRight = 95; const paddingRight = 95;
@ -75,7 +75,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
locale, locale,
langs_count = 5, langs_count = 5,
border_radius, border_radius,
border_color border_color,
} = options; } = options;
const i18n = new I18n({ const i18n = new I18n({
@ -171,7 +171,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
}).join("")} }).join("")}
`; `;
} else { } else {
finalLayout = FlexLayout({ finalLayout = flexLayout({
items: langs.map((lang) => { items: langs.map((lang) => {
return createProgressTextNode({ return createProgressTextNode({
width: width, width: width,

View File

@ -2,9 +2,9 @@ const Card = require("../common/Card");
const I18n = require("../common/I18n"); const I18n = require("../common/I18n");
const { getStyles } = require("../getStyles"); const { getStyles } = require("../getStyles");
const { wakatimeCardLocales } = require("../translations"); const { wakatimeCardLocales } = require("../translations");
const { clampValue, getCardColors, FlexLayout } = require("../common/utils");
const { createProgressNode } = require("../common/createProgressNode");
const languageColors = require("../common/languageColors.json"); const languageColors = require("../common/languageColors.json");
const { createProgressNode } = require("../common/createProgressNode");
const { clampValue, getCardColors, flexLayout } = require("../common/utils");
const noCodingActivityNode = ({ color, text }) => { const noCodingActivityNode = ({ color, text }) => {
return ` return `
@ -204,7 +204,7 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
}).join("")} }).join("")}
`; `;
} else { } else {
finalLayout = FlexLayout({ finalLayout = flexLayout({
items: statItems.length items: statItems.length
? statItems ? statItems
: [ : [

View File

@ -1,5 +1,5 @@
const { FlexLayout, encodeHTML } = require("../common/utils");
const { getAnimations } = require("../getStyles"); const { getAnimations } = require("../getStyles");
const { flexLayout, encodeHTML } = require("../common/utils");
class Card { class Card {
constructor({ constructor({
@ -85,7 +85,7 @@ class Card {
data-testid="card-title" data-testid="card-title"
transform="translate(${this.paddingX}, ${this.paddingY})" transform="translate(${this.paddingX}, ${this.paddingY})"
> >
${FlexLayout({ ${flexLayout({
items: [this.titlePrefixIcon && prefixIcon, titleText], items: [this.titlePrefixIcon && prefixIcon, titleText],
gap: 25, gap: 25,
}).join("")} }).join("")}

View File

@ -97,7 +97,7 @@ function request(data, headers) {
* Auto layout utility, allows us to layout things * Auto layout utility, allows us to layout things
* vertically or horizontally with proper gaping * vertically or horizontally with proper gaping
*/ */
function FlexLayout({ items, gap, direction }) { function flexLayout({ items, gap, direction }) {
// filter() for filtering out empty strings // filter() for filtering out empty strings
return items.filter(Boolean).map((item, i) => { return items.filter(Boolean).map((item, i) => {
let transform = `translate(${gap * i}, 0)`; let transform = `translate(${gap * i}, 0)`;
@ -120,7 +120,8 @@ function getCardColors({
}) { }) {
const defaultTheme = themes[fallbackTheme]; const defaultTheme = themes[fallbackTheme];
const selectedTheme = themes[theme] || defaultTheme; const selectedTheme = themes[theme] || defaultTheme;
const defaultBorderColor = selectedTheme.border_color || defaultTheme.border_color; const defaultBorderColor =
selectedTheme.border_color || defaultTheme.border_color;
// get the color provided by the user else the theme color // get the color provided by the user else the theme color
// finally if both colors are invalid fallback to default theme // finally if both colors are invalid fallback to default theme
@ -239,7 +240,7 @@ module.exports = {
parseArray, parseArray,
parseBoolean, parseBoolean,
fallbackColor, fallbackColor,
FlexLayout, flexLayout,
getCardColors, getCardColors,
clampValue, clampValue,
wrapTextMultiline, wrapTextMultiline,

View File

@ -1,5 +1,5 @@
const { request } = require("../common/utils");
const retryer = require("../common/retryer"); const retryer = require("../common/retryer");
const { request } = require("../common/utils");
const fetcher = (variables, token) => { const fetcher = (variables, token) => {
return request( return request(

View File

@ -1,8 +1,9 @@
const { request, logger, CustomError } = require("../common/utils");
const axios = require("axios"); const axios = require("axios");
const githubUsernameRegex = require("github-username-regex");
const retryer = require("../common/retryer"); const retryer = require("../common/retryer");
const calculateRank = require("../calculateRank"); const calculateRank = require("../calculateRank");
const githubUsernameRegex = require("github-username-regex"); const { request, logger, CustomError } = require("../common/utils");
require("dotenv").config(); require("dotenv").config();

View File

@ -1,4 +1,4 @@
const { request, logger, clampValue } = require("../common/utils"); const { request, logger } = require("../common/utils");
const retryer = require("../common/retryer"); const retryer = require("../common/retryer");
require("dotenv").config(); require("dotenv").config();
@ -34,11 +34,7 @@ const fetcher = (variables, token) => {
); );
}; };
async function fetchTopLanguages( async function fetchTopLanguages(username, exclude_repo = []) {
username,
exclude_repo = [],
hide = [],
) {
if (!username) throw Error("Invalid username"); if (!username) throw Error("Invalid username");
const res = await retryer(fetcher, { login: username }); const res = await retryer(fetcher, { login: username });

View File

@ -1,30 +1,31 @@
const { encodeHTML } = require("./common/utils"); const { encodeHTML } = require("./common/utils");
const statCardLocales = ({ name, apostrophe }) => { const statCardLocales = ({ name, apostrophe }) => {
const encodedName = encodeHTML(name);
return { return {
"statcard.title": { "statcard.title": {
cn: `${encodeHTML(name)} 的 GitHub 统计数据`, cn: `${encodedName} 的 GitHub 统计数据`,
cs: `GitHub statistiky uživatele ${encodeHTML(name)}`, cs: `GitHub statistiky uživatele ${encodedName}`,
de: `${encodeHTML(name) + apostrophe} GitHub-Statistiken`, de: `${encodedName + apostrophe} GitHub-Statistiken`,
en: `${encodeHTML(name)}'${apostrophe} GitHub Stats`, en: `${encodedName}'${apostrophe} GitHub Stats`,
es: `Estadísticas de GitHub de ${encodeHTML(name)}`, es: `Estadísticas de GitHub de ${encodedName}`,
fr: `Statistiques GitHub de ${encodeHTML(name)}`, fr: `Statistiques GitHub de ${encodedName}`,
hu: `${encodeHTML(name)} GitHub statisztika`, hu: `${encodedName} GitHub statisztika`,
it: `Statistiche GitHub di ${encodeHTML(name)}`, it: `Statistiche GitHub di ${encodedName}`,
ja: `${encodeHTML(name)}の GitHub 統計`, ja: `${encodedName}の GitHub 統計`,
kr: `${encodeHTML(name)}의 GitHub 통계`, kr: `${encodedName}의 GitHub 통계`,
nl: `${encodeHTML(name)}'${apostrophe} GitHub Statistieken`, nl: `${encodedName}'${apostrophe} GitHub Statistieken`,
"pt-pt": `Estatísticas do GitHub de ${encodeHTML(name)}`, "pt-pt": `Estatísticas do GitHub de ${encodedName}`,
"pt-br": `Estatísticas do GitHub de ${encodeHTML(name)}`, "pt-br": `Estatísticas do GitHub de ${encodedName}`,
np: `${encodeHTML(name)}'${apostrophe} गिटहब तथ्याङ्क`, np: `${encodedName}'${apostrophe} गिटहब तथ्याङ्क`,
el: `Στατιστικά GitHub του ${encodeHTML(name)}`, el: `Στατιστικά GitHub του ${encodedName}`,
ru: `Статистика GitHub пользователя ${encodeHTML(name)}`, ru: `Статистика GitHub пользователя ${encodedName}`,
"uk-ua": `Статистика GitHub користувача ${encodeHTML(name)}`, "uk-ua": `Статистика GitHub користувача ${encodedName}`,
id: `Statistik GitHub ${encodeHTML(name)}`, id: `Statistik GitHub ${encodedName}`,
my: `Statistik GitHub ${encodeHTML(name)}`, my: `Statistik GitHub ${encodedName}`,
sk: `GitHub štatistiky používateľa ${encodeHTML(name)}`, sk: `GitHub štatistiky používateľa ${encodedName}`,
tr: `${encodeHTML(name)} Hesabının GitHub Yıldızları`, tr: `${encodedName} Hesabının GitHub Yıldızları`,
pl: `Statystyki GitHub użytkownika ${encodeHTML(name)}`, pl: `Statystyki GitHub użytkownika ${encodedName}`,
}, },
"statcard.totalstars": { "statcard.totalstars": {
cn: "获标星数star", cn: "获标星数star",

View File

@ -3,7 +3,7 @@ const {
kFormatter, kFormatter,
encodeHTML, encodeHTML,
renderError, renderError,
FlexLayout, flexLayout,
getCardColors, getCardColors,
wrapTextMultiline, wrapTextMultiline,
} = require("../src/common/utils"); } = require("../src/common/utils");
@ -44,8 +44,8 @@ describe("Test utils.js", () => {
).toHaveTextContent(/Secondary Message/gim); ).toHaveTextContent(/Secondary Message/gim);
}); });
it("should test FlexLayout", () => { it("should test flexLayout", () => {
const layout = FlexLayout({ const layout = flexLayout({
items: ["<text>1</text>", "<text>2</text>"], items: ["<text>1</text>", "<text>2</text>"],
gap: 60, gap: 60,
}).join(""); }).join("");
@ -54,7 +54,7 @@ describe("Test utils.js", () => {
`<g transform=\"translate(0, 0)\"><text>1</text></g><g transform=\"translate(60, 0)\"><text>2</text></g>`, `<g transform=\"translate(0, 0)\"><text>1</text></g><g transform=\"translate(60, 0)\"><text>2</text></g>`,
); );
const columns = FlexLayout({ const columns = flexLayout({
items: ["<text>1</text>", "<text>2</text>"], items: ["<text>1</text>", "<text>2</text>"],
gap: 60, gap: 60,
direction: "column", direction: "column",