createNode.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. var Alias = require('../nodes/Alias.js');
  3. var identity = require('../nodes/identity.js');
  4. var Scalar = require('../nodes/Scalar.js');
  5. const defaultTagPrefix = 'tag:yaml.org,2002:';
  6. function findTagObject(value, tagName, tags) {
  7. if (tagName) {
  8. const match = tags.filter(t => t.tag === tagName);
  9. const tagObj = match.find(t => !t.format) ?? match[0];
  10. if (!tagObj)
  11. throw new Error(`Tag ${tagName} not found`);
  12. return tagObj;
  13. }
  14. return tags.find(t => t.identify?.(value) && !t.format);
  15. }
  16. function createNode(value, tagName, ctx) {
  17. if (identity.isDocument(value))
  18. value = value.contents;
  19. if (identity.isNode(value))
  20. return value;
  21. if (identity.isPair(value)) {
  22. const map = ctx.schema[identity.MAP].createNode?.(ctx.schema, null, ctx);
  23. map.items.push(value);
  24. return map;
  25. }
  26. if (value instanceof String ||
  27. value instanceof Number ||
  28. value instanceof Boolean ||
  29. (typeof BigInt !== 'undefined' && value instanceof BigInt) // not supported everywhere
  30. ) {
  31. // https://tc39.es/ecma262/#sec-serializejsonproperty
  32. value = value.valueOf();
  33. }
  34. const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
  35. // Detect duplicate references to the same object & use Alias nodes for all
  36. // after first. The `ref` wrapper allows for circular references to resolve.
  37. let ref = undefined;
  38. if (aliasDuplicateObjects && value && typeof value === 'object') {
  39. ref = sourceObjects.get(value);
  40. if (ref) {
  41. if (!ref.anchor)
  42. ref.anchor = onAnchor(value);
  43. return new Alias.Alias(ref.anchor);
  44. }
  45. else {
  46. ref = { anchor: null, node: null };
  47. sourceObjects.set(value, ref);
  48. }
  49. }
  50. if (tagName?.startsWith('!!'))
  51. tagName = defaultTagPrefix + tagName.slice(2);
  52. let tagObj = findTagObject(value, tagName, schema.tags);
  53. if (!tagObj) {
  54. if (value && typeof value.toJSON === 'function') {
  55. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  56. value = value.toJSON();
  57. }
  58. if (!value || typeof value !== 'object') {
  59. const node = new Scalar.Scalar(value);
  60. if (ref)
  61. ref.node = node;
  62. return node;
  63. }
  64. tagObj =
  65. value instanceof Map
  66. ? schema[identity.MAP]
  67. : Symbol.iterator in Object(value)
  68. ? schema[identity.SEQ]
  69. : schema[identity.MAP];
  70. }
  71. if (onTagObj) {
  72. onTagObj(tagObj);
  73. delete ctx.onTagObj;
  74. }
  75. const node = tagObj?.createNode
  76. ? tagObj.createNode(ctx.schema, value, ctx)
  77. : typeof tagObj?.nodeClass?.from === 'function'
  78. ? tagObj.nodeClass.from(ctx.schema, value, ctx)
  79. : new Scalar.Scalar(value);
  80. if (tagName)
  81. node.tag = tagName;
  82. else if (!tagObj.default)
  83. node.tag = tagObj.tag;
  84. if (ref)
  85. ref.node = node;
  86. return node;
  87. }
  88. exports.createNode = createNode;