mirror of
https://github.com/Mibew/simple-icons.git
synced 2025-01-31 14:24:45 +03:00
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"
This commit is contained in:
parent
e1c7f20914
commit
c6a9346985
@ -42,7 +42,7 @@ The API can then be used as follows:
|
|||||||
```javascript
|
```javascript
|
||||||
const simpleIcons = require('simple-icons');
|
const simpleIcons = require('simple-icons');
|
||||||
|
|
||||||
console.log(simpleIcons['Simple Icons']);
|
console.log(simpleIcons.get('Simple Icons'));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
{
|
{
|
||||||
|
7
package-lock.json
generated
7
package-lock.json
generated
@ -868,8 +868,7 @@
|
|||||||
"version": "2.20.0",
|
"version": "2.20.0",
|
||||||
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz",
|
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz",
|
||||||
"integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==",
|
"integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"optional": true
|
|
||||||
},
|
},
|
||||||
"compare-versions": {
|
"compare-versions": {
|
||||||
"version": "3.4.0",
|
"version": "3.4.0",
|
||||||
@ -5629,7 +5628,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.0.tgz",
|
||||||
"integrity": "sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==",
|
"integrity": "sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
"requires": {
|
||||||
"commander": "~2.20.0",
|
"commander": "~2.20.0",
|
||||||
"source-map": "~0.6.1"
|
"source-map": "~0.6.1"
|
||||||
@ -5639,8 +5637,7 @@
|
|||||||
"version": "0.6.1",
|
"version": "0.6.1",
|
||||||
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||||
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
|
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
|
||||||
"dev": true,
|
"dev": true
|
||||||
"optional": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
"editorconfig-checker": "^2.0.8",
|
"editorconfig-checker": "^2.0.8",
|
||||||
"jest": "^24.1.0",
|
"jest": "^24.1.0",
|
||||||
"jsonlint2": "^1.7.1",
|
"jsonlint2": "^1.7.1",
|
||||||
"svglint": "^1.0.4"
|
"svglint": "^1.0.4",
|
||||||
|
"uglify-js": "^3.6.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"jsonlint": "jsonlint _data/simple-icons.json -q -V .jsonlintschema",
|
"jsonlint": "jsonlint _data/simple-icons.json -q -V .jsonlintschema",
|
||||||
|
@ -7,12 +7,16 @@
|
|||||||
* Also generates an index.js that exports all icons by title, but is not tree-shakeable
|
* 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 dataFile = "../_data/simple-icons.json";
|
||||||
const indexFile = `${__dirname}/../index.js`;
|
const indexFile = `${__dirname}/../index.js`;
|
||||||
const iconsDir = `${__dirname}/../icons`;
|
const iconsDir = `${__dirname}/../icons`;
|
||||||
const data = require(dataFile);
|
const indexTemplateFile = `${__dirname}/templates/index.js`;
|
||||||
const fs = require("fs");
|
|
||||||
|
|
||||||
|
const data = require(dataFile);
|
||||||
const { titleToFilename } = require("./utils");
|
const { titleToFilename } = require("./utils");
|
||||||
|
|
||||||
// Local helper functions
|
// Local helper functions
|
||||||
@ -39,5 +43,10 @@ data.icons.forEach(icon => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// write our generic index.js
|
// write our generic index.js
|
||||||
const iconsString = icons.map(iconToKeyValue).join(',');
|
const indexTemplate = fs.readFileSync(indexTemplateFile, "utf8");
|
||||||
fs.writeFileSync(indexFile, `module.exports={${iconsString}};`);
|
const { error, code } = minify(util.format(indexTemplate, icons.map(iconToKeyValue).join(',')));
|
||||||
|
if (error) {
|
||||||
|
process.exit(1);
|
||||||
|
} else {
|
||||||
|
fs.writeFileSync(indexFile, code);
|
||||||
|
}
|
||||||
|
17
scripts/templates/index.js
Normal file
17
scripts/templates/index.js
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
const { icons } = require('../_data/simple-icons.json');
|
const { icons } = require('../_data/simple-icons.json');
|
||||||
const simpleIcons = require('../index.js');
|
const simpleIcons = require('../index.js');
|
||||||
|
const { titleToFilename } = require("../scripts/utils.js");
|
||||||
|
|
||||||
icons.forEach(icon => {
|
icons.forEach(icon => {
|
||||||
const subject = simpleIcons[icon.title];
|
const subject = simpleIcons[icon.title];
|
||||||
@ -29,4 +30,17 @@ icons.forEach(icon => {
|
|||||||
test(`${icon.title} has a "slug"`, () => {
|
test(`${icon.title} has a "slug"`, () => {
|
||||||
expect(typeof subject.slug).toBe('string');
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user