Simplify how scripts get icon slugs (#5066)

* Accept icon rather than title in `iconToSlug`

This removes the need in (most of the) calls to this function to
seperately check if there is a custom slug already defined for the icon.

* Rename `titleToSlug` to `getIconSlug`

* Revert unnecessary style change

* Update function documentation

* Keep `titleToSlug` and add `getIconSlug` helper

Unfortunately `this` is not available because we're using arrow syntax
for these functions, but refering to `titleToSlug` through
`module.exports` works fine as well.

* Fix parameter name in `getIconSlug` docs
This commit is contained in:
Eric Cornelissen 2021-02-22 14:15:37 +01:00 committed by GitHub
parent 1df43dd50b
commit 8431fd9683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 8 deletions

View File

@ -25,7 +25,7 @@ const indexTemplate = fs.readFileSync(indexTemplateFile, UTF8);
const iconObjectTemplate = fs.readFileSync(iconObjectTemplateFile, UTF8);
const data = require(dataFile);
const { titleToSlug } = require("./utils.js");
const { getIconSlug, titleToSlug } = require("./utils.js");
// Local helper functions
function escape(value) {
@ -61,7 +61,7 @@ function minifyAndWrite(filepath, rawJavaScript) {
// 'main'
const icons = [];
data.icons.forEach(icon => {
const filename = icon.slug || titleToSlug(icon.title);
const filename = getIconSlug(icon);
const svgFilepath = path.resolve(iconsDir, `${filename}.svg`);
icon.svg = fs.readFileSync(svgFilepath, UTF8).replace(/\r?\n/, '');
icon.slug = filename;

View File

@ -5,7 +5,7 @@
* icon SVG filename to standard output.
*/
const utils = require("./utils.js");
const { titleToSlug } = require("./utils.js");
if (process.argv.length < 3) {
console.error("Provide a brand name as argument");
@ -14,6 +14,6 @@ if (process.argv.length < 3) {
const brandName = process.argv.slice(3)
.reduce((acc, arg) => `${acc} ${arg}`, process.argv[2]);
const filename = utils.titleToSlug(brandName);
const filename = titleToSlug(brandName);
console.log(`For '${brandName}' use the file 'icons/${filename}.svg'`);
}

View File

@ -4,6 +4,12 @@
*/
module.exports = {
/**
* Get the slug/filename for an icon.
* @param {Object} icon The icon data as it appears in _data/simple-icons.json
*/
getIconSlug: icon => icon.slug || module.exports.titleToSlug(icon.title),
/**
* Converts a brand title into a slug/filename.
* @param {String} title The title to convert

View File

@ -1,8 +1,8 @@
const { icons } = require('../_data/simple-icons.json');
const { titleToSlug } = require('../scripts/utils.js');
const { getIconSlug } = require('../scripts/utils.js');
icons.forEach(icon => {
const filename = icon.slug || titleToSlug(icon.title);
const filename = getIconSlug(icon);
const subject = require(`../icons/${filename}.js`);
test(`${icon.title} has a "title"`, () => {

View File

@ -1,6 +1,6 @@
const { icons } = require('../_data/simple-icons.json');
const simpleIcons = require('../index.js');
const { titleToSlug } = require("../scripts/utils.js");
const { getIconSlug } = require("../scripts/utils.js");
icons.forEach(icon => {
const name = icon.slug || icon.title;
@ -45,7 +45,7 @@ icons.forEach(icon => {
}
test(`${icon.title} can be found by it's slug`, () => {
const name = icon.slug || titleToSlug(icon.title);
const name = getIconSlug(icon);
const found = simpleIcons.get(name);
expect(found).toBeDefined();
expect(found.title).toEqual(icon.title);