id.cjs 964 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const regex = /\sid="(\S+)"/g;
  3. const randomPrefix = "IconifyId" + Date.now().toString(16) + (Math.random() * 16777216 | 0).toString(16);
  4. let counter = 0;
  5. function replaceIDs(body, prefix = randomPrefix) {
  6. const ids = [];
  7. let match;
  8. while (match = regex.exec(body)) {
  9. ids.push(match[1]);
  10. }
  11. if (!ids.length) {
  12. return body;
  13. }
  14. const suffix = "suffix" + (Math.random() * 16777216 | Date.now()).toString(16);
  15. ids.forEach((id) => {
  16. const newID = typeof prefix === "function" ? prefix(id) : prefix + (counter++).toString();
  17. const escapedID = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  18. body = body.replace(
  19. // Allowed characters before id: [#;"]
  20. // Allowed characters after id: [)"], .[a-z]
  21. new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', "g"),
  22. "$1" + newID + suffix + "$3"
  23. );
  24. });
  25. body = body.replace(new RegExp(suffix, "g"), "");
  26. return body;
  27. }
  28. exports.replaceIDs = replaceIDs;