replace-sprite-placeholder.js 722 B

1234567891011121314151617181920212223242526
  1. const escapeRegExpSpecialChars = require('escape-string-regexp');
  2. const isWindows = /^win/i.test(process.platform);
  3. /**
  4. * @param {string} content
  5. * @param {Object<string, string>} replacements
  6. * @return {string}
  7. */
  8. function replaceSpritePlaceholder(content, replacements) {
  9. let result = content;
  10. Object.keys(replacements)
  11. .forEach((subj) => {
  12. let re = new RegExp(escapeRegExpSpecialChars(subj), 'g');
  13. result = result.replace(re, replacements[subj]);
  14. if (isWindows) {
  15. re = new RegExp(escapeRegExpSpecialChars(subj), 'g');
  16. result = result.replace(/\\\\/g, '\\').replace(re, replacements[subj]);
  17. }
  18. });
  19. return result;
  20. }
  21. module.exports = replaceSpritePlaceholder;