yaml 2.0.0-3 → 2.0.0-7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +46 -26
- package/browser/dist/compose/compose-collection.js +59 -0
- package/browser/dist/compose/compose-doc.js +39 -0
- package/browser/dist/compose/compose-node.js +79 -0
- package/browser/dist/compose/compose-scalar.js +74 -0
- package/browser/dist/compose/composer.js +219 -0
- package/browser/dist/compose/resolve-block-map.js +93 -0
- package/browser/dist/compose/resolve-block-scalar.js +185 -0
- package/browser/dist/compose/resolve-block-seq.js +40 -0
- package/browser/dist/compose/resolve-end.js +37 -0
- package/browser/dist/compose/resolve-flow-collection.js +189 -0
- package/browser/dist/compose/resolve-flow-scalar.js +217 -0
- package/browser/dist/compose/resolve-props.js +126 -0
- package/browser/dist/compose/util-contains-newline.js +34 -0
- package/browser/dist/compose/util-empty-scalar-position.js +27 -0
- package/browser/dist/compose/util-map-includes.js +17 -0
- package/browser/dist/doc/Document.js +251 -348
- package/browser/dist/doc/anchors.js +72 -0
- package/browser/dist/doc/applyReviver.js +41 -28
- package/browser/dist/doc/createNode.js +75 -55
- package/browser/dist/doc/directives.js +161 -0
- package/browser/dist/errors.js +50 -72
- package/browser/dist/index.js +16 -72
- package/browser/dist/log.js +11 -7
- package/browser/dist/nodes/Alias.js +94 -0
- package/browser/dist/nodes/Collection.js +133 -0
- package/browser/dist/nodes/Node.js +41 -0
- package/browser/dist/nodes/Pair.js +28 -0
- package/browser/dist/nodes/Scalar.js +23 -0
- package/browser/dist/nodes/YAMLMap.js +116 -0
- package/browser/dist/nodes/YAMLSeq.js +105 -0
- package/browser/dist/nodes/addPairToJSMap.js +103 -0
- package/browser/dist/nodes/toJS.js +35 -0
- package/browser/dist/options.js +14 -95
- package/browser/dist/parse/cst-scalar.js +219 -0
- package/browser/dist/parse/cst-stringify.js +61 -0
- package/browser/dist/parse/cst-visit.js +97 -0
- package/browser/dist/parse/cst.js +98 -0
- package/browser/dist/parse/lexer.js +674 -0
- package/browser/dist/parse/line-counter.js +39 -0
- package/browser/dist/parse/parser.js +876 -0
- package/browser/dist/public-api.js +101 -0
- package/browser/dist/schema/Schema.js +23 -0
- package/browser/dist/schema/common/map.js +42 -0
- package/browser/dist/schema/common/null.js +13 -0
- package/browser/dist/schema/common/seq.js +33 -0
- package/browser/dist/schema/common/string.js +14 -0
- package/browser/dist/schema/core/bool.js +19 -0
- package/browser/dist/schema/core/float.js +40 -0
- package/browser/dist/schema/core/int.js +38 -0
- package/browser/dist/schema/core/schema.js +23 -0
- package/browser/dist/schema/json/schema.js +62 -0
- package/browser/dist/schema/tags.js +79 -0
- package/browser/dist/schema/yaml-1.1/binary.js +66 -0
- package/browser/dist/schema/yaml-1.1/bool.js +26 -0
- package/browser/dist/schema/yaml-1.1/float.js +43 -0
- package/browser/dist/schema/yaml-1.1/int.js +71 -0
- package/browser/dist/schema/yaml-1.1/omap.js +73 -0
- package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
- package/browser/dist/schema/yaml-1.1/schema.js +37 -0
- package/browser/dist/schema/yaml-1.1/set.js +87 -0
- package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
- package/browser/dist/stringify/foldFlowLines.js +121 -139
- package/browser/dist/stringify/stringify.js +87 -72
- package/browser/dist/stringify/stringifyCollection.js +122 -0
- package/browser/dist/stringify/stringifyComment.js +14 -0
- package/browser/dist/stringify/stringifyDocument.js +64 -0
- package/browser/dist/stringify/stringifyNumber.js +20 -23
- package/browser/dist/stringify/stringifyPair.js +111 -0
- package/browser/dist/stringify/stringifyString.js +295 -322
- package/browser/dist/util.js +4 -4
- package/browser/dist/visit.js +134 -71
- package/browser/index.js +4 -1
- package/dist/compose/compose-collection.d.ts +5 -0
- package/dist/compose/compose-collection.js +61 -0
- package/dist/compose/compose-doc.d.ts +6 -0
- package/dist/compose/compose-doc.js +41 -0
- package/dist/compose/compose-node.d.ts +25 -0
- package/dist/compose/compose-node.js +82 -0
- package/dist/compose/compose-scalar.d.ts +5 -0
- package/dist/compose/compose-scalar.js +76 -0
- package/dist/compose/composer.d.ts +62 -0
- package/dist/compose/composer.js +223 -0
- package/dist/compose/resolve-block-map.d.ts +6 -0
- package/dist/compose/resolve-block-map.js +95 -0
- package/dist/compose/resolve-block-scalar.d.ts +10 -0
- package/dist/compose/resolve-block-scalar.js +187 -0
- package/dist/compose/resolve-block-seq.d.ts +5 -0
- package/dist/compose/resolve-block-seq.js +42 -0
- package/dist/compose/resolve-end.d.ts +6 -0
- package/dist/compose/resolve-end.js +39 -0
- package/dist/compose/resolve-flow-collection.d.ts +6 -0
- package/dist/compose/resolve-flow-collection.js +191 -0
- package/dist/compose/resolve-flow-scalar.d.ts +10 -0
- package/dist/compose/resolve-flow-scalar.js +219 -0
- package/dist/compose/resolve-props.d.ts +21 -0
- package/dist/compose/resolve-props.js +128 -0
- package/dist/compose/util-contains-newline.d.ts +2 -0
- package/dist/compose/util-contains-newline.js +36 -0
- package/dist/compose/util-empty-scalar-position.d.ts +2 -0
- package/dist/compose/util-empty-scalar-position.js +29 -0
- package/dist/compose/util-map-includes.d.ts +4 -0
- package/dist/compose/util-map-includes.js +19 -0
- package/dist/doc/Document.d.ts +130 -0
- package/dist/doc/Document.js +250 -347
- package/dist/doc/anchors.d.ts +24 -0
- package/dist/doc/anchors.js +77 -0
- package/dist/doc/applyReviver.d.ts +9 -0
- package/dist/doc/applyReviver.js +41 -28
- package/dist/doc/createNode.d.ts +16 -0
- package/dist/doc/createNode.js +75 -55
- package/dist/doc/directives.d.ts +46 -0
- package/dist/doc/directives.js +163 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +51 -74
- package/dist/index.d.ts +19 -0
- package/dist/index.js +39 -70
- package/dist/log.d.ts +3 -0
- package/dist/log.js +11 -6
- package/dist/nodes/Alias.d.ts +24 -0
- package/dist/nodes/Alias.js +96 -0
- package/dist/nodes/Collection.d.ts +67 -0
- package/dist/nodes/Collection.js +137 -0
- package/dist/nodes/Node.d.ts +48 -0
- package/dist/nodes/Node.js +59 -0
- package/dist/nodes/Pair.d.ts +16 -0
- package/dist/nodes/Pair.js +31 -0
- package/dist/nodes/Scalar.d.ts +40 -0
- package/dist/nodes/Scalar.js +26 -0
- package/dist/nodes/YAMLMap.d.ts +39 -0
- package/dist/nodes/YAMLMap.js +119 -0
- package/dist/nodes/YAMLSeq.d.ts +53 -0
- package/dist/nodes/YAMLSeq.js +107 -0
- package/dist/nodes/addPairToJSMap.d.ts +3 -0
- package/dist/nodes/addPairToJSMap.js +105 -0
- package/dist/nodes/toJS.d.ts +30 -0
- package/dist/nodes/toJS.js +37 -0
- package/dist/options.d.ts +278 -0
- package/dist/options.js +13 -96
- package/dist/parse/cst-scalar.d.ts +58 -0
- package/dist/parse/cst-scalar.js +223 -0
- package/dist/parse/cst-stringify.d.ts +8 -0
- package/dist/parse/cst-stringify.js +63 -0
- package/dist/parse/cst-visit.d.ts +39 -0
- package/dist/parse/cst-visit.js +99 -0
- package/dist/parse/cst.d.ts +106 -0
- package/dist/parse/cst.js +112 -0
- package/dist/parse/lexer.d.ts +85 -0
- package/dist/parse/lexer.js +676 -0
- package/dist/parse/line-counter.d.ts +22 -0
- package/dist/parse/line-counter.js +41 -0
- package/dist/parse/parser.d.ts +84 -0
- package/dist/parse/parser.js +880 -0
- package/dist/public-api.d.ts +43 -0
- package/dist/public-api.js +106 -0
- package/dist/schema/Schema.d.ts +16 -0
- package/dist/schema/Schema.js +25 -0
- package/dist/schema/common/map.d.ts +2 -0
- package/dist/schema/common/map.js +44 -0
- package/dist/schema/common/null.d.ts +4 -0
- package/dist/schema/common/null.js +15 -0
- package/dist/schema/common/seq.d.ts +2 -0
- package/dist/schema/common/seq.js +35 -0
- package/dist/schema/common/string.d.ts +2 -0
- package/dist/schema/common/string.js +16 -0
- package/dist/schema/core/bool.d.ts +4 -0
- package/dist/schema/core/bool.js +21 -0
- package/dist/schema/core/float.d.ts +4 -0
- package/dist/schema/core/float.js +44 -0
- package/dist/schema/core/int.d.ts +4 -0
- package/dist/schema/core/int.js +42 -0
- package/dist/schema/core/schema.d.ts +1 -0
- package/dist/schema/core/schema.js +25 -0
- package/dist/schema/json/schema.d.ts +2 -0
- package/dist/schema/json/schema.js +64 -0
- package/dist/schema/tags.d.ts +41 -0
- package/dist/schema/tags.js +82 -0
- package/dist/schema/types.d.ts +82 -0
- package/dist/schema/yaml-1.1/binary.d.ts +2 -0
- package/dist/schema/yaml-1.1/binary.js +68 -0
- package/dist/schema/yaml-1.1/bool.d.ts +7 -0
- package/dist/schema/yaml-1.1/bool.js +29 -0
- package/dist/schema/yaml-1.1/float.d.ts +4 -0
- package/dist/schema/yaml-1.1/float.js +47 -0
- package/dist/schema/yaml-1.1/int.d.ts +5 -0
- package/dist/schema/yaml-1.1/int.js +76 -0
- package/dist/schema/yaml-1.1/omap.d.ts +21 -0
- package/dist/schema/yaml-1.1/omap.js +76 -0
- package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
- package/dist/schema/yaml-1.1/pairs.js +81 -0
- package/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/dist/schema/yaml-1.1/schema.js +39 -0
- package/dist/schema/yaml-1.1/set.d.ts +22 -0
- package/dist/schema/yaml-1.1/set.js +90 -0
- package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
- package/dist/schema/yaml-1.1/timestamp.js +105 -0
- package/dist/stringify/foldFlowLines.d.ts +34 -0
- package/dist/stringify/foldFlowLines.js +121 -139
- package/dist/stringify/stringify.d.ts +18 -0
- package/dist/stringify/stringify.js +87 -71
- package/dist/stringify/stringifyCollection.d.ts +21 -0
- package/dist/stringify/stringifyCollection.js +124 -0
- package/dist/stringify/stringifyComment.d.ts +2 -0
- package/dist/stringify/stringifyComment.js +17 -0
- package/dist/stringify/stringifyDocument.d.ts +3 -0
- package/dist/stringify/stringifyDocument.js +66 -0
- package/dist/stringify/stringifyNumber.d.ts +2 -0
- package/dist/stringify/stringifyNumber.js +20 -23
- package/dist/stringify/stringifyPair.d.ts +3 -0
- package/dist/stringify/stringifyPair.js +113 -0
- package/dist/stringify/stringifyString.d.ts +3 -0
- package/dist/stringify/stringifyString.js +295 -322
- package/dist/test-events.d.ts +4 -0
- package/dist/test-events.js +124 -148
- package/dist/util.d.ts +6 -0
- package/dist/util.js +9 -12
- package/dist/visit.d.ts +63 -0
- package/dist/visit.js +134 -71
- package/package.json +31 -39
- package/util.js +2 -14
- package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
- package/browser/dist/ast/Alias.js +0 -95
- package/browser/dist/ast/Collection.js +0 -192
- package/browser/dist/ast/Merge.js +0 -71
- package/browser/dist/ast/Node.js +0 -3
- package/browser/dist/ast/Pair.js +0 -200
- package/browser/dist/ast/Scalar.js +0 -21
- package/browser/dist/ast/YAMLMap.js +0 -91
- package/browser/dist/ast/YAMLSeq.js +0 -67
- package/browser/dist/ast/toJS.js +0 -29
- package/browser/dist/constants.js +0 -40
- package/browser/dist/cst/Alias.js +0 -26
- package/browser/dist/cst/BlankLine.js +0 -34
- package/browser/dist/cst/BlockValue.js +0 -240
- package/browser/dist/cst/Collection.js +0 -259
- package/browser/dist/cst/CollectionItem.js +0 -123
- package/browser/dist/cst/Comment.js +0 -27
- package/browser/dist/cst/Directive.js +0 -53
- package/browser/dist/cst/Document.js +0 -262
- package/browser/dist/cst/FlowCollection.js +0 -201
- package/browser/dist/cst/Node.js +0 -355
- package/browser/dist/cst/ParseContext.js +0 -234
- package/browser/dist/cst/PlainValue.js +0 -171
- package/browser/dist/cst/QuoteDouble.js +0 -224
- package/browser/dist/cst/QuoteSingle.js +0 -100
- package/browser/dist/cst/Range.js +0 -56
- package/browser/dist/cst/parse.js +0 -46
- package/browser/dist/cst/source-utils.js +0 -169
- package/browser/dist/doc/Anchors.js +0 -115
- package/browser/dist/doc/Schema.js +0 -34
- package/browser/dist/doc/getSchemaTags.js +0 -33
- package/browser/dist/doc/listTagNames.js +0 -27
- package/browser/dist/doc/parseContents.js +0 -60
- package/browser/dist/doc/parseDirectives.js +0 -107
- package/browser/dist/resolve/collection-utils.js +0 -86
- package/browser/dist/resolve/resolveMap.js +0 -315
- package/browser/dist/resolve/resolveNode.js +0 -173
- package/browser/dist/resolve/resolveScalar.js +0 -20
- package/browser/dist/resolve/resolveSeq.js +0 -170
- package/browser/dist/resolve/resolveTag.js +0 -107
- package/browser/dist/resolve/resolveTagName.js +0 -87
- package/browser/dist/stringify/addComment.js +0 -10
- package/browser/dist/stringify/stringifyTag.js +0 -28
- package/browser/dist/tags/core.js +0 -128
- package/browser/dist/tags/failsafe/index.js +0 -7
- package/browser/dist/tags/failsafe/map.js +0 -37
- package/browser/dist/tags/failsafe/seq.js +0 -34
- package/browser/dist/tags/failsafe/string.js +0 -20
- package/browser/dist/tags/index.js +0 -39
- package/browser/dist/tags/json.js +0 -62
- package/browser/dist/tags/options.js +0 -31
- package/browser/dist/tags/yaml-1.1/binary.js +0 -83
- package/browser/dist/tags/yaml-1.1/index.js +0 -174
- package/browser/dist/tags/yaml-1.1/omap.js +0 -86
- package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
- package/browser/dist/tags/yaml-1.1/set.js +0 -78
- package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
- package/browser/dist/types.js +0 -10
- package/cst.d.ts +0 -185
- package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
- package/dist/ast/Alias.js +0 -97
- package/dist/ast/Collection.js +0 -196
- package/dist/ast/Merge.js +0 -74
- package/dist/ast/Node.js +0 -5
- package/dist/ast/Pair.js +0 -203
- package/dist/ast/Scalar.js +0 -24
- package/dist/ast/YAMLMap.js +0 -94
- package/dist/ast/YAMLSeq.js +0 -69
- package/dist/ast/toJS.js +0 -31
- package/dist/constants.js +0 -46
- package/dist/cst/Alias.js +0 -28
- package/dist/cst/BlankLine.js +0 -36
- package/dist/cst/BlockValue.js +0 -243
- package/dist/cst/Collection.js +0 -262
- package/dist/cst/CollectionItem.js +0 -125
- package/dist/cst/Comment.js +0 -29
- package/dist/cst/Directive.js +0 -55
- package/dist/cst/Document.js +0 -264
- package/dist/cst/FlowCollection.js +0 -203
- package/dist/cst/Node.js +0 -357
- package/dist/cst/ParseContext.js +0 -236
- package/dist/cst/PlainValue.js +0 -173
- package/dist/cst/QuoteDouble.js +0 -226
- package/dist/cst/QuoteSingle.js +0 -102
- package/dist/cst/Range.js +0 -58
- package/dist/cst/parse.js +0 -48
- package/dist/cst/source-utils.js +0 -173
- package/dist/doc/Anchors.js +0 -117
- package/dist/doc/Schema.js +0 -36
- package/dist/doc/getSchemaTags.js +0 -35
- package/dist/doc/listTagNames.js +0 -29
- package/dist/doc/parseContents.js +0 -62
- package/dist/doc/parseDirectives.js +0 -109
- package/dist/resolve/collection-utils.js +0 -91
- package/dist/resolve/resolveMap.js +0 -317
- package/dist/resolve/resolveNode.js +0 -175
- package/dist/resolve/resolveScalar.js +0 -22
- package/dist/resolve/resolveSeq.js +0 -172
- package/dist/resolve/resolveTag.js +0 -109
- package/dist/resolve/resolveTagName.js +0 -89
- package/dist/stringify/addComment.js +0 -13
- package/dist/stringify/stringifyTag.js +0 -30
- package/dist/tags/core.js +0 -138
- package/dist/tags/failsafe/index.js +0 -9
- package/dist/tags/failsafe/map.js +0 -39
- package/dist/tags/failsafe/seq.js +0 -36
- package/dist/tags/failsafe/string.js +0 -22
- package/dist/tags/index.js +0 -42
- package/dist/tags/json.js +0 -64
- package/dist/tags/options.js +0 -37
- package/dist/tags/yaml-1.1/binary.js +0 -85
- package/dist/tags/yaml-1.1/index.js +0 -176
- package/dist/tags/yaml-1.1/omap.js +0 -89
- package/dist/tags/yaml-1.1/pairs.js +0 -66
- package/dist/tags/yaml-1.1/set.js +0 -81
- package/dist/tags/yaml-1.1/timestamp.js +0 -105
- package/dist/types.js +0 -29
- package/index.d.ts +0 -519
- package/index.js +0 -1
- package/types.d.ts +0 -379
- package/types.js +0 -17
- package/types.mjs +0 -17
- package/util.d.ts +0 -76
- package/util.mjs +0 -15
package/browser/dist/log.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
function debug(logLevel, ...messages) {
|
|
2
|
+
if (logLevel === 'debug')
|
|
3
|
+
console.log(...messages);
|
|
4
|
+
}
|
|
4
5
|
function warn(logLevel, warning) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
if (logLevel === 'debug' || logLevel === 'warn') {
|
|
7
|
+
if (typeof process !== 'undefined' && process.emitWarning)
|
|
8
|
+
process.emitWarning(warning);
|
|
9
|
+
else
|
|
10
|
+
console.warn(warning);
|
|
11
|
+
}
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
export { warn };
|
|
14
|
+
export { debug, warn };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { anchorIsValid } from '../doc/anchors.js';
|
|
2
|
+
import { visit } from '../visit.js';
|
|
3
|
+
import { NodeBase, ALIAS, isAlias, isCollection, isPair } from './Node.js';
|
|
4
|
+
|
|
5
|
+
class Alias extends NodeBase {
|
|
6
|
+
constructor(source) {
|
|
7
|
+
super(ALIAS);
|
|
8
|
+
this.source = source;
|
|
9
|
+
Object.defineProperty(this, 'tag', {
|
|
10
|
+
set() {
|
|
11
|
+
throw new Error('Alias nodes cannot have tags');
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the value of this alias within `doc`, finding the last
|
|
17
|
+
* instance of the `source` anchor before this node.
|
|
18
|
+
*/
|
|
19
|
+
resolve(doc) {
|
|
20
|
+
let found = undefined;
|
|
21
|
+
visit(doc, {
|
|
22
|
+
Node: (_key, node) => {
|
|
23
|
+
if (node === this)
|
|
24
|
+
return visit.BREAK;
|
|
25
|
+
if (node.anchor === this.source)
|
|
26
|
+
found = node;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
toJSON(_arg, ctx) {
|
|
32
|
+
if (!ctx)
|
|
33
|
+
return { source: this.source };
|
|
34
|
+
const { anchors, doc, maxAliasCount } = ctx;
|
|
35
|
+
const source = this.resolve(doc);
|
|
36
|
+
if (!source) {
|
|
37
|
+
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
|
|
38
|
+
throw new ReferenceError(msg);
|
|
39
|
+
}
|
|
40
|
+
const data = anchors.get(source);
|
|
41
|
+
/* istanbul ignore if */
|
|
42
|
+
if (!data || data.res === undefined) {
|
|
43
|
+
const msg = 'This should not happen: Alias anchor was not resolved?';
|
|
44
|
+
throw new ReferenceError(msg);
|
|
45
|
+
}
|
|
46
|
+
if (maxAliasCount >= 0) {
|
|
47
|
+
data.count += 1;
|
|
48
|
+
if (data.aliasCount === 0)
|
|
49
|
+
data.aliasCount = getAliasCount(doc, source, anchors);
|
|
50
|
+
if (data.count * data.aliasCount > maxAliasCount) {
|
|
51
|
+
const msg = 'Excessive alias count indicates a resource exhaustion attack';
|
|
52
|
+
throw new ReferenceError(msg);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return data.res;
|
|
56
|
+
}
|
|
57
|
+
toString(ctx, _onComment, _onChompKeep) {
|
|
58
|
+
const src = `*${this.source}`;
|
|
59
|
+
if (ctx) {
|
|
60
|
+
anchorIsValid(this.source);
|
|
61
|
+
if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
|
|
62
|
+
const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
|
|
63
|
+
throw new Error(msg);
|
|
64
|
+
}
|
|
65
|
+
if (ctx.implicitKey)
|
|
66
|
+
return `${src} `;
|
|
67
|
+
}
|
|
68
|
+
return src;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function getAliasCount(doc, node, anchors) {
|
|
72
|
+
if (isAlias(node)) {
|
|
73
|
+
const source = node.resolve(doc);
|
|
74
|
+
const anchor = anchors && source && anchors.get(source);
|
|
75
|
+
return anchor ? anchor.count * anchor.aliasCount : 0;
|
|
76
|
+
}
|
|
77
|
+
else if (isCollection(node)) {
|
|
78
|
+
let count = 0;
|
|
79
|
+
for (const item of node.items) {
|
|
80
|
+
const c = getAliasCount(doc, item, anchors);
|
|
81
|
+
if (c > count)
|
|
82
|
+
count = c;
|
|
83
|
+
}
|
|
84
|
+
return count;
|
|
85
|
+
}
|
|
86
|
+
else if (isPair(node)) {
|
|
87
|
+
const kc = getAliasCount(doc, node.key, anchors);
|
|
88
|
+
const vc = getAliasCount(doc, node.value, anchors);
|
|
89
|
+
return Math.max(kc, vc);
|
|
90
|
+
}
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { Alias };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createNode } from '../doc/createNode.js';
|
|
2
|
+
import { NodeBase, isCollection, isScalar, isPair } from './Node.js';
|
|
3
|
+
|
|
4
|
+
function collectionFromPath(schema, path, value) {
|
|
5
|
+
let v = value;
|
|
6
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
7
|
+
const k = path[i];
|
|
8
|
+
if (typeof k === 'number' && Number.isInteger(k) && k >= 0) {
|
|
9
|
+
const a = [];
|
|
10
|
+
a[k] = v;
|
|
11
|
+
v = a;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
const o = {};
|
|
15
|
+
Object.defineProperty(o, typeof k === 'symbol' ? k : String(k), {
|
|
16
|
+
value: v,
|
|
17
|
+
writable: true,
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true
|
|
20
|
+
});
|
|
21
|
+
v = o;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return createNode(v, undefined, {
|
|
25
|
+
onAnchor() {
|
|
26
|
+
throw new Error('Repeated objects are not supported here');
|
|
27
|
+
},
|
|
28
|
+
schema,
|
|
29
|
+
sourceObjects: new Map()
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// null, undefined, or an empty non-string iterable (e.g. [])
|
|
33
|
+
const isEmptyPath = (path) => path == null ||
|
|
34
|
+
(typeof path === 'object' && !!path[Symbol.iterator]().next().done);
|
|
35
|
+
class Collection extends NodeBase {
|
|
36
|
+
constructor(type, schema) {
|
|
37
|
+
super(type);
|
|
38
|
+
Object.defineProperty(this, 'schema', {
|
|
39
|
+
value: schema,
|
|
40
|
+
configurable: true,
|
|
41
|
+
enumerable: false,
|
|
42
|
+
writable: true
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Adds a value to the collection. For `!!map` and `!!omap` the value must
|
|
47
|
+
* be a Pair instance or a `{ key, value }` object, which may not have a key
|
|
48
|
+
* that already exists in the map.
|
|
49
|
+
*/
|
|
50
|
+
addIn(path, value) {
|
|
51
|
+
if (isEmptyPath(path))
|
|
52
|
+
this.add(value);
|
|
53
|
+
else {
|
|
54
|
+
const [key, ...rest] = path;
|
|
55
|
+
const node = this.get(key, true);
|
|
56
|
+
if (isCollection(node))
|
|
57
|
+
node.addIn(rest, value);
|
|
58
|
+
else if (node === undefined && this.schema)
|
|
59
|
+
this.set(key, collectionFromPath(this.schema, rest, value));
|
|
60
|
+
else
|
|
61
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Removes a value from the collection.
|
|
66
|
+
* @returns `true` if the item was found and removed.
|
|
67
|
+
*/
|
|
68
|
+
deleteIn([key, ...rest]) {
|
|
69
|
+
if (rest.length === 0)
|
|
70
|
+
return this.delete(key);
|
|
71
|
+
const node = this.get(key, true);
|
|
72
|
+
if (isCollection(node))
|
|
73
|
+
return node.deleteIn(rest);
|
|
74
|
+
else
|
|
75
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
79
|
+
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
80
|
+
* `true` (collections are always returned intact).
|
|
81
|
+
*/
|
|
82
|
+
getIn([key, ...rest], keepScalar) {
|
|
83
|
+
const node = this.get(key, true);
|
|
84
|
+
if (rest.length === 0)
|
|
85
|
+
return !keepScalar && isScalar(node) ? node.value : node;
|
|
86
|
+
else
|
|
87
|
+
return isCollection(node) ? node.getIn(rest, keepScalar) : undefined;
|
|
88
|
+
}
|
|
89
|
+
hasAllNullValues(allowScalar) {
|
|
90
|
+
return this.items.every(node => {
|
|
91
|
+
if (!isPair(node))
|
|
92
|
+
return false;
|
|
93
|
+
const n = node.value;
|
|
94
|
+
return (n == null ||
|
|
95
|
+
(allowScalar &&
|
|
96
|
+
isScalar(n) &&
|
|
97
|
+
n.value == null &&
|
|
98
|
+
!n.commentBefore &&
|
|
99
|
+
!n.comment &&
|
|
100
|
+
!n.tag));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Checks if the collection includes a value with the key `key`.
|
|
105
|
+
*/
|
|
106
|
+
hasIn([key, ...rest]) {
|
|
107
|
+
if (rest.length === 0)
|
|
108
|
+
return this.has(key);
|
|
109
|
+
const node = this.get(key, true);
|
|
110
|
+
return isCollection(node) ? node.hasIn(rest) : false;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Sets a value in this collection. For `!!set`, `value` needs to be a
|
|
114
|
+
* boolean to add/remove the item from the set.
|
|
115
|
+
*/
|
|
116
|
+
setIn([key, ...rest], value) {
|
|
117
|
+
if (rest.length === 0) {
|
|
118
|
+
this.set(key, value);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const node = this.get(key, true);
|
|
122
|
+
if (isCollection(node))
|
|
123
|
+
node.setIn(rest, value);
|
|
124
|
+
else if (node === undefined && this.schema)
|
|
125
|
+
this.set(key, collectionFromPath(this.schema, rest, value));
|
|
126
|
+
else
|
|
127
|
+
throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
Collection.maxFlowStringSingleLineLength = 60;
|
|
132
|
+
|
|
133
|
+
export { Collection, collectionFromPath, isEmptyPath };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const ALIAS = Symbol.for('yaml.alias');
|
|
2
|
+
const DOC = Symbol.for('yaml.document');
|
|
3
|
+
const MAP = Symbol.for('yaml.map');
|
|
4
|
+
const PAIR = Symbol.for('yaml.pair');
|
|
5
|
+
const SCALAR = Symbol.for('yaml.scalar');
|
|
6
|
+
const SEQ = Symbol.for('yaml.seq');
|
|
7
|
+
const NODE_TYPE = Symbol.for('yaml.node.type');
|
|
8
|
+
const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
|
|
9
|
+
const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
|
|
10
|
+
const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
|
|
11
|
+
const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
|
|
12
|
+
const isScalar = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR;
|
|
13
|
+
const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
|
|
14
|
+
function isCollection(node) {
|
|
15
|
+
if (node && typeof node === 'object')
|
|
16
|
+
switch (node[NODE_TYPE]) {
|
|
17
|
+
case MAP:
|
|
18
|
+
case SEQ:
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function isNode(node) {
|
|
24
|
+
if (node && typeof node === 'object')
|
|
25
|
+
switch (node[NODE_TYPE]) {
|
|
26
|
+
case ALIAS:
|
|
27
|
+
case MAP:
|
|
28
|
+
case SCALAR:
|
|
29
|
+
case SEQ:
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
|
|
35
|
+
class NodeBase {
|
|
36
|
+
constructor(type) {
|
|
37
|
+
Object.defineProperty(this, NODE_TYPE, { value: type });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { ALIAS, DOC, MAP, NODE_TYPE, NodeBase, PAIR, SCALAR, SEQ, hasAnchor, isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createNode } from '../doc/createNode.js';
|
|
2
|
+
import { stringifyPair } from '../stringify/stringifyPair.js';
|
|
3
|
+
import { addPairToJSMap } from './addPairToJSMap.js';
|
|
4
|
+
import { NODE_TYPE, PAIR } from './Node.js';
|
|
5
|
+
|
|
6
|
+
function createPair(key, value, ctx) {
|
|
7
|
+
const k = createNode(key, undefined, ctx);
|
|
8
|
+
const v = createNode(value, undefined, ctx);
|
|
9
|
+
return new Pair(k, v);
|
|
10
|
+
}
|
|
11
|
+
class Pair {
|
|
12
|
+
constructor(key, value = null) {
|
|
13
|
+
Object.defineProperty(this, NODE_TYPE, { value: PAIR });
|
|
14
|
+
this.key = key;
|
|
15
|
+
this.value = value;
|
|
16
|
+
}
|
|
17
|
+
toJSON(_, ctx) {
|
|
18
|
+
const pair = ctx && ctx.mapAsMap ? new Map() : {};
|
|
19
|
+
return addPairToJSMap(ctx, pair, this);
|
|
20
|
+
}
|
|
21
|
+
toString(ctx, onComment, onChompKeep) {
|
|
22
|
+
return ctx && ctx.doc
|
|
23
|
+
? stringifyPair(this, ctx, onComment, onChompKeep)
|
|
24
|
+
: JSON.stringify(this);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { Pair, createPair };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NodeBase, SCALAR } from './Node.js';
|
|
2
|
+
import { toJS } from './toJS.js';
|
|
3
|
+
|
|
4
|
+
const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
|
|
5
|
+
class Scalar extends NodeBase {
|
|
6
|
+
constructor(value) {
|
|
7
|
+
super(SCALAR);
|
|
8
|
+
this.value = value;
|
|
9
|
+
}
|
|
10
|
+
toJSON(arg, ctx) {
|
|
11
|
+
return ctx && ctx.keep ? this.value : toJS(this.value, arg, ctx);
|
|
12
|
+
}
|
|
13
|
+
toString() {
|
|
14
|
+
return String(this.value);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
Scalar.BLOCK_FOLDED = 'BLOCK_FOLDED';
|
|
18
|
+
Scalar.BLOCK_LITERAL = 'BLOCK_LITERAL';
|
|
19
|
+
Scalar.PLAIN = 'PLAIN';
|
|
20
|
+
Scalar.QUOTE_DOUBLE = 'QUOTE_DOUBLE';
|
|
21
|
+
Scalar.QUOTE_SINGLE = 'QUOTE_SINGLE';
|
|
22
|
+
|
|
23
|
+
export { Scalar, isScalarValue };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { stringifyCollection } from '../stringify/stringifyCollection.js';
|
|
2
|
+
import { addPairToJSMap } from './addPairToJSMap.js';
|
|
3
|
+
import { Collection } from './Collection.js';
|
|
4
|
+
import { isPair, isScalar, MAP } from './Node.js';
|
|
5
|
+
import { Pair } from './Pair.js';
|
|
6
|
+
import { isScalarValue } from './Scalar.js';
|
|
7
|
+
|
|
8
|
+
function findPair(items, key) {
|
|
9
|
+
const k = isScalar(key) ? key.value : key;
|
|
10
|
+
for (const it of items) {
|
|
11
|
+
if (isPair(it)) {
|
|
12
|
+
if (it.key === key || it.key === k)
|
|
13
|
+
return it;
|
|
14
|
+
if (isScalar(it.key) && it.key.value === k)
|
|
15
|
+
return it;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
class YAMLMap extends Collection {
|
|
21
|
+
constructor(schema) {
|
|
22
|
+
super(MAP, schema);
|
|
23
|
+
this.items = [];
|
|
24
|
+
}
|
|
25
|
+
static get tagName() {
|
|
26
|
+
return 'tag:yaml.org,2002:map';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Adds a value to the collection.
|
|
30
|
+
*
|
|
31
|
+
* @param overwrite - If not set `true`, using a key that is already in the
|
|
32
|
+
* collection will throw. Otherwise, overwrites the previous value.
|
|
33
|
+
*/
|
|
34
|
+
add(pair, overwrite) {
|
|
35
|
+
let _pair;
|
|
36
|
+
if (isPair(pair))
|
|
37
|
+
_pair = pair;
|
|
38
|
+
else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
|
|
39
|
+
// In TypeScript, this never happens.
|
|
40
|
+
_pair = new Pair(pair, pair.value);
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
_pair = new Pair(pair.key, pair.value);
|
|
44
|
+
const prev = findPair(this.items, _pair.key);
|
|
45
|
+
const sortEntries = this.schema && this.schema.sortMapEntries;
|
|
46
|
+
if (prev) {
|
|
47
|
+
if (!overwrite)
|
|
48
|
+
throw new Error(`Key ${_pair.key} already set`);
|
|
49
|
+
// For scalars, keep the old node & its comments and anchors
|
|
50
|
+
if (isScalar(prev.value) && isScalarValue(_pair.value))
|
|
51
|
+
prev.value.value = _pair.value;
|
|
52
|
+
else
|
|
53
|
+
prev.value = _pair.value;
|
|
54
|
+
}
|
|
55
|
+
else if (sortEntries) {
|
|
56
|
+
const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
|
|
57
|
+
if (i === -1)
|
|
58
|
+
this.items.push(_pair);
|
|
59
|
+
else
|
|
60
|
+
this.items.splice(i, 0, _pair);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.items.push(_pair);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
delete(key) {
|
|
67
|
+
const it = findPair(this.items, key);
|
|
68
|
+
if (!it)
|
|
69
|
+
return false;
|
|
70
|
+
const del = this.items.splice(this.items.indexOf(it), 1);
|
|
71
|
+
return del.length > 0;
|
|
72
|
+
}
|
|
73
|
+
get(key, keepScalar) {
|
|
74
|
+
const it = findPair(this.items, key);
|
|
75
|
+
const node = it && it.value;
|
|
76
|
+
return !keepScalar && isScalar(node) ? node.value : node;
|
|
77
|
+
}
|
|
78
|
+
has(key) {
|
|
79
|
+
return !!findPair(this.items, key);
|
|
80
|
+
}
|
|
81
|
+
set(key, value) {
|
|
82
|
+
this.add(new Pair(key, value), true);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* @param ctx - Conversion context, originally set in Document#toJS()
|
|
86
|
+
* @param {Class} Type - If set, forces the returned collection type
|
|
87
|
+
* @returns Instance of Type, Map, or Object
|
|
88
|
+
*/
|
|
89
|
+
toJSON(_, ctx, Type) {
|
|
90
|
+
const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
|
|
91
|
+
if (ctx && ctx.onCreate)
|
|
92
|
+
ctx.onCreate(map);
|
|
93
|
+
for (const item of this.items)
|
|
94
|
+
addPairToJSMap(ctx, map, item);
|
|
95
|
+
return map;
|
|
96
|
+
}
|
|
97
|
+
toString(ctx, onComment, onChompKeep) {
|
|
98
|
+
if (!ctx)
|
|
99
|
+
return JSON.stringify(this);
|
|
100
|
+
for (const item of this.items) {
|
|
101
|
+
if (!isPair(item))
|
|
102
|
+
throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
|
|
103
|
+
}
|
|
104
|
+
if (!ctx.allNullValues && this.hasAllNullValues(false))
|
|
105
|
+
ctx = Object.assign({}, ctx, { allNullValues: true });
|
|
106
|
+
return stringifyCollection(this, ctx, {
|
|
107
|
+
blockItem: n => n.str,
|
|
108
|
+
flowChars: { start: '{', end: '}' },
|
|
109
|
+
itemIndent: ctx.indent || '',
|
|
110
|
+
onChompKeep,
|
|
111
|
+
onComment
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { YAMLMap, findPair };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { stringifyCollection } from '../stringify/stringifyCollection.js';
|
|
2
|
+
import { Collection } from './Collection.js';
|
|
3
|
+
import { SEQ, isScalar } from './Node.js';
|
|
4
|
+
import { isScalarValue } from './Scalar.js';
|
|
5
|
+
import { toJS } from './toJS.js';
|
|
6
|
+
|
|
7
|
+
class YAMLSeq extends Collection {
|
|
8
|
+
constructor(schema) {
|
|
9
|
+
super(SEQ, schema);
|
|
10
|
+
this.items = [];
|
|
11
|
+
}
|
|
12
|
+
static get tagName() {
|
|
13
|
+
return 'tag:yaml.org,2002:seq';
|
|
14
|
+
}
|
|
15
|
+
add(value) {
|
|
16
|
+
this.items.push(value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Removes a value from the collection.
|
|
20
|
+
*
|
|
21
|
+
* `key` must contain a representation of an integer for this to succeed.
|
|
22
|
+
* It may be wrapped in a `Scalar`.
|
|
23
|
+
*
|
|
24
|
+
* @returns `true` if the item was found and removed.
|
|
25
|
+
*/
|
|
26
|
+
delete(key) {
|
|
27
|
+
const idx = asItemIndex(key);
|
|
28
|
+
if (typeof idx !== 'number')
|
|
29
|
+
return false;
|
|
30
|
+
const del = this.items.splice(idx, 1);
|
|
31
|
+
return del.length > 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns item at `key`, or `undefined` if not found. By default unwraps
|
|
35
|
+
* scalar values from their surrounding node; to disable set `keepScalar` to
|
|
36
|
+
* `true` (collections are always returned intact).
|
|
37
|
+
*
|
|
38
|
+
* `key` must contain a representation of an integer for this to succeed.
|
|
39
|
+
* It may be wrapped in a `Scalar`.
|
|
40
|
+
*/
|
|
41
|
+
get(key, keepScalar) {
|
|
42
|
+
const idx = asItemIndex(key);
|
|
43
|
+
if (typeof idx !== 'number')
|
|
44
|
+
return undefined;
|
|
45
|
+
const it = this.items[idx];
|
|
46
|
+
return !keepScalar && isScalar(it) ? it.value : it;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Checks if the collection includes a value with the key `key`.
|
|
50
|
+
*
|
|
51
|
+
* `key` must contain a representation of an integer for this to succeed.
|
|
52
|
+
* It may be wrapped in a `Scalar`.
|
|
53
|
+
*/
|
|
54
|
+
has(key) {
|
|
55
|
+
const idx = asItemIndex(key);
|
|
56
|
+
return typeof idx === 'number' && idx < this.items.length;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sets a value in this collection. For `!!set`, `value` needs to be a
|
|
60
|
+
* boolean to add/remove the item from the set.
|
|
61
|
+
*
|
|
62
|
+
* If `key` does not contain a representation of an integer, this will throw.
|
|
63
|
+
* It may be wrapped in a `Scalar`.
|
|
64
|
+
*/
|
|
65
|
+
set(key, value) {
|
|
66
|
+
const idx = asItemIndex(key);
|
|
67
|
+
if (typeof idx !== 'number')
|
|
68
|
+
throw new Error(`Expected a valid index, not ${key}.`);
|
|
69
|
+
const prev = this.items[idx];
|
|
70
|
+
if (isScalar(prev) && isScalarValue(value))
|
|
71
|
+
prev.value = value;
|
|
72
|
+
else
|
|
73
|
+
this.items[idx] = value;
|
|
74
|
+
}
|
|
75
|
+
toJSON(_, ctx) {
|
|
76
|
+
const seq = [];
|
|
77
|
+
if (ctx && ctx.onCreate)
|
|
78
|
+
ctx.onCreate(seq);
|
|
79
|
+
let i = 0;
|
|
80
|
+
for (const item of this.items)
|
|
81
|
+
seq.push(toJS(item, String(i++), ctx));
|
|
82
|
+
return seq;
|
|
83
|
+
}
|
|
84
|
+
toString(ctx, onComment, onChompKeep) {
|
|
85
|
+
if (!ctx)
|
|
86
|
+
return JSON.stringify(this);
|
|
87
|
+
return stringifyCollection(this, ctx, {
|
|
88
|
+
blockItem: n => (n.comment ? n.str : `- ${n.str}`),
|
|
89
|
+
flowChars: { start: '[', end: ']' },
|
|
90
|
+
itemIndent: (ctx.indent || '') + ' ',
|
|
91
|
+
onChompKeep,
|
|
92
|
+
onComment
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function asItemIndex(key) {
|
|
97
|
+
let idx = isScalar(key) ? key.value : key;
|
|
98
|
+
if (idx && typeof idx === 'string')
|
|
99
|
+
idx = Number(idx);
|
|
100
|
+
return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
|
|
101
|
+
? idx
|
|
102
|
+
: null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { YAMLSeq };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { warn } from '../log.js';
|
|
2
|
+
import { createStringifyContext } from '../stringify/stringify.js';
|
|
3
|
+
import { isScalar, isSeq, isAlias, isMap, isNode } from './Node.js';
|
|
4
|
+
import { Scalar } from './Scalar.js';
|
|
5
|
+
import { toJS } from './toJS.js';
|
|
6
|
+
|
|
7
|
+
const MERGE_KEY = '<<';
|
|
8
|
+
function addPairToJSMap(ctx, map, { key, value }) {
|
|
9
|
+
if (ctx && ctx.doc.schema.merge && isMergeKey(key)) {
|
|
10
|
+
if (isSeq(value))
|
|
11
|
+
for (const it of value.items)
|
|
12
|
+
mergeToJSMap(ctx, map, it);
|
|
13
|
+
else if (Array.isArray(value))
|
|
14
|
+
for (const it of value)
|
|
15
|
+
mergeToJSMap(ctx, map, it);
|
|
16
|
+
else
|
|
17
|
+
mergeToJSMap(ctx, map, value);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const jsKey = toJS(key, '', ctx);
|
|
21
|
+
if (map instanceof Map) {
|
|
22
|
+
map.set(jsKey, toJS(value, jsKey, ctx));
|
|
23
|
+
}
|
|
24
|
+
else if (map instanceof Set) {
|
|
25
|
+
map.add(jsKey);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const stringKey = stringifyKey(key, jsKey, ctx);
|
|
29
|
+
const jsValue = toJS(value, stringKey, ctx);
|
|
30
|
+
if (stringKey in map)
|
|
31
|
+
Object.defineProperty(map, stringKey, {
|
|
32
|
+
value: jsValue,
|
|
33
|
+
writable: true,
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true
|
|
36
|
+
});
|
|
37
|
+
else
|
|
38
|
+
map[stringKey] = jsValue;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return map;
|
|
42
|
+
}
|
|
43
|
+
const isMergeKey = (key) => key === MERGE_KEY ||
|
|
44
|
+
(isScalar(key) &&
|
|
45
|
+
key.value === MERGE_KEY &&
|
|
46
|
+
(!key.type || key.type === Scalar.PLAIN));
|
|
47
|
+
// If the value associated with a merge key is a single mapping node, each of
|
|
48
|
+
// its key/value pairs is inserted into the current mapping, unless the key
|
|
49
|
+
// already exists in it. If the value associated with the merge key is a
|
|
50
|
+
// sequence, then this sequence is expected to contain mapping nodes and each
|
|
51
|
+
// of these nodes is merged in turn according to its order in the sequence.
|
|
52
|
+
// Keys in mapping nodes earlier in the sequence override keys specified in
|
|
53
|
+
// later mapping nodes. -- http://yaml.org/type/merge.html
|
|
54
|
+
function mergeToJSMap(ctx, map, value) {
|
|
55
|
+
const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
|
|
56
|
+
if (!isMap(source))
|
|
57
|
+
throw new Error('Merge sources must be maps or map aliases');
|
|
58
|
+
const srcMap = source.toJSON(null, ctx, Map);
|
|
59
|
+
for (const [key, value] of srcMap) {
|
|
60
|
+
if (map instanceof Map) {
|
|
61
|
+
if (!map.has(key))
|
|
62
|
+
map.set(key, value);
|
|
63
|
+
}
|
|
64
|
+
else if (map instanceof Set) {
|
|
65
|
+
map.add(key);
|
|
66
|
+
}
|
|
67
|
+
else if (!Object.prototype.hasOwnProperty.call(map, key)) {
|
|
68
|
+
Object.defineProperty(map, key, {
|
|
69
|
+
value,
|
|
70
|
+
writable: true,
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return map;
|
|
77
|
+
}
|
|
78
|
+
function stringifyKey(key, jsKey, ctx) {
|
|
79
|
+
if (jsKey === null)
|
|
80
|
+
return '';
|
|
81
|
+
if (typeof jsKey !== 'object')
|
|
82
|
+
return String(jsKey);
|
|
83
|
+
if (isNode(key) && ctx && ctx.doc) {
|
|
84
|
+
const strCtx = createStringifyContext(ctx.doc, {});
|
|
85
|
+
strCtx.anchors = new Set();
|
|
86
|
+
for (const node of ctx.anchors.keys())
|
|
87
|
+
strCtx.anchors.add(node.anchor);
|
|
88
|
+
strCtx.inFlow = true;
|
|
89
|
+
strCtx.inStringifyKey = true;
|
|
90
|
+
const strKey = key.toString(strCtx);
|
|
91
|
+
if (!ctx.mapKeyWarned) {
|
|
92
|
+
let jsonStr = JSON.stringify(strKey);
|
|
93
|
+
if (jsonStr.length > 40)
|
|
94
|
+
jsonStr = jsonStr.substring(0, 36) + '..."';
|
|
95
|
+
warn(ctx.doc.options.logLevel, `Keys with collection values will be stringified due to JS Object restrictions: ${jsonStr}. Set mapAsMap: true to use object keys.`);
|
|
96
|
+
ctx.mapKeyWarned = true;
|
|
97
|
+
}
|
|
98
|
+
return strKey;
|
|
99
|
+
}
|
|
100
|
+
return JSON.stringify(jsKey);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { addPairToJSMap };
|