Skip to content

Commit e697b3a

Browse files
committed
refactor: Refactor build-sprite-string.js
1 parent 920bd45 commit e697b3a

3 files changed

Lines changed: 13 additions & 30 deletions

File tree

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`builds sprite correctly 1`] = `
4-
"<svg xmlns=\\"http://www.w3.org/2000/svg\\">
5-
<defs>
6-
<symbol id=\\"icon1\\" viewBox=\\"0 0 24 24\\">
7-
<line x1=\\"23\\" y1=\\"1\\" x2=\\"1\\" y2=\\"23\\"></line><line x1=\\"1\\" y1=\\"1\\" x2=\\"23\\" y2=\\"23\\"></line>
8-
</symbol>
9-
<symbol id=\\"icon2\\" viewBox=\\"0 0 24 24\\">
10-
<circle cx=\\"12\\" cy=\\"12\\" r=\\"11\\"></circle>
11-
</symbol>
12-
</defs>
13-
</svg>"
14-
`;
3+
exports[`builds sprite correctly 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><defs><symbol id=\\"icon1\\" viewBox=\\"0 0 24 24\\"><line x1=\\"23\\" y1=\\"1\\" x2=\\"1\\" y2=\\"23\\"></line><line x1=\\"1\\" y1=\\"1\\" x2=\\"23\\" y2=\\"23\\"></line></symbol><symbol id=\\"icon2\\" viewBox=\\"0 0 24 24\\"><circle cx=\\"12\\" cy=\\"12\\" r=\\"11\\"></circle></symbol></defs></svg>"`;

bin/build-sprite-string.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import defaultAttrs from '../src/default-attrs.json';
22

3-
const svgStartTag = `<svg xmlns="${defaultAttrs.xmlns}">\n<defs>\n`;
4-
const svgEndTag = '</defs>\n</svg>';
5-
63
/**
7-
* Renders the inner sprites as SVG Symbols
8-
* @param {object} icons the icons object
9-
* @returns {string} the rendered string with SVG symbols
4+
* Build an SVG sprite string containing SVG symbols.
5+
* @param {Object} icons
6+
* @returns {string}
107
*/
118
function buildSpriteString(icons) {
129
const symbols = Object.keys(icons)
1310
.map(icon => toSvgSymbol(icon, icons[icon]))
1411
.join('');
1512

16-
return svgStartTag + symbols + svgEndTag;
13+
return `<svg xmlns="${defaultAttrs.xmlns}"><defs>${symbols}</defs></svg>`;
1714
}
1815

1916
/**
20-
* Renders a SVG symbol tag
21-
* @param {string} name The name of the icon
22-
* @param {string} contents The contents of the icon
23-
* @returns {string} the rendered SVG symbol
17+
* Create an SVG symbol string.
18+
* @param {string} name - Icon name
19+
* @param {string} contents - SVG contents
20+
* @returns {string}
2421
*/
2522
function toSvgSymbol(name, contents) {
26-
return `<symbol id="${name}" viewBox="${defaultAttrs.viewBox}">
27-
${contents}\n</symbol>\n`;
23+
return `<symbol id="${name}" viewBox="${defaultAttrs.viewBox}">${
24+
contents
25+
}</symbol>`;
2826
}
2927

3028
export default buildSpriteString;

bin/build-sprite.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import path from 'path';
33
import icons from '../dist/icons.json';
44
import buildSpriteString from './build-sprite-string';
55

6-
const sprite = buildSpriteString(icons);
7-
86
const OUT_FILE = path.resolve(__dirname, '../dist/feather-sprite.svg');
97

108
console.log(`Building ${OUT_FILE}`); // eslint-disable-line no-console
119

12-
fs.writeFile(OUT_FILE, sprite, err => {
13-
if (err) throw err;
14-
});
10+
fs.writeFileSync(OUT_FILE, buildSpriteString(icons));

0 commit comments

Comments
 (0)