Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
feather
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
feather
Commits
e697b3a9
Commit
e697b3a9
authored
Apr 01, 2018
by
Cole Bemis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Refactor build-sprite-string.js
parent
920bd457
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
30 deletions
+13
-30
build-sprite-string.test.js.snap
bin/__tests__/__snapshots__/build-sprite-string.test.js.snap
+1
-12
build-sprite-string.js
bin/build-sprite-string.js
+11
-13
build-sprite.js
bin/build-sprite.js
+1
-5
No files found.
bin/__tests__/__snapshots__/build-sprite-string.test.js.snap
View file @
e697b3a9
// Jest Snapshot v1, https://goo.gl/fbAQLP
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>"
`;
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
View file @
e697b3a9
import
defaultAttrs
from
'
../src/default-attrs.json
'
;
const
svgStartTag
=
`<svg xmlns="
${
defaultAttrs
.
xmlns
}
">\n<defs>\n`
;
const
svgEndTag
=
'
</defs>
\n
</svg>
'
;
/**
*
Renders the inner sprites as SVG Symbols
* @param {
object} icons the icons object
* @returns {string}
the rendered string with SVG symbols
*
Build an SVG sprite string containing SVG symbols.
* @param {
Object} icons
* @returns {string}
*/
function
buildSpriteString
(
icons
)
{
const
symbols
=
Object
.
keys
(
icons
)
.
map
(
icon
=>
toSvgSymbol
(
icon
,
icons
[
icon
]))
.
join
(
''
);
return
svgStartTag
+
symbols
+
svgEndTag
;
return
`<svg xmlns="
${
defaultAttrs
.
xmlns
}
"><defs>
${
symbols
}
</defs></svg>`
;
}
/**
*
Renders a SVG symbol tag
* @param {string} name
The name of the icon
* @param {string} contents
The contents of the icon
* @returns {string}
the rendered SVG symbol
*
Create an SVG symbol string.
* @param {string} name
- Icon name
* @param {string} contents
- SVG contents
* @returns {string}
*/
function
toSvgSymbol
(
name
,
contents
)
{
return
`<symbol id="
${
name
}
" viewBox="
${
defaultAttrs
.
viewBox
}
">
${
contents
}
\n</symbol>\n`
;
return
`<symbol id="
${
name
}
" viewBox="
${
defaultAttrs
.
viewBox
}
">
${
contents
}
</symbol>`
;
}
export
default
buildSpriteString
;
bin/build-sprite.js
View file @
e697b3a9
...
...
@@ -3,12 +3,8 @@ import path from 'path';
import
icons
from
'
../dist/icons.json
'
;
import
buildSpriteString
from
'
./build-sprite-string
'
;
const
sprite
=
buildSpriteString
(
icons
);
const
OUT_FILE
=
path
.
resolve
(
__dirname
,
'
../dist/feather-sprite.svg
'
);
console
.
log
(
`Building
${
OUT_FILE
}
`
);
// eslint-disable-line no-console
fs
.
writeFile
(
OUT_FILE
,
sprite
,
err
=>
{
if
(
err
)
throw
err
;
});
fs
.
writeFileSync
(
OUT_FILE
,
buildSpriteString
(
icons
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment