Skip to content

Commit

Permalink
Merge pull request #7 from Infineon/6-add-geticons-functionality-to-l…
Browse files Browse the repository at this point in the history
…ibrary

add getIcons functionality to library
  • Loading branch information
kaiwerther authored Sep 30, 2022
2 parents 27aa3b6 + 6994b24 commit d8d21eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/generate_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs').promises;
const fsr = require('fs');
const path = require('path');

const regex = /<svg.*height="(.*?)".*?width="(.*?)".*?>(.*)<\/svg>/m;
const regex = /<svg.*?height="(.*?)".*?width="(.*?)".*?>(.*)<\/svg>/m;
const svgSourceFolder = './svg/';
const jsTargetFolder = './generated_js/';

Expand Down
22 changes: 12 additions & 10 deletions library/library.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const iconLibrary = {};
const iconLibraryAlternativeName = {};
const iconLibraryName = {};

const library = {
add: (...icons) => {
Expand All @@ -8,25 +9,26 @@ const library = {
// first check if icon is a valid icon
if (!name || !height || !width || !svgContent) {
console.error(`Trying to add unsupported icon to library "${name}"`);
} else if (iconLibrary[name] && iconLibrary[name]) {
} else if (iconLibraryName[name]) {
console.error(`Adding icon twice ${name}`);
} else {
if (!iconLibrary[name]) {
iconLibrary[name] = {};
}
const iconObj = { height, width, svgContent };
iconLibrary[name] = iconObj;
iconLibrary[name.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase())] = iconObj;
const alternativeName = name.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
const iconObj = {
name, alternativeName, height, width, svgContent,
};
iconLibraryName[name] = iconObj;
iconLibraryAlternativeName[alternativeName] = iconObj;
}
});
},
getIcon: (name) => {
if (!iconLibrary[name]) {
if (!iconLibraryName[name] && !iconLibraryAlternativeName[name]) {
console.error(`Trying to load icon that was not put into iconLibrary before "${name}"`);
return undefined;
}
return iconLibrary[name];
return iconLibraryName[name] || iconLibraryAlternativeName[name];
},
getIcons: () => iconLibraryName,
};

export default library;

0 comments on commit d8d21eb

Please sign in to comment.