1 | import { Alias } from '../nodes/Alias.js';
|
---|
2 | import { isNode, isPair, MAP, SEQ, isDocument } from '../nodes/identity.js';
|
---|
3 | import { Scalar } from '../nodes/Scalar.js';
|
---|
4 |
|
---|
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 (isDocument(value))
|
---|
18 | value = value.contents;
|
---|
19 | if (isNode(value))
|
---|
20 | return value;
|
---|
21 | if (isPair(value)) {
|
---|
22 | const map = ctx.schema[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(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(value);
|
---|
60 | if (ref)
|
---|
61 | ref.node = node;
|
---|
62 | return node;
|
---|
63 | }
|
---|
64 | tagObj =
|
---|
65 | value instanceof Map
|
---|
66 | ? schema[MAP]
|
---|
67 | : Symbol.iterator in Object(value)
|
---|
68 | ? schema[SEQ]
|
---|
69 | : schema[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(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 |
|
---|
89 | export { createNode };
|
---|