util.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setEvaluated = exports.evaluatedPropsToName = exports.mergeEvaluated = exports.eachItem = exports.unescapeJsonPointer = exports.escapeJsonPointer = exports.escapeFragment = exports.unescapeFragment = exports.schemaRefOrVal = exports.schemaHasRulesButRef = exports.schemaHasRules = exports.checkUnknownRules = exports.alwaysValidSchema = exports.toHash = void 0;
  4. const codegen_1 = require("./codegen");
  5. const validate_1 = require("./validate");
  6. // TODO refactor to use Set
  7. function toHash(arr) {
  8. const hash = {};
  9. for (const item of arr)
  10. hash[item] = true;
  11. return hash;
  12. }
  13. exports.toHash = toHash;
  14. function alwaysValidSchema(it, schema) {
  15. if (typeof schema == "boolean")
  16. return schema;
  17. if (Object.keys(schema).length === 0)
  18. return true;
  19. checkUnknownRules(it, schema);
  20. return !schemaHasRules(schema, it.self.RULES.all);
  21. }
  22. exports.alwaysValidSchema = alwaysValidSchema;
  23. function checkUnknownRules(it, schema = it.schema) {
  24. const { opts, self } = it;
  25. if (!opts.strict)
  26. return;
  27. if (typeof schema === "boolean")
  28. return;
  29. const rules = self.RULES.keywords;
  30. for (const key in schema) {
  31. if (!rules[key])
  32. validate_1.checkStrictMode(it, `unknown keyword: "${key}"`);
  33. }
  34. }
  35. exports.checkUnknownRules = checkUnknownRules;
  36. function schemaHasRules(schema, rules) {
  37. if (typeof schema == "boolean")
  38. return !schema;
  39. for (const key in schema)
  40. if (rules[key])
  41. return true;
  42. return false;
  43. }
  44. exports.schemaHasRules = schemaHasRules;
  45. function schemaHasRulesButRef(schema, RULES) {
  46. if (typeof schema == "boolean")
  47. return !schema;
  48. for (const key in schema)
  49. if (key !== "$ref" && RULES.all[key])
  50. return true;
  51. return false;
  52. }
  53. exports.schemaHasRulesButRef = schemaHasRulesButRef;
  54. function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
  55. if (!$data) {
  56. if (typeof schema == "number" || typeof schema == "boolean")
  57. return schema;
  58. if (typeof schema == "string")
  59. return codegen_1._ `${schema}`;
  60. }
  61. return codegen_1._ `${topSchemaRef}${schemaPath}${codegen_1.getProperty(keyword)}`;
  62. }
  63. exports.schemaRefOrVal = schemaRefOrVal;
  64. function unescapeFragment(str) {
  65. return unescapeJsonPointer(decodeURIComponent(str));
  66. }
  67. exports.unescapeFragment = unescapeFragment;
  68. function escapeFragment(str) {
  69. return encodeURIComponent(escapeJsonPointer(str));
  70. }
  71. exports.escapeFragment = escapeFragment;
  72. function escapeJsonPointer(str) {
  73. if (typeof str == "number")
  74. return `${str}`;
  75. return str.replace(/~/g, "~0").replace(/\//g, "~1");
  76. }
  77. exports.escapeJsonPointer = escapeJsonPointer;
  78. function unescapeJsonPointer(str) {
  79. return str.replace(/~1/g, "/").replace(/~0/g, "~");
  80. }
  81. exports.unescapeJsonPointer = unescapeJsonPointer;
  82. function eachItem(xs, f) {
  83. if (Array.isArray(xs)) {
  84. for (const x of xs)
  85. f(x);
  86. }
  87. else {
  88. f(xs);
  89. }
  90. }
  91. exports.eachItem = eachItem;
  92. function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues, resultToName, }) {
  93. return (gen, from, to, toName) => {
  94. const res = to === undefined
  95. ? from
  96. : to instanceof codegen_1.Name
  97. ? (from instanceof codegen_1.Name ? mergeNames(gen, from, to) : mergeToName(gen, from, to), to)
  98. : from instanceof codegen_1.Name
  99. ? (mergeToName(gen, to, from), from)
  100. : mergeValues(from, to);
  101. return toName === codegen_1.Name && !(res instanceof codegen_1.Name) ? resultToName(gen, res) : res;
  102. };
  103. }
  104. exports.mergeEvaluated = {
  105. props: makeMergeEvaluated({
  106. mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => {
  107. gen.if(codegen_1._ `${from} === true`, () => gen.assign(to, true), () => gen.code(codegen_1._ `Object.assign(${to}, ${from})`));
  108. }),
  109. mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => {
  110. if (from === true) {
  111. gen.assign(to, true);
  112. }
  113. else {
  114. gen.assign(to, codegen_1._ `${to} || {}`);
  115. setEvaluated(gen, to, from);
  116. }
  117. }),
  118. mergeValues: (from, to) => (from === true ? true : { ...from, ...to }),
  119. resultToName: evaluatedPropsToName,
  120. }),
  121. items: makeMergeEvaluated({
  122. mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => gen.assign(to, codegen_1._ `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
  123. mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => gen.assign(to, from === true ? true : codegen_1._ `${to} > ${from} ? ${to} : ${from}`)),
  124. mergeValues: (from, to) => (from === true ? true : Math.max(from, to)),
  125. resultToName: (gen, items) => gen.var("items", items),
  126. }),
  127. };
  128. function evaluatedPropsToName(gen, ps) {
  129. if (ps === true)
  130. return gen.var("props", true);
  131. const props = gen.var("props", codegen_1._ `{}`);
  132. if (ps !== undefined)
  133. setEvaluated(gen, props, ps);
  134. return props;
  135. }
  136. exports.evaluatedPropsToName = evaluatedPropsToName;
  137. function setEvaluated(gen, props, ps) {
  138. Object.keys(ps).forEach((p) => gen.assign(codegen_1._ `${props}${codegen_1.getProperty(p)}`, true));
  139. }
  140. exports.setEvaluated = setEvaluated;
  141. //# sourceMappingURL=util.js.map