Files
lucide/scripts/render/renderIconNodes.js
Eric Fennis cedf113b54 React package (#105)
* init new react package project

* setup react package

* Fix config for build

* update package

* Fix build

* update package

* fix package.json

* refactor and cleanup

* bumb beta version

* fix regex

* bump version

* Add hashing

* Add tests

* bump version

* Add peer dependecies

* Add docs

* Add readme link
2020-12-02 13:48:39 +01:00

52 lines
1.2 KiB
JavaScript

/* eslint-disable import/no-extraneous-dependencies */
import { parseDOM } from 'htmlparser2';
import DEFAULT_ATTRS from './default-attrs.json';
import { toCamelCase, hash } from '../helpers';
const camelizeAttrs = attrs =>
Object.keys(attrs).reduce((newAttrs, attr) => {
const attrKey = toCamelCase(attr);
newAttrs[attrKey] = attrs[attr];
return newAttrs;
}, {});
export default (iconsObject, options) => {
const iconNodes = {};
Object.keys(iconsObject).forEach(icon => {
const svgString = iconsObject[icon];
const dom = parseDOM(svgString);
const children = dom.map(element => {
if (options.renderUniqueKey) {
const hashSource = {
name: element.name,
...element.attribs,
};
const uniqueKey = hash(JSON.stringify(hashSource));
element.attribs.key = uniqueKey;
}
return [
element.name,
{
...(options.camelizeAttrs ? camelizeAttrs(element.attribs) : element.attribs),
},
];
});
iconNodes[icon] = [
'svg',
{
...(options.camelizeAttrs ? camelizeAttrs(DEFAULT_ATTRS) : DEFAULT_ATTRS),
},
children,
];
});
return iconNodes;
};