Skip to content

Commit

Permalink
refactor: migrate to using arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rickstaa committed Sep 17, 2022
1 parent eacc3cd commit 5866cd0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 61 deletions.
28 changes: 14 additions & 14 deletions scripts/preview-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const OWNER = "anuraghazra";
const REPO = "github-readme-stats";
const COMMENT_TITLE = "Automated Theme Preview";

function getPrNumber() {
const getPrNumber = () => {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}

return pullRequest.number;
}
};

function findCommentPredicate(inputs, comment) {
const findCommentPredicate = (inputs, comment) => {
return (
(inputs.commentAuthor && comment.user
? comment.user.login === inputs.commentAuthor
Expand All @@ -29,9 +29,9 @@ function findCommentPredicate(inputs, comment) {
? comment.body.includes(inputs.bodyIncludes)
: true)
);
}
};

async function findComment(octokit, issueNumber) {
const findComment = async (octokit, issueNumber) => {
const parameters = {
owner: OWNER,
repo: REPO,
Expand All @@ -52,28 +52,28 @@ async function findComment(octokit, issueNumber) {
);
if (comment) return comment;
}
}
};

async function upsertComment(octokit, props) {
const upsertComment = async (octokit, props) => {
if (props.comment_id !== undefined) {
await octokit.issues.updateComment(props);
} else {
await octokit.issues.createComment(props);
}
}
};

function getWebAimLink(color1, color2) {
const getWebAimLink = (color1, color2) => {
return `https://webaim.org/resources/contrastchecker/?fcolor=${color1}&bcolor=${color2}`;
}
};

function getGrsLink(colors) {
const getGrsLink = (colors) => {
const url = `https://github-readme-stats.vercel.app/api?username=anuraghazra`;
const colorString = Object.keys(colors)
.map((colorKey) => `${colorKey}=${colors[colorKey]}`)
.join("&");

return `${url}&${colorString}&show_icons=true`;
}
};

const themeContribGuidelines = `
\rHi, thanks for the theme contribution, please read our theme [contribution guidelines](https://github.com/anuraghazra/github-readme-stats/blob/master/CONTRIBUTING.md#themes-contribution).
Expand All @@ -82,7 +82,7 @@ const themeContribGuidelines = `
\r> Also note that if this theme is exclusively for your personal use, then instead of adding it to our theme collection you can use card [customization options](https://github.com/anuraghazra/github-readme-stats#customization)
`;

async function run() {
const run = async () => {
try {
const ccc = new ColorContrastChecker();
const warnings = [];
Expand Down Expand Up @@ -183,6 +183,6 @@ async function run() {
} catch (error) {
console.log(error);
}
}
};

run();
10 changes: 5 additions & 5 deletions src/calculateRank.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://stackoverflow.com/a/5263759/10629172
function normalcdf(mean, sigma, to) {
const normalcdf = (mean, sigma, to) => {
var z = (to - mean) / Math.sqrt(2 * sigma * sigma);
var t = 1 / (1 + 0.3275911 * Math.abs(z));
var a1 = 0.254829592;
Expand All @@ -14,17 +14,17 @@ function normalcdf(mean, sigma, to) {
sign = -1;
}
return (1 / 2) * (1 + sign * erf);
}
};

function calculateRank({
const calculateRank = ({
totalRepos,
totalCommits,
contributions,
followers,
prs,
issues,
stargazers,
}) {
}) => {
const COMMITS_OFFSET = 1.65;
const CONTRIBS_OFFSET = 1.65;
const ISSUES_OFFSET = 1;
Expand Down Expand Up @@ -72,6 +72,6 @@ function calculateRank({
})();

return { level, score: normalizedScore };
}
};

module.exports = calculateRank;
2 changes: 1 addition & 1 deletion src/cards/top-languages-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const useLanguages = (topLangs, hide, langs_count) => {
const renderTopLanguages = (topLangs, options = {}) => {
const {
hide_title,
hide_border,
hide_border = false,
card_width,
title_color,
text_color,
Expand Down
64 changes: 32 additions & 32 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const wrap = require("word-wrap");
const themes = require("../../themes");
const toEmoji = require("emoji-name-map");

/**
/** Renders error message on the card.
* @param {string} message
* @param {string} secondaryMessage
* @returns {string}
Expand Down Expand Up @@ -32,79 +32,79 @@ const renderError = (message, secondaryMessage = "") => {
* @param {string} str
* @returns {string}
*/
function encodeHTML(str) {
const encodeHTML = (str) => {
return str
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
return "&#" + i.charCodeAt(0) + ";";
})
.replace(/\u0008/gim, "");
}
};

/**
* @param {number} num
*/
function kFormatter(num) {
const kFormatter = (num) => {
return Math.abs(num) > 999
? Math.sign(num) * parseFloat((Math.abs(num) / 1000).toFixed(1)) + "k"
: Math.sign(num) * Math.abs(num);
}
};

/**
* @param {string} hexColor
* @returns {boolean}
*/
function isValidHexColor(hexColor) {
const isValidHexColor = (hexColor) => {
return new RegExp(
/^([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{4})$/,
).test(hexColor);
}
};

/**
* @param {string} value
* @returns {boolean | string}
*/
function parseBoolean(value) {
const parseBoolean = (value) => {
if (value === "true") {
return true;
} else if (value === "false") {
return false;
} else {
return value;
}
}
};

/**
* @param {string} str
*/
function parseArray(str) {
const parseArray = (str) => {
if (!str) return [];
return str.split(",");
}
};

/**
* @param {number} number
* @param {number} min
* @param {number} max
*/
function clampValue(number, min, max) {
const clampValue = (number, min, max) => {
// @ts-ignore
if (Number.isNaN(parseInt(number))) return min;
return Math.max(min, Math.min(number, max));
}
};

/**
* @param {string[]} colors
*/
function isValidGradient(colors) {
const isValidGradient = (colors) => {
return isValidHexColor(colors[1]) && isValidHexColor(colors[2]);
}
};

/**
* @param {string} color
* @param {string} fallbackColor
* @returns {string | string[]}
*/
function fallbackColor(color, fallbackColor) {
const fallbackColor = (color, fallbackColor) => {
let colors = color.split(",");
let gradient = null;

Expand All @@ -116,21 +116,21 @@ function fallbackColor(color, fallbackColor) {
(gradient ? gradient : isValidHexColor(color) && `#${color}`) ||
fallbackColor
);
}
};

