parse.cjs 2.8 KB

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