parse.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { getEmojiSequenceFromString, getUnqualifiedEmojiSequence } from '../cleanup.mjs';
  2. import { getEmojiSequenceKeyword } from '../format.mjs';
  3. import '../convert.mjs';
  4. import '../data.mjs';
  5. const componentStatus = "component";
  6. const allowedStatus = /* @__PURE__ */ new Set([
  7. componentStatus,
  8. "fully-qualified",
  9. "minimally-qualified",
  10. "unqualified"
  11. ]);
  12. function getQualifiedTestData(data) {
  13. const results = /* @__PURE__ */ Object.create(null);
  14. for (const key in data) {
  15. const item = data[key];
  16. const sequence = getUnqualifiedEmojiSequence(item.sequence);
  17. const shortKey = getEmojiSequenceKeyword(sequence);
  18. if (!results[shortKey] || results[shortKey].sequence.length < sequence.length) {
  19. results[shortKey] = item;
  20. }
  21. }
  22. return results;
  23. }
  24. function parseEmojiTestFile(data) {
  25. const results = /* @__PURE__ */ Object.create(null);
  26. let group;
  27. let subgroup;
  28. data.split("\n").forEach((line) => {
  29. line = line.trim();
  30. const parts = line.split("#");
  31. if (parts.length < 2) {
  32. return;
  33. }
  34. const firstChunk = parts.shift().trim();
  35. const secondChunk = parts.join("#").trim();
  36. if (!firstChunk) {
  37. const commentParts = secondChunk.split(":");
  38. if (commentParts.length === 2) {
  39. const key2 = commentParts[0].trim();
  40. const value = commentParts[1].trim();
  41. switch (key2) {
  42. case "group":
  43. group = value;
  44. subgroup = void 0;
  45. break;
  46. case "subgroup":
  47. subgroup = value;
  48. break;
  49. }
  50. }
  51. return;
  52. }
  53. if (!group || !subgroup) {
  54. return;
  55. }
  56. const firstChunkParts = firstChunk.split(";");
  57. if (firstChunkParts.length !== 2) {
  58. return;
  59. }
  60. const code = firstChunkParts[0].trim();
  61. if (!code || !code.match(/^[A-F0-9]+[A-F0-9\s]*[A-F0-9]+$/)) {
  62. return;
  63. }
  64. const status = firstChunkParts[1].trim();
  65. if (!allowedStatus.has(status)) {
  66. throw new Error(`Bad emoji type: ${status}`);
  67. }
  68. const secondChunkParts = secondChunk.split(/\s+/);
  69. if (secondChunkParts.length < 3) {
  70. throw new Error(`Bad emoji comment for: ${code}`);
  71. }
  72. const emoji = secondChunkParts.shift();
  73. const version = secondChunkParts.shift();
  74. if (version.slice(0, 1) !== "E") {
  75. throw new Error(`Bad unicode version "${version}" for: ${code}`);
  76. }
  77. const name = secondChunkParts.join(" ");
  78. const sequence = getEmojiSequenceFromString(code);
  79. const key = getEmojiSequenceKeyword(sequence);
  80. if (results[key]) {
  81. throw new Error(`Duplicate entry for "${code}"`);
  82. }
  83. results[key] = {
  84. group,
  85. subgroup,
  86. sequence,
  87. emoji,
  88. status,
  89. version,
  90. name
  91. };
  92. });
  93. return getQualifiedTestData(results);
  94. }
  95. export { componentStatus, parseEmojiTestFile };