build-sprite-string.js 696 Bytes
Newer Older
1
import DEFAULT_ATTRS from '../src/default-attrs.json';
2 3

/**
4 5 6
 * Build an SVG sprite string containing SVG symbols.
 * @param {Object} icons
 * @returns {string}
7 8 9 10 11 12
 */
function buildSpriteString(icons) {
  const symbols = Object.keys(icons)
    .map(icon => toSvgSymbol(icon, icons[icon]))
    .join('');

13
  return `<svg xmlns="${DEFAULT_ATTRS.xmlns}"><defs>${symbols}</defs></svg>`;
14 15 16
}

/**
17 18 19 20
 * Create an SVG symbol string.
 * @param {string} name - Icon name
 * @param {string} contents - SVG contents
 * @returns {string}
21 22
 */
function toSvgSymbol(name, contents) {
23
  return `<symbol id="${name}" viewBox="${DEFAULT_ATTRS.viewBox}">${
24 25
    contents
  }</symbol>`;
26 27 28
}

export default buildSpriteString;