Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Last active September 1, 2023 08:53
Show Gist options
  • Save manishsongirkar/c0d87703c17ef5973475d9405b89ee17 to your computer and use it in GitHub Desktop.
Save manishsongirkar/c0d87703c17ef5973475d9405b89ee17 to your computer and use it in GitHub Desktop.
Fetch FontAwesome icons through JSON and generate custom data object in console
document.addEventListener( 'DOMContentLoaded', function() {
var getPost = async function() {
var response = await fetch( 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/6.x/metadata/icons.json' );
var icons = await response.json();
let fontAwesomeObj = {};
let currentObject = fontAwesomeObj;
Object.entries( icons ).forEach( ( [ key, icon ] ) => {
const styles = icon.styles;
const iconName = `fa-${ key }`;
const iconUnicode = icon.unicode;
styles.forEach( ( style, index ) => {
currentObject[key + '-' + style] = {};
currentObject[key + '-' + style].name = key + '-' + style;
currentObject[key + '-' + style].slug = key;
currentObject[key + '-' + style].title = icon.label;
currentObject[key + '-' + style].type = 'fa';
currentObject[key + '-' + style].style = style;
currentObject[key + '-' + style].unicode = '\\' + iconUnicode;
currentObject[key + '-' + style].classes = `${ iconName } fa-${ style }`;
currentObject[key + '-' + style].searchTerms = undefined !== icon.search?.terms ? icon.search.terms : [];
currentObject[key + '-' + style].aliasesNames = undefined !== icon.aliases?.names ? icon.aliases.names : [];
} );
} );
// Check output in console.
// window.console.log( fontAwesomeObj );
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent( JSON.stringify( fontAwesomeObj ) );
var downloadAnchorNode = document.createElement( 'a' );
downloadAnchorNode.setAttribute( "href", dataStr );
downloadAnchorNode.setAttribute( "download", "font-awesome-min.json" );
document.body.appendChild( downloadAnchorNode );
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
getPost();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment