All files utils.js

100% Statements 43/43
62.5% Branches 10/16
100% Functions 13/13
100% Lines 39/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99    1x           1x 1x 1x     1x 1x             1x 1x 1x 1x   8x   1x 1x 8x   1x     1x 4x   1x   1x 976x   19x   976x 970x                           4x   4x 4x 4x 4x 4x   4x       1x 4x   4x           1x 2x     2x 2x     2x           2x      
import once from 'once';
 
export const supportsInlineSVG = once(() => {
  /* istanbul ignore next */
  if (!document) {
    return false;
  }
 
  const div = document.createElement('div');
  div.innerHTML = '<svg />';
  return div.firstChild && div.firstChild.namespaceURI === 'http://www.w3.org/2000/svg';
});
 
export const isSupportedEnvironment = once(() =>
  (
    (typeof window !== 'undefined' && window !== null ? window.XMLHttpRequest : false) ||
    (typeof window !== 'undefined' && window !== null ? window.XDomainRequest : false)
  )
  && supportsInlineSVG()
);
 
export const randomString = (length = 8) => {
  const letters = 'abcdefghijklmnopqrstuvwxyz';
  const numbers = '1234567890';
  const charset = letters + letters.toUpperCase() + numbers;
 
  const randomCharacter = array => array[Math.floor(Math.random() * array.length)];
 
  let R = '';
  for (let i = 0; i < length; i++) {
    R += randomCharacter(charset);
  }
  return R;
};
 
export const uniquifySVGIDs = (() => {
  const mkAttributePattern = attr => `(?:(?:\\s|\\:)${attr})`;
 
  const idPattern = new RegExp(`(?:(${(mkAttributePattern('id'))})="([^"]+)")|(?:(${(mkAttributePattern('href'))}|${(mkAttributePattern('role'))}|${(mkAttributePattern('arcrole'))})="\\#([^"]+)")|(?:="url\\(\\#([^\\)]+)\\)")`, 'g');
 
  return (svgText, svgID) => {
    const uniquifyID = id => `${id}___${svgID}`;
 
    return svgText.replace(idPattern, (m, p1, p2, p3, p4, p5) => { //eslint-disable-line consistent-return
      /* istanbul ignore else */
      if (p2) {
        return `${p1}="${(uniquifyID(p2))}"`;
      }
      else if (p4) {
        return `${p3}="#${(uniquifyID(p4))}"`;
      }
      else if (p5) {
        return `="url(#${(uniquifyID(p5))})"`;
      }
    });
  };
})();
 
class InlineSVGError extends Error {
  constructor(message) {
    super();
 
    this.name = 'InlineSVGError';
    this.isSupportedBrowser = true;
    this.isConfigurationError = false;
    this.isUnsupportedBrowserError = false;
    this.message = message;
 
    return this;
  }
}
 
const createError = (message, attrs) => {
  const err = new InlineSVGError(message);
 
  return {
    ...err,
    ...attrs,
  };
};
 
export const unsupportedBrowserError = message => {
  let newMessage = message;
 
  /* istanbul ignore else */
  if (!newMessage) {
    newMessage = 'Unsupported Browser';
  }
 
  return createError(newMessage, {
    isSupportedBrowser: false,
    isUnsupportedBrowserError: true
  });
};
 
export const configurationError = message => createError(message, {
  isConfigurationError: true
});