版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. const icon_name = require('../icon/name.cjs');
  3. const icon_defaults = require('../icon/defaults.cjs');
  4. const iconSet_tree = require('./tree.cjs');
  5. const matchChar = /^[a-f0-9]+(-[a-f0-9]+)*$/;
  6. function validateIconProps(item, fix, checkOtherProps) {
  7. for (const key in item) {
  8. const attr = key;
  9. const value = item[attr];
  10. const type = typeof value;
  11. if (type === "undefined") {
  12. delete item[attr];
  13. continue;
  14. }
  15. const expectedType = typeof icon_defaults.defaultExtendedIconProps[attr];
  16. if (expectedType !== "undefined") {
  17. if (type !== expectedType) {
  18. if (fix) {
  19. delete item[attr];
  20. continue;
  21. }
  22. return attr;
  23. }
  24. continue;
  25. }
  26. if (checkOtherProps && type === "object") {
  27. if (fix) {
  28. delete item[attr];
  29. } else {
  30. return key;
  31. }
  32. }
  33. }
  34. return null;
  35. }
  36. function validateIconSet(obj, options) {
  37. const fix = !!(options && options.fix);
  38. if (typeof obj !== "object" || obj === null || typeof obj.icons !== "object" || !obj.icons) {
  39. throw new Error("Bad icon set");
  40. }
  41. const data = obj;
  42. if (options && typeof options.prefix === "string") {
  43. data.prefix = options.prefix;
  44. } else if (typeof data.prefix !== "string" || !data.prefix.match(icon_name.matchIconName)) {
  45. throw new Error("Invalid prefix");
  46. }
  47. if (options && typeof options.provider === "string") {
  48. data.provider = options.provider;
  49. } else if (data.provider !== void 0) {
  50. const value = data.provider;
  51. if (typeof value !== "string" || value !== "" && !value.match(icon_name.matchIconName)) {
  52. if (fix) {
  53. delete data.provider;
  54. } else {
  55. throw new Error("Invalid provider");
  56. }
  57. }
  58. }
  59. if (data.aliases !== void 0) {
  60. if (typeof data.aliases !== "object" || data.aliases === null) {
  61. if (fix) {
  62. delete data.aliases;
  63. } else {
  64. throw new Error("Invalid aliases list");
  65. }
  66. }
  67. }
  68. const tree = iconSet_tree.getIconsTree(data);
  69. const icons = data.icons;
  70. const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
  71. for (const name in tree) {
  72. const treeItem = tree[name];
  73. const isAlias = !icons[name];
  74. const parentObj = isAlias ? aliases : icons;
  75. if (!treeItem) {
  76. if (fix) {
  77. delete parentObj[name];
  78. continue;
  79. }
  80. throw new Error(`Invalid alias: ${name}`);
  81. }
  82. if (!name.match(icon_name.matchIconName)) {
  83. if (fix) {
  84. delete parentObj[name];
  85. continue;
  86. }
  87. throw new Error(`Invalid icon name: "${name}"`);
  88. }
  89. const item = parentObj[name];
  90. if (!isAlias) {
  91. if (typeof item.body !== "string") {
  92. if (fix) {
  93. delete parentObj[name];
  94. continue;
  95. }
  96. throw new Error(`Invalid icon: "${name}"`);
  97. }
  98. }
  99. const requiredProp = isAlias ? "parent" : "body";
  100. const key = typeof item[requiredProp] !== "string" ? requiredProp : validateIconProps(item, fix, true);
  101. if (key !== null) {
  102. throw new Error(`Invalid property "${key}" in "${name}"`);
  103. }
  104. }
  105. if (data.not_found !== void 0 && !(data.not_found instanceof Array)) {
  106. if (fix) {
  107. delete data.not_found;
  108. } else {
  109. throw new Error("Invalid not_found list");
  110. }
  111. }
  112. if (!Object.keys(data.icons).length && !(data.not_found && data.not_found.length)) {
  113. throw new Error("Icon set is empty");
  114. }
  115. if (fix && !Object.keys(aliases).length) {
  116. delete data.aliases;
  117. }
  118. const failedOptionalProp = validateIconProps(data, false, false);
  119. if (failedOptionalProp) {
  120. throw new Error(`Invalid value type for "${failedOptionalProp}"`);
  121. }
  122. if (data.chars !== void 0) {
  123. if (typeof data.chars !== "object" || data.chars === null) {
  124. if (fix) {
  125. delete data.chars;
  126. } else {
  127. throw new Error("Invalid characters map");
  128. }
  129. }
  130. }
  131. if (typeof data.chars === "object") {
  132. const chars = data.chars;
  133. Object.keys(chars).forEach((char) => {
  134. if (!matchChar.exec(char) || typeof chars[char] !== "string") {
  135. if (fix) {
  136. delete chars[char];
  137. return;
  138. }
  139. throw new Error(`Invalid character "${char}"`);
  140. }
  141. const target = chars[char];
  142. if (!data.icons[target] && (!data.aliases || !data.aliases[target])) {
  143. if (fix) {
  144. delete chars[char];
  145. return;
  146. }
  147. throw new Error(
  148. `Character "${char}" points to missing icon "${target}"`
  149. );
  150. }
  151. });
  152. if (fix && !Object.keys(data.chars).length) {
  153. delete data.chars;
  154. }
  155. }
  156. return data;
  157. }
  158. exports.matchChar = matchChar;
  159. exports.validateIconSet = validateIconSet;