minify.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { defaultIconDimensions } from '../icon/defaults.mjs';
  2. function minifyIconSet(data) {
  3. const icons = Object.keys(data.icons);
  4. Object.keys(
  5. defaultIconDimensions
  6. ).forEach((prop) => {
  7. if (data[prop] === defaultIconDimensions[prop]) {
  8. delete data[prop];
  9. }
  10. const defaultValue = defaultIconDimensions[prop];
  11. const propType = typeof defaultValue;
  12. const hasMinifiedDefault = typeof data[prop] === propType && data[prop] !== defaultValue;
  13. let maxCount = 0;
  14. let maxValue = null;
  15. const counters = /* @__PURE__ */ new Map();
  16. for (let i = 0; i < icons.length; i++) {
  17. const item = data.icons[icons[i]];
  18. let value;
  19. if (typeof item[prop] === propType) {
  20. value = item[prop];
  21. } else if (hasMinifiedDefault) {
  22. value = data[prop];
  23. } else {
  24. value = defaultIconDimensions[prop];
  25. }
  26. if (i === 0) {
  27. maxCount = 1;
  28. maxValue = value;
  29. counters.set(value, 1);
  30. continue;
  31. }
  32. if (!counters.has(value)) {
  33. counters.set(value, 1);
  34. continue;
  35. }
  36. const count = counters.get(value) + 1;
  37. counters.set(value, count);
  38. if (count > maxCount) {
  39. maxCount = count;
  40. maxValue = value;
  41. }
  42. }
  43. const canMinify = maxValue !== null && maxCount > 1;
  44. const oldDefault = hasMinifiedDefault ? data[prop] : null;
  45. const newDefault = canMinify ? maxValue : oldDefault;
  46. if (newDefault === defaultValue) {
  47. delete data[prop];
  48. } else if (canMinify) {
  49. data[prop] = newDefault;
  50. }
  51. icons.forEach((key) => {
  52. const item = data.icons[key];
  53. const value = prop in item ? item[prop] : hasMinifiedDefault ? oldDefault : defaultValue;
  54. if (value === newDefault || newDefault === null && value === defaultValue) {
  55. delete item[prop];
  56. return;
  57. }
  58. if (canMinify && !(prop in item)) {
  59. item[prop] = value;
  60. }
  61. });
  62. });
  63. }
  64. export { minifyIconSet };