mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-12 13:38:32 +00:00
* New setup for new NPM package * Add build scripts for dist * Add introduction readme * Refactor names * update package.json * remove log * rename variable * Factoring * Improve optimize script * Add eslint config * Eslint fixes * rename import * Move packeges * Setup rollup and build progress * Refactor scripts * fix lint error * remove lint disabler * Bring back old libraries * add indentation * reset packages directory * remove vscode setting files * 0.1.0-alpha.0 * new version * 0.1.0-alpha.1 * Fix build process * Add create element to the entry file * update version number * publish new alhpa version * fixing bugs * Add jest and tests * replace with XML createElement * set new version * Fix svg generation * Add tests for main library * Update docs * Adjust tests and selectors * update the spec * Update README.md * Update README.md * Update README.md * update version * Update README.md * Move function to helpers file * rename license, package and readme * Fix build files * rename packages Co-authored-by: Eric Fennis <eric.fennis@endurance.com>
84 lines
2.0 KiB
JavaScript
84 lines
2.0 KiB
JavaScript
import { camelCase, upperFirst } from 'lodash-es';
|
|
import createElement from './createElement';
|
|
|
|
/**
|
|
* Get the attributes of an HTML element.
|
|
* @param {HTMLElement} element
|
|
* @returns {Object}
|
|
*/
|
|
export const getAttrs = element =>
|
|
Array.from(element.attributes).reduce((attrs, attr) => {
|
|
attrs[attr.name] = attr.value;
|
|
return attrs;
|
|
}, {});
|
|
|
|
/**
|
|
* Gets the classNames of an attributes Object.
|
|
* @param {Object} attrs
|
|
* @returns {Array}
|
|
*/
|
|
export const getClassNames = attrs => {
|
|
if (!attrs || !attrs.class) return '';
|
|
if (attrs.class && typeof attrs.class === 'string') {
|
|
return attrs.class.split(' ');
|
|
}
|
|
if (attrs.class && Array.isArray(attrs.class)) {
|
|
return attrs.class;
|
|
}
|
|
return '';
|
|
};
|
|
|
|
/**
|
|
* Combines the classNames of array of classNames to a String
|
|
* @param {Array} arrayOfClassnames
|
|
* @returns {String}
|
|
*/
|
|
export const combineClassNames = arrayOfClassnames => {
|
|
const classNameArray = arrayOfClassnames.flatMap(getClassNames);
|
|
|
|
return classNameArray
|
|
.map(classItem => classItem.trim())
|
|
.filter(Boolean)
|
|
.join(' ');
|
|
};
|
|
|
|
/**
|
|
* ReplaceElement, replaces the given element with the created icon.
|
|
* @param {HTMLElement} element
|
|
* @param {Object: {String, Array, Object}} options: { nameAttr, icons, attrs }
|
|
* @returns {Function}
|
|
*/
|
|
export default (element, { nameAttr, icons, attrs }) => {
|
|
const iconName = element.getAttribute(nameAttr);
|
|
const ComponentName = upperFirst(camelCase(iconName));
|
|
|
|
const iconNode = icons[ComponentName];
|
|
|
|
if (!iconNode) {
|
|
return console.warn(
|
|
`${element.outerHTML} icon name was not found in the provided icons object.`,
|
|
);
|
|
}
|
|
|
|
const elementAttrs = getAttrs(element);
|
|
|
|
const [, iconAttrs] = iconNode;
|
|
|
|
const allAttrs = {
|
|
...iconAttrs,
|
|
...attrs,
|
|
};
|
|
|
|
iconNode[1] = { ...allAttrs };
|
|
|
|
const classNames = combineClassNames([iconAttrs, elementAttrs, attrs]);
|
|
|
|
if (classNames) {
|
|
iconNode[1].class = classNames;
|
|
}
|
|
|
|
const svgElement = createElement(iconNode);
|
|
|
|
return element.parentNode.replaceChild(svgElement, element);
|
|
};
|