tree.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { emojiComponents } from '../data.mjs';
  2. function mergeComponentTypes(value) {
  3. return "[" + value.join(",") + "]";
  4. }
  5. function mergeComponentsCount(value) {
  6. const keys = [];
  7. for (const key in emojiComponents) {
  8. const type = key;
  9. for (let i = 0; i < value[type]; i++) {
  10. keys.push(type);
  11. }
  12. }
  13. return keys.length ? mergeComponentTypes(keys) : "";
  14. }
  15. function getGroupItem(items, components) {
  16. const key = mergeComponentsCount(components);
  17. const item = items[key];
  18. if (item) {
  19. item.parsed = true;
  20. return item.item;
  21. }
  22. }
  23. function getEmojiTestDataTree(data) {
  24. const groups = /* @__PURE__ */ Object.create(null);
  25. for (const key in data) {
  26. const item = data[key];
  27. const text = item.name.key;
  28. const parent = groups[text] || (groups[text] = {});
  29. const components = {
  30. "hair-style": 0,
  31. "skin-tone": 0
  32. };
  33. item.sequence.forEach((value) => {
  34. if (typeof value !== "number") {
  35. components[value]++;
  36. }
  37. });
  38. const componentsKey = mergeComponentsCount(components);
  39. if (parent[componentsKey]) {
  40. throw new Error(`Duplicate components tree item for "${text}"`);
  41. }
  42. parent[componentsKey] = {
  43. item: {
  44. ...item,
  45. components,
  46. componentsKey
  47. }
  48. };
  49. }
  50. const results = /* @__PURE__ */ Object.create(null);
  51. for (const key in groups) {
  52. const items = groups[key];
  53. const check = (parent, parentComponents, type) => {
  54. const item = parse(parentComponents, [type]);
  55. if (item) {
  56. const children = parent.children || (parent.children = {});
  57. children[type] = item;
  58. return true;
  59. }
  60. };
  61. const parse = (parentComponents, newComponents) => {
  62. const components = {
  63. "hair-style": 0,
  64. "skin-tone": 0
  65. };
  66. const componentsList = parentComponents.concat(newComponents);
  67. componentsList.forEach((type) => {
  68. components[type]++;
  69. });
  70. let item = getGroupItem(items, components);
  71. if (!item && newComponents.length === 1 && newComponents[0] === "skin-tone") {
  72. const doubleComponents = {
  73. ...components
  74. };
  75. doubleComponents["skin-tone"]++;
  76. item = getGroupItem(items, doubleComponents);
  77. }
  78. if (item) {
  79. const result = {
  80. item
  81. };
  82. for (const key2 in emojiComponents) {
  83. check(result, componentsList, key2);
  84. }
  85. return result;
  86. }
  87. };
  88. const root = parse([], []);
  89. if (!root) {
  90. throw new Error(`Cannot find parent item for "${key}"`);
  91. }
  92. for (const itemsKey in items) {
  93. if (!items[itemsKey].parsed) {
  94. throw new Error(`Error generating tree for "${key}"`);
  95. }
  96. }
  97. if (root.children) {
  98. results[key] = root;
  99. }
  100. }
  101. return results;
  102. }
  103. export { getEmojiTestDataTree };