From c6a9346985af70b8f1905823a561bf12c9630c6c Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sun, 14 Jul 2019 20:05:38 +0100 Subject: [PATCH] Generic get icon function for easier access to brands with "difficult" names (#1522) * Add URL friendy/slug/file name as property to package icon objects * Test new property * Build index from template * Add .get function to index.js export * Test new .get function in index.js export * Use Uglify-JS to minify the code in the package's index.js * Update API in README.md * Renaem test using "slug" instead of "name" --- README.md | 2 +- package-lock.json | 7 ++----- package.json | 3 ++- scripts/prepublish.js | 17 +++++++++++++---- scripts/templates/index.js | 17 +++++++++++++++++ tests/index.test.js | 14 ++++++++++++++ 6 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 scripts/templates/index.js diff --git a/README.md b/README.md index cce2fe71..5e973f0f 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ The API can then be used as follows: ```javascript const simpleIcons = require('simple-icons'); -console.log(simpleIcons['Simple Icons']); +console.log(simpleIcons.get('Simple Icons')); /* { diff --git a/package-lock.json b/package-lock.json index 10cb86f5..58795944 100644 --- a/package-lock.json +++ b/package-lock.json @@ -868,8 +868,7 @@ "version": "2.20.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==", - "dev": true, - "optional": true + "dev": true }, "compare-versions": { "version": "3.4.0", @@ -5629,7 +5628,6 @@ "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.0.tgz", "integrity": "sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==", "dev": true, - "optional": true, "requires": { "commander": "~2.20.0", "source-map": "~0.6.1" @@ -5639,8 +5637,7 @@ "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true, - "optional": true + "dev": true } } }, diff --git a/package.json b/package.json index 44a4c727..e9d1363d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "editorconfig-checker": "^2.0.8", "jest": "^24.1.0", "jsonlint2": "^1.7.1", - "svglint": "^1.0.4" + "svglint": "^1.0.4", + "uglify-js": "^3.6.0" }, "scripts": { "jsonlint": "jsonlint _data/simple-icons.json -q -V .jsonlintschema", diff --git a/scripts/prepublish.js b/scripts/prepublish.js index 70954797..7fa7e1e2 100755 --- a/scripts/prepublish.js +++ b/scripts/prepublish.js @@ -7,12 +7,16 @@ * Also generates an index.js that exports all icons by title, but is not tree-shakeable */ +const fs = require("fs"); +const util = require("util"); +const minify = require("uglify-js").minify; + const dataFile = "../_data/simple-icons.json"; const indexFile = `${__dirname}/../index.js`; const iconsDir = `${__dirname}/../icons`; -const data = require(dataFile); -const fs = require("fs"); +const indexTemplateFile = `${__dirname}/templates/index.js`; +const data = require(dataFile); const { titleToFilename } = require("./utils"); // Local helper functions @@ -39,5 +43,10 @@ data.icons.forEach(icon => { }); // write our generic index.js -const iconsString = icons.map(iconToKeyValue).join(','); -fs.writeFileSync(indexFile, `module.exports={${iconsString}};`); +const indexTemplate = fs.readFileSync(indexTemplateFile, "utf8"); +const { error, code } = minify(util.format(indexTemplate, icons.map(iconToKeyValue).join(','))); +if (error) { + process.exit(1); +} else { + fs.writeFileSync(indexFile, code); +} diff --git a/scripts/templates/index.js b/scripts/templates/index.js new file mode 100644 index 00000000..8dda4057 --- /dev/null +++ b/scripts/templates/index.js @@ -0,0 +1,17 @@ +var icons = {%s}; + +module.exports = icons; +module.exports.get = function(targetName) { + if (icons[targetName]) { + return icons[targetName]; + } else { + var normalizedName = targetName.toLowerCase(); + for (var iconName in icons) { + var icon = icons[iconName]; + if ((icon.title && icon.title.toLowerCase() === normalizedName) + || (icon.slug && icon.slug === normalizedName)) { + return icon; + } + } + } +} diff --git a/tests/index.test.js b/tests/index.test.js index fcec8937..f040045b 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,5 +1,6 @@ const { icons } = require('../_data/simple-icons.json'); const simpleIcons = require('../index.js'); +const { titleToFilename } = require("../scripts/utils.js"); icons.forEach(icon => { const subject = simpleIcons[icon.title]; @@ -29,4 +30,17 @@ icons.forEach(icon => { test(`${icon.title} has a "slug"`, () => { expect(typeof subject.slug).toBe('string'); }); + + test(`${icon.title} can be found by it's title`, () => { + const found = simpleIcons.get(icon.title); + expect(found).toBeDefined(); + expect(found.title).toEqual(icon.title); + }); + + test(`${icon.title} can be found by it's slug`, () => { + const name = titleToFilename(icon.title); + const found = simpleIcons.get(name); + expect(found).toBeDefined(); + expect(found.title).toEqual(icon.title); + }); });