@publidata/utils-fontawesome
Version:
Collection of methods to handle opening hours
141 lines (121 loc) • 3.99 kB
JavaScript
const FontAwesomeApi = require("../FontAwesomeApi/index.js");
const { isValidFa, isFaKit } = require("../validators");
const {
icon,
library,
findIconDefinition,
} = require("@fortawesome/fontawesome-svg-core");
/**
* Provides methods to fetch svgs from FontAwesome
*/
class FontAwesomeUtils {
api;
static _kit;
static _token;
constructor(token, kit) {
this._kit = kit;
this._token = token;
if (this._token) this.api = new FontAwesomeApi(this._token);
}
/**
* Set the token to use for the api
* @param {string} token
* @returns {void}
* @memberof FontAwesomeSvg
* @example FontAwesome.setToken("TOKEN");
*/
setToken(token) {
this._token = token;
this.api = new FontAwesomeApi(this._token);
}
/**
* Set the kit to use for the api
* @param {string} kit
* @returns {void}
* @memberof FontAwesomeSvg
* @example
* FontAwesomeSvg.setKit("2BC39F11-A80E-4DB4-AA7A-41FC26176101");
*/
setKit(kit) {
this._kit = kit;
}
/**
* Fetch the svg from FontAwesome
* @param {string} fa
* @returns {Promise<string>}
* @memberof FontAwesomeSvg
* @example
* const fa = "fas fa-nop";
* FontAwesomeSvg.toSgv(fa).then(res => {
* console.log(res);
* });
*/
toSvg(fa) {
return new Promise((resolve, reject) => {
if (!isValidFa(fa)) return reject(Error("Invalid FontAwesome object"));
const iconName = fa.replace("fa-", "").split(" ")[1];
if (!this._token) reject(Error("FontAwesome token not set"));
if (!isFaKit(fa)) {
return this.api.getIcon(iconName).then((icon) => {
resolve(icon?.html);
});
}
if (!this._kit) reject(Error("FontAwesome kit not set"));
this.api.kitIcons(this._kit, [iconName]).then((icons) => {
if (!icons.length) reject(Error("Invalid FontAwesome custom icon"));
const asSvg = faKitAsSvg(icons[0]);
if (!asSvg)
console.log(
`Error while converting to svg, this icon is invalid : ${iconName} for kit ${this._kit}`
);
resolve(asSvg);
});
});
}
toSvgs(fas) {
return new Promise((resolve, reject) => {
if (!Array.isArray(fas)) reject(Error("Invalid FontAwesome array"));
const cleanFas = fas.filter(isValidFa);
const kitFas = cleanFas.filter(isFaKit);
const nativeFas = cleanFas.filter((fa) => !isFaKit(fa));
const hasSvgKey = (fa) => fa.svg;
let nativeSgvs = nativeFas
.map((fa) => ({ fa, svg: nativeAsSvg(fa) }))
.filter(hasSvgKey);
if (!kitFas.length) resolve(nativeSgvs);
if (!this._kit) reject(Error("FontAwesome kit not set"));
if (!this._token) reject(Error("FontAwesome token not set"));
const kitIcons = kitFas.filter(isFaKit).map(
(fa) => fa?.replace("fa-", "")?.split(" ")[1]
);
if (!kitIcons.length) resolve(nativeSgvs);
this.api.kitIcons(this._kit, kitIcons).then((icons) => {
let kitSgvs = icons
.map((icon, index) => {
const fa = `fak fa-${icon.name}`;
const svg = faKitAsSvg(icon);
if (!svg)
console.log(
`Error while converting to svg, this icon is invalid : ${kitIcons[index]} for kit ${this._kit}`
);
return { fa, svg };
})
.filter(hasSvgKey);
const sgvs = [...nativeSgvs, ...kitSgvs];
resolve(sgvs);
});
});
}
}
const nativeAsSvg = (fa) => {
const [prefix, iconName] = fa.replace("fa-", "").split(" ");
const iconDefinition = findIconDefinition({ prefix, iconName });
if (!iconDefinition) return null;
return icon(iconDefinition).html[0];
};
const faKitAsSvg = icon => {
if (!icon) return null;
const { height, width, path } = icon;
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}"><path d="${path}"></path></svg>`;
};
module.exports = new FontAwesomeUtils();