/**
* @param {import('axios').AxiosRequestConfig['data']} data
* @param {import('axios').AxiosRequestConfig['headers']} headers
*/
function request(data, headers) {
const request = (data, headers) => {
// @ts-ignore
return axios({
url: "https://api.github.com/graphql",
method: "post",
headers,
data,
});
}
};

/**
* @param {object} props
Expand All @@ -145,7 +145,7 @@ function request(data, headers) {
* Auto layout utility, allows us to layout things
* vertically or horizontally with proper gaping
*/
function flexLayout({ items, gap, direction, sizes = [] }) {
const flexLayout = ({ items, gap, direction, sizes = [] }) => {
let lastSize = 0;
// filter() for filtering out empty strings
return items.filter(Boolean).map((item, i) => {
Expand All @@ -157,7 +157,7 @@ function flexLayout({ items, gap, direction, sizes = [] }) {
lastSize += size + gap;
return `<g transform="${transform}">${item}</g>`;
});
}
};

/**
* @typedef {object} CardColors
Expand All @@ -173,15 +173,15 @@ function flexLayout({ items, gap, direction, sizes = [] }) {
* returns theme based colors with proper overrides and defaults
* @param {CardColors} options
*/
function getCardColors({
const getCardColors = ({
title_color,
text_color,
icon_color,
bg_color,
border_color,
theme,
fallbackTheme = "default",
}) {
}) => {
const defaultTheme = themes[fallbackTheme];
const selectedTheme = themes[theme] || defaultTheme;
const defaultBorderColor =
Expand Down Expand Up @@ -212,15 +212,15 @@ function getCardColors({
);

return { titleColor, iconColor, textColor, bgColor, borderColor };
}
};

/**
* @param {string} text
* @param {number} width
* @param {number} maxLines
* @returns {string[]}
*/
function wrapTextMultiline(text, width = 59, maxLines = 3) {
const wrapTextMultiline = (text, width = 59, maxLines = 3) => {
const fullWidthComma = ",";
const encoded = encodeHTML(text);
const isChinese = encoded.includes(fullWidthComma);
Expand All @@ -245,7 +245,7 @@ function wrapTextMultiline(text, width = 59, maxLines = 3) {
// Remove empty lines if text fits in less than maxLines lines
const multiLineText = lines.filter(Boolean);
return multiLineText;
}
};

const noop = () => {};
// return console instance based on the environment
Expand Down Expand Up @@ -301,7 +301,7 @@ class MissingParamError extends Error {
* @param {number} fontSize
* @returns
*/
function measureText(str, fontSize = 10) {
const measureText = (str, fontSize = 10) => {
// prettier-ignore
const widths = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -333,7 +333,7 @@ function measureText(str, fontSize = 10) {
)
.reduce((cur, acc) => acc + cur) * fontSize
);
}
};

/** @param {string} name */
const lowercaseTrim = (name) => name.toLowerCase().trim();
Expand All @@ -344,7 +344,7 @@ const lowercaseTrim = (name) => name.toLowerCase().trim();
* @param {number} perChunk
* @returns {Array<T>}
*/
function chunkArray(arr, perChunk) {
const chunkArray = (arr, perChunk) => {
return arr.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / perChunk);

Expand All @@ -356,19 +356,19 @@ function chunkArray(arr, perChunk) {

return resultArray;
}, []);
}
};

/**
*
* @param {string} str
* @returns {string}
*/
function parseEmojis(str) {
const parseEmojis = (str) => {
if (!str) throw new Error("[parseEmoji]: str argument not provided");
return str.replace(/:\w+:/gm, (emoji) => {
return toEmoji.get(emoji) || "";
});
}
};

module.exports = {
renderError,
Expand Down
4 changes: 2 additions & 2 deletions src/fetchers/repo-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const urlExample = "/api/pin?username=USERNAME&amp;repo=REPO_NAME";
* @param {string} reponame
* @returns {Promise<import("./types").RepositoryData>}
*/
async function fetchRepo(username, reponame) {
const fetchRepo = async (username, reponame) => {
if (!username && !reponame) {
throw new MissingParamError(["username", "repo"], urlExample);
}
Expand Down Expand Up @@ -95,6 +95,6 @@ async function fetchRepo(username, reponame) {
starCount: data.organization.repository.stargazers.totalCount,
};
}
}
};

module.exports = fetchRepo;

0 comments on commit 5866cd0

Please sign in to comment.