UNPKG

@publidata/utils-fontawesome

Version:
144 lines (121 loc) 2.93 kB
# @publidata/utils-fontawesome ## Description: Provides utilities to work with fontawesome icons and retrieve them as SVG. ## Installation: ```bash npm install @publidata/utils-fontawesome # or yarn add @publidata/utils-fontawesome ``` ## Usage: Retrieve a FA icon as SVG (native of from a kit) : ```js import { FontAwesomeUtils } from "@publidata/utils-fontawesome"; const icons = [ "fas fa-home", // Native icon are retrieved from fontawesome npm package "far fa-home", // Regular icon are retrieved from fontawesome npm package "fa-home", // No prefix will not work "fak fa-[name]", // Custom icon are retrieved from fontawesome api ]; // Only if you use icons from a fontawesome kit FontAwesomeUtils.setToken("YOUR_TOKEN"); FontAwesomeUtils.setKit("KIT_ID"); // To get a single icon FontAwesomeUtils.toSvg("fas fa-ambulance").then(svg => { console.log(svg); }); // To get multiple icons FontAwesomeUtils.toSvgs(icons).then(svgs => { console.log(svgs); }); ``` The `FontAwesomeApi` class allows to communicate with the fontawesome api. ```js import { FontAwesomeApi } from "@publidata/utils-fontawesome"; const fontAwesomeApi = new FontAwesomeApi("YOUR_TOKEN"); // Mandatory to pass the token in the constructor // Get all kits related to the account fontAwesomeApi.kits().then(kits => { console.log(kits); }); /* LOGS : -------- { me { kits [ { id, name, token, version, licenseSelected, technologySelected }, ... ] } } */ // Get one kit with all details fontAwesomeApi.getKit("KIT_ID").then(kit => { console.log(kit); }); /* LOGS : -------- { me: { kit: { name, token, version, licenseSelected, technologySelected, iconUploads:[ { name, path, width, height, version, unicode }, ... ] } } */ // Get all icons related uploded to the kit fontAwesomeApi.kitIcons("KIT_ID").then(icons => { console.log(icons); }); /* LOGS : -------- [ { name, path, width, height, version, unicode } ] */ ``` Use `isValidFa` to check if a string is a valid fontawesome icon. ```js import { isValidFa } from "@publidata/utils-fontawesome"; isValidFa("fas fa-home"); // true isValidFa("far fa-home"); // true isValidFa("fa-home"); // false isValidFa("fak fa-[name]"); // true ``` See the test file for more examples : ./tests/4_validators.test.js Use `isFaKit` to check if a string is a valid fontawesome kit. ```js import { isFaKit } from "@publidata/utils-fontawesome"; isFaKit("fas fa-home"); // false isFaKit("far fa-home"); // false isFaKit("fa-home"); // false isFaKit("fak fa-[name]"); // true ``` See the test file for more examples : ./tests/4_validators.test.js