yaml 2.0.0-2 → 2.0.0-6

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.
Files changed (347) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +46 -26
  3. package/browser/dist/compose/compose-collection.js +59 -0
  4. package/browser/dist/compose/compose-doc.js +39 -0
  5. package/browser/dist/compose/compose-node.js +72 -0
  6. package/browser/dist/compose/compose-scalar.js +74 -0
  7. package/browser/dist/compose/composer.js +218 -0
  8. package/browser/dist/compose/resolve-block-map.js +93 -0
  9. package/browser/dist/compose/resolve-block-scalar.js +185 -0
  10. package/browser/dist/compose/resolve-block-seq.js +40 -0
  11. package/browser/dist/compose/resolve-end.js +37 -0
  12. package/browser/dist/compose/resolve-flow-collection.js +189 -0
  13. package/browser/dist/compose/resolve-flow-scalar.js +211 -0
  14. package/browser/dist/compose/resolve-props.js +126 -0
  15. package/browser/dist/compose/util-contains-newline.js +34 -0
  16. package/browser/dist/compose/util-empty-scalar-position.js +27 -0
  17. package/browser/dist/compose/util-map-includes.js +17 -0
  18. package/browser/dist/doc/Document.js +251 -348
  19. package/browser/dist/doc/anchors.js +72 -0
  20. package/browser/dist/doc/applyReviver.js +41 -28
  21. package/browser/dist/doc/createNode.js +71 -55
  22. package/browser/dist/doc/directives.js +161 -0
  23. package/browser/dist/errors.js +50 -72
  24. package/browser/dist/index.js +17 -73
  25. package/browser/dist/log.js +11 -7
  26. package/browser/dist/nodes/Alias.js +94 -0
  27. package/browser/dist/nodes/Collection.js +133 -0
  28. package/browser/dist/nodes/Node.js +41 -0
  29. package/browser/dist/nodes/Pair.js +28 -0
  30. package/browser/dist/nodes/Scalar.js +23 -0
  31. package/browser/dist/nodes/YAMLMap.js +116 -0
  32. package/browser/dist/nodes/YAMLSeq.js +105 -0
  33. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  34. package/browser/dist/nodes/toJS.js +35 -0
  35. package/browser/dist/options.js +14 -95
  36. package/browser/dist/parse/cst-scalar.js +219 -0
  37. package/browser/dist/parse/cst-stringify.js +61 -0
  38. package/browser/dist/parse/cst-visit.js +97 -0
  39. package/browser/dist/parse/cst.js +98 -0
  40. package/browser/dist/parse/lexer.js +656 -0
  41. package/browser/dist/parse/line-counter.js +39 -0
  42. package/browser/dist/parse/parser.js +876 -0
  43. package/browser/dist/public-api.js +101 -0
  44. package/browser/dist/schema/Schema.js +23 -0
  45. package/browser/dist/schema/common/map.js +42 -0
  46. package/browser/dist/schema/common/null.js +13 -0
  47. package/browser/dist/schema/common/seq.js +33 -0
  48. package/browser/dist/schema/common/string.js +14 -0
  49. package/browser/dist/schema/core/bool.js +19 -0
  50. package/browser/dist/schema/core/float.js +40 -0
  51. package/browser/dist/schema/core/int.js +38 -0
  52. package/browser/dist/schema/core/schema.js +23 -0
  53. package/browser/dist/schema/json/schema.js +62 -0
  54. package/browser/dist/schema/tags.js +79 -0
  55. package/browser/dist/schema/yaml-1.1/binary.js +66 -0
  56. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  57. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  58. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  59. package/browser/dist/schema/yaml-1.1/omap.js +73 -0
  60. package/browser/dist/schema/yaml-1.1/pairs.js +77 -0
  61. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  62. package/browser/dist/schema/yaml-1.1/set.js +87 -0
  63. package/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
  64. package/browser/dist/stringify/foldFlowLines.js +121 -139
  65. package/browser/dist/stringify/stringify.js +87 -72
  66. package/browser/dist/stringify/stringifyCollection.js +122 -0
  67. package/browser/dist/stringify/stringifyComment.js +14 -0
  68. package/browser/dist/stringify/stringifyDocument.js +64 -0
  69. package/browser/dist/stringify/stringifyNumber.js +20 -23
  70. package/browser/dist/stringify/stringifyPair.js +111 -0
  71. package/browser/dist/stringify/stringifyString.js +295 -322
  72. package/browser/dist/util.js +4 -4
  73. package/browser/dist/visit.js +146 -0
  74. package/browser/index.js +4 -1
  75. package/dist/compose/compose-collection.d.ts +5 -0
  76. package/dist/compose/compose-collection.js +61 -0
  77. package/dist/compose/compose-doc.d.ts +6 -0
  78. package/dist/compose/compose-doc.js +41 -0
  79. package/dist/compose/compose-node.d.ts +25 -0
  80. package/dist/compose/compose-node.js +75 -0
  81. package/dist/compose/compose-scalar.d.ts +5 -0
  82. package/dist/compose/compose-scalar.js +76 -0
  83. package/dist/compose/composer.d.ts +62 -0
  84. package/dist/compose/composer.js +222 -0
  85. package/dist/compose/resolve-block-map.d.ts +6 -0
  86. package/dist/compose/resolve-block-map.js +95 -0
  87. package/dist/compose/resolve-block-scalar.d.ts +10 -0
  88. package/dist/compose/resolve-block-scalar.js +187 -0
  89. package/dist/compose/resolve-block-seq.d.ts +5 -0
  90. package/dist/compose/resolve-block-seq.js +42 -0
  91. package/dist/compose/resolve-end.d.ts +6 -0
  92. package/dist/compose/resolve-end.js +39 -0
  93. package/dist/compose/resolve-flow-collection.d.ts +6 -0
  94. package/dist/compose/resolve-flow-collection.js +191 -0
  95. package/dist/compose/resolve-flow-scalar.d.ts +10 -0
  96. package/dist/compose/resolve-flow-scalar.js +213 -0
  97. package/dist/compose/resolve-props.d.ts +21 -0
  98. package/dist/compose/resolve-props.js +128 -0
  99. package/dist/compose/util-contains-newline.d.ts +2 -0
  100. package/dist/compose/util-contains-newline.js +36 -0
  101. package/dist/compose/util-empty-scalar-position.d.ts +2 -0
  102. package/dist/compose/util-empty-scalar-position.js +29 -0
  103. package/dist/compose/util-map-includes.d.ts +4 -0
  104. package/dist/compose/util-map-includes.js +19 -0
  105. package/dist/doc/Document.d.ts +130 -0
  106. package/dist/doc/Document.js +250 -347
  107. package/dist/doc/anchors.d.ts +24 -0
  108. package/dist/doc/anchors.js +77 -0
  109. package/dist/doc/applyReviver.d.ts +9 -0
  110. package/dist/doc/applyReviver.js +41 -28
  111. package/dist/doc/createNode.d.ts +16 -0
  112. package/dist/doc/createNode.js +71 -55
  113. package/dist/doc/directives.d.ts +46 -0
  114. package/dist/doc/directives.js +163 -0
  115. package/dist/errors.d.ts +21 -0
  116. package/dist/errors.js +51 -74
  117. package/dist/index.d.ts +19 -0
  118. package/dist/index.js +40 -71
  119. package/dist/log.d.ts +3 -0
  120. package/dist/log.js +11 -6
  121. package/dist/nodes/Alias.d.ts +24 -0
  122. package/dist/nodes/Alias.js +96 -0
  123. package/dist/nodes/Collection.d.ts +67 -0
  124. package/dist/nodes/Collection.js +137 -0
  125. package/dist/nodes/Node.d.ts +48 -0
  126. package/dist/nodes/Node.js +59 -0
  127. package/dist/nodes/Pair.d.ts +16 -0
  128. package/dist/nodes/Pair.js +31 -0
  129. package/dist/nodes/Scalar.d.ts +40 -0
  130. package/dist/nodes/Scalar.js +26 -0
  131. package/dist/nodes/YAMLMap.d.ts +39 -0
  132. package/dist/nodes/YAMLMap.js +119 -0
  133. package/dist/nodes/YAMLSeq.d.ts +53 -0
  134. package/dist/nodes/YAMLSeq.js +107 -0
  135. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  136. package/dist/nodes/addPairToJSMap.js +105 -0
  137. package/dist/nodes/toJS.d.ts +30 -0
  138. package/dist/nodes/toJS.js +37 -0
  139. package/dist/options.d.ts +278 -0
  140. package/dist/options.js +13 -96
  141. package/dist/parse/cst-scalar.d.ts +58 -0
  142. package/dist/parse/cst-scalar.js +223 -0
  143. package/dist/parse/cst-stringify.d.ts +8 -0
  144. package/dist/parse/cst-stringify.js +63 -0
  145. package/dist/parse/cst-visit.d.ts +39 -0
  146. package/dist/parse/cst-visit.js +99 -0
  147. package/dist/parse/cst.d.ts +106 -0
  148. package/dist/parse/cst.js +112 -0
  149. package/dist/parse/lexer.d.ts +85 -0
  150. package/dist/parse/lexer.js +658 -0
  151. package/dist/parse/line-counter.d.ts +22 -0
  152. package/dist/parse/line-counter.js +41 -0
  153. package/dist/parse/parser.d.ts +84 -0
  154. package/dist/parse/parser.js +880 -0
  155. package/dist/public-api.d.ts +43 -0
  156. package/dist/public-api.js +106 -0
  157. package/dist/schema/Schema.d.ts +16 -0
  158. package/dist/schema/Schema.js +25 -0
  159. package/dist/schema/common/map.d.ts +2 -0
  160. package/dist/schema/common/map.js +44 -0
  161. package/dist/schema/common/null.d.ts +4 -0
  162. package/dist/schema/common/null.js +15 -0
  163. package/dist/schema/common/seq.d.ts +2 -0
  164. package/dist/schema/common/seq.js +35 -0
  165. package/dist/schema/common/string.d.ts +2 -0
  166. package/dist/schema/common/string.js +16 -0
  167. package/dist/schema/core/bool.d.ts +4 -0
  168. package/dist/schema/core/bool.js +21 -0
  169. package/dist/schema/core/float.d.ts +4 -0
  170. package/dist/schema/core/float.js +44 -0
  171. package/dist/schema/core/int.d.ts +4 -0
  172. package/dist/schema/core/int.js +42 -0
  173. package/dist/schema/core/schema.d.ts +1 -0
  174. package/dist/schema/core/schema.js +25 -0
  175. package/dist/schema/json/schema.d.ts +2 -0
  176. package/dist/schema/json/schema.js +64 -0
  177. package/dist/schema/tags.d.ts +41 -0
  178. package/dist/schema/tags.js +82 -0
  179. package/dist/schema/types.d.ts +82 -0
  180. package/dist/schema/yaml-1.1/binary.d.ts +2 -0
  181. package/dist/schema/yaml-1.1/binary.js +68 -0
  182. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  183. package/dist/schema/yaml-1.1/bool.js +29 -0
  184. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  185. package/dist/schema/yaml-1.1/float.js +47 -0
  186. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  187. package/dist/schema/yaml-1.1/int.js +76 -0
  188. package/dist/schema/yaml-1.1/omap.d.ts +21 -0
  189. package/dist/schema/yaml-1.1/omap.js +76 -0
  190. package/dist/schema/yaml-1.1/pairs.d.ts +10 -0
  191. package/dist/schema/yaml-1.1/pairs.js +81 -0
  192. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  193. package/dist/schema/yaml-1.1/schema.js +39 -0
  194. package/dist/schema/yaml-1.1/set.d.ts +22 -0
  195. package/dist/schema/yaml-1.1/set.js +90 -0
  196. package/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
  197. package/dist/schema/yaml-1.1/timestamp.js +105 -0
  198. package/dist/stringify/foldFlowLines.d.ts +34 -0
  199. package/dist/stringify/foldFlowLines.js +121 -139
  200. package/dist/stringify/stringify.d.ts +18 -0
  201. package/dist/stringify/stringify.js +87 -71
  202. package/dist/stringify/stringifyCollection.d.ts +21 -0
  203. package/dist/stringify/stringifyCollection.js +124 -0
  204. package/dist/stringify/stringifyComment.d.ts +2 -0
  205. package/dist/stringify/stringifyComment.js +17 -0
  206. package/dist/stringify/stringifyDocument.d.ts +3 -0
  207. package/dist/stringify/stringifyDocument.js +66 -0
  208. package/dist/stringify/stringifyNumber.d.ts +2 -0
  209. package/dist/stringify/stringifyNumber.js +20 -23
  210. package/dist/stringify/stringifyPair.d.ts +3 -0
  211. package/dist/stringify/stringifyPair.js +113 -0
  212. package/dist/stringify/stringifyString.d.ts +3 -0
  213. package/dist/stringify/stringifyString.js +295 -322
  214. package/dist/test-events.d.ts +4 -0
  215. package/dist/test-events.js +124 -148
  216. package/dist/util.d.ts +6 -0
  217. package/dist/util.js +9 -12
  218. package/dist/visit.d.ts +63 -0
  219. package/dist/visit.js +148 -0
  220. package/package.json +31 -39
  221. package/util.js +2 -14
  222. package/browser/dist/_virtual/_rollupPluginBabelHelpers.js +0 -16
  223. package/browser/dist/ast/Alias.js +0 -95
  224. package/browser/dist/ast/Collection.js +0 -192
  225. package/browser/dist/ast/Merge.js +0 -71
  226. package/browser/dist/ast/Node.js +0 -3
  227. package/browser/dist/ast/Pair.js +0 -200
  228. package/browser/dist/ast/Scalar.js +0 -21
  229. package/browser/dist/ast/YAMLMap.js +0 -91
  230. package/browser/dist/ast/YAMLSeq.js +0 -67
  231. package/browser/dist/ast/toJS.js +0 -29
  232. package/browser/dist/ast/visit.js +0 -83
  233. package/browser/dist/constants.js +0 -40
  234. package/browser/dist/cst/Alias.js +0 -26
  235. package/browser/dist/cst/BlankLine.js +0 -34
  236. package/browser/dist/cst/BlockValue.js +0 -240
  237. package/browser/dist/cst/Collection.js +0 -259
  238. package/browser/dist/cst/CollectionItem.js +0 -123
  239. package/browser/dist/cst/Comment.js +0 -27
  240. package/browser/dist/cst/Directive.js +0 -53
  241. package/browser/dist/cst/Document.js +0 -262
  242. package/browser/dist/cst/FlowCollection.js +0 -201
  243. package/browser/dist/cst/Node.js +0 -355
  244. package/browser/dist/cst/ParseContext.js +0 -234
  245. package/browser/dist/cst/PlainValue.js +0 -171
  246. package/browser/dist/cst/QuoteDouble.js +0 -224
  247. package/browser/dist/cst/QuoteSingle.js +0 -100
  248. package/browser/dist/cst/Range.js +0 -56
  249. package/browser/dist/cst/parse.js +0 -46
  250. package/browser/dist/cst/source-utils.js +0 -169
  251. package/browser/dist/doc/Anchors.js +0 -115
  252. package/browser/dist/doc/Schema.js +0 -34
  253. package/browser/dist/doc/getSchemaTags.js +0 -33
  254. package/browser/dist/doc/listTagNames.js +0 -27
  255. package/browser/dist/doc/parseContents.js +0 -60
  256. package/browser/dist/doc/parseDirectives.js +0 -107
  257. package/browser/dist/resolve/collection-utils.js +0 -86
  258. package/browser/dist/resolve/resolveMap.js +0 -315
  259. package/browser/dist/resolve/resolveNode.js +0 -173
  260. package/browser/dist/resolve/resolveScalar.js +0 -20
  261. package/browser/dist/resolve/resolveSeq.js +0 -170
  262. package/browser/dist/resolve/resolveTag.js +0 -107
  263. package/browser/dist/resolve/resolveTagName.js +0 -87
  264. package/browser/dist/stringify/addComment.js +0 -10
  265. package/browser/dist/stringify/stringifyTag.js +0 -28
  266. package/browser/dist/tags/core.js +0 -128
  267. package/browser/dist/tags/failsafe/index.js +0 -7
  268. package/browser/dist/tags/failsafe/map.js +0 -37
  269. package/browser/dist/tags/failsafe/seq.js +0 -34
  270. package/browser/dist/tags/failsafe/string.js +0 -20
  271. package/browser/dist/tags/index.js +0 -39
  272. package/browser/dist/tags/json.js +0 -62
  273. package/browser/dist/tags/options.js +0 -31
  274. package/browser/dist/tags/yaml-1.1/binary.js +0 -83
  275. package/browser/dist/tags/yaml-1.1/index.js +0 -174
  276. package/browser/dist/tags/yaml-1.1/omap.js +0 -86
  277. package/browser/dist/tags/yaml-1.1/pairs.js +0 -62
  278. package/browser/dist/tags/yaml-1.1/set.js +0 -78
  279. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -101
  280. package/browser/dist/types.js +0 -10
  281. package/cst.d.ts +0 -185
  282. package/dist/_virtual/_rollupPluginBabelHelpers.js +0 -18
  283. package/dist/ast/Alias.js +0 -97
  284. package/dist/ast/Collection.js +0 -196
  285. package/dist/ast/Merge.js +0 -74
  286. package/dist/ast/Node.js +0 -5
  287. package/dist/ast/Pair.js +0 -203
  288. package/dist/ast/Scalar.js +0 -24
  289. package/dist/ast/YAMLMap.js +0 -94
  290. package/dist/ast/YAMLSeq.js +0 -69
  291. package/dist/ast/toJS.js +0 -31
  292. package/dist/ast/visit.js +0 -85
  293. package/dist/constants.js +0 -46
  294. package/dist/cst/Alias.js +0 -28
  295. package/dist/cst/BlankLine.js +0 -36
  296. package/dist/cst/BlockValue.js +0 -243
  297. package/dist/cst/Collection.js +0 -262
  298. package/dist/cst/CollectionItem.js +0 -125
  299. package/dist/cst/Comment.js +0 -29
  300. package/dist/cst/Directive.js +0 -55
  301. package/dist/cst/Document.js +0 -264
  302. package/dist/cst/FlowCollection.js +0 -203
  303. package/dist/cst/Node.js +0 -357
  304. package/dist/cst/ParseContext.js +0 -236
  305. package/dist/cst/PlainValue.js +0 -173
  306. package/dist/cst/QuoteDouble.js +0 -226
  307. package/dist/cst/QuoteSingle.js +0 -102
  308. package/dist/cst/Range.js +0 -58
  309. package/dist/cst/parse.js +0 -48
  310. package/dist/cst/source-utils.js +0 -173
  311. package/dist/doc/Anchors.js +0 -117
  312. package/dist/doc/Schema.js +0 -36
  313. package/dist/doc/getSchemaTags.js +0 -35
  314. package/dist/doc/listTagNames.js +0 -29
  315. package/dist/doc/parseContents.js +0 -62
  316. package/dist/doc/parseDirectives.js +0 -109
  317. package/dist/resolve/collection-utils.js +0 -91
  318. package/dist/resolve/resolveMap.js +0 -317
  319. package/dist/resolve/resolveNode.js +0 -175
  320. package/dist/resolve/resolveScalar.js +0 -22
  321. package/dist/resolve/resolveSeq.js +0 -172
  322. package/dist/resolve/resolveTag.js +0 -109
  323. package/dist/resolve/resolveTagName.js +0 -89
  324. package/dist/stringify/addComment.js +0 -13
  325. package/dist/stringify/stringifyTag.js +0 -30
  326. package/dist/tags/core.js +0 -138
  327. package/dist/tags/failsafe/index.js +0 -9
  328. package/dist/tags/failsafe/map.js +0 -39
  329. package/dist/tags/failsafe/seq.js +0 -36
  330. package/dist/tags/failsafe/string.js +0 -22
  331. package/dist/tags/index.js +0 -42
  332. package/dist/tags/json.js +0 -64
  333. package/dist/tags/options.js +0 -37
  334. package/dist/tags/yaml-1.1/binary.js +0 -85
  335. package/dist/tags/yaml-1.1/index.js +0 -176
  336. package/dist/tags/yaml-1.1/omap.js +0 -89
  337. package/dist/tags/yaml-1.1/pairs.js +0 -66
  338. package/dist/tags/yaml-1.1/set.js +0 -81
  339. package/dist/tags/yaml-1.1/timestamp.js +0 -105
  340. package/dist/types.js +0 -29
  341. package/index.d.ts +0 -519
  342. package/index.js +0 -1
  343. package/types.d.ts +0 -379
  344. package/types.js +0 -17
  345. package/types.mjs +0 -17
  346. package/util.d.ts +0 -76
  347. package/util.mjs +0 -15
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ const ALIAS = Symbol.for('yaml.alias');
4
+ const DOC = Symbol.for('yaml.document');
5
+ const MAP = Symbol.for('yaml.map');
6
+ const PAIR = Symbol.for('yaml.pair');
7
+ const SCALAR = Symbol.for('yaml.scalar');
8
+ const SEQ = Symbol.for('yaml.seq');
9
+ const NODE_TYPE = Symbol.for('yaml.node.type');
10
+ const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
11
+ const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
12
+ const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
13
+ const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
14
+ const isScalar = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR;
15
+ const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
16
+ function isCollection(node) {
17
+ if (node && typeof node === 'object')
18
+ switch (node[NODE_TYPE]) {
19
+ case MAP:
20
+ case SEQ:
21
+ return true;
22
+ }
23
+ return false;
24
+ }
25
+ function isNode(node) {
26
+ if (node && typeof node === 'object')
27
+ switch (node[NODE_TYPE]) {
28
+ case ALIAS:
29
+ case MAP:
30
+ case SCALAR:
31
+ case SEQ:
32
+ return true;
33
+ }
34
+ return false;
35
+ }
36
+ const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
37
+ class NodeBase {
38
+ constructor(type) {
39
+ Object.defineProperty(this, NODE_TYPE, { value: type });
40
+ }
41
+ }
42
+
43
+ exports.ALIAS = ALIAS;
44
+ exports.DOC = DOC;
45
+ exports.MAP = MAP;
46
+ exports.NODE_TYPE = NODE_TYPE;
47
+ exports.NodeBase = NodeBase;
48
+ exports.PAIR = PAIR;
49
+ exports.SCALAR = SCALAR;
50
+ exports.SEQ = SEQ;
51
+ exports.hasAnchor = hasAnchor;
52
+ exports.isAlias = isAlias;
53
+ exports.isCollection = isCollection;
54
+ exports.isDocument = isDocument;
55
+ exports.isMap = isMap;
56
+ exports.isNode = isNode;
57
+ exports.isPair = isPair;
58
+ exports.isScalar = isScalar;
59
+ exports.isSeq = isSeq;
@@ -0,0 +1,16 @@
1
+ import { CreateNodeContext } from '../doc/createNode.js';
2
+ import { StringifyContext } from '../stringify/stringify.js';
3
+ import { addPairToJSMap } from './addPairToJSMap.js';
4
+ import { NODE_TYPE } from './Node.js';
5
+ import { ToJSContext } from './toJS.js';
6
+ export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
7
+ export declare class Pair<K = unknown, V = unknown> {
8
+ readonly [NODE_TYPE]: symbol;
9
+ /** Always Node or null when parsed, but can be set to anything. */
10
+ key: K;
11
+ /** Always Node or null when parsed, but can be set to anything. */
12
+ value: V | null;
13
+ constructor(key: K, value?: V | null);
14
+ toJSON(_?: unknown, ctx?: ToJSContext): ReturnType<typeof addPairToJSMap>;
15
+ toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
16
+ }
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ var createNode = require('../doc/createNode.js');
4
+ var stringifyPair = require('../stringify/stringifyPair.js');
5
+ var addPairToJSMap = require('./addPairToJSMap.js');
6
+ var Node = require('./Node.js');
7
+
8
+ function createPair(key, value, ctx) {
9
+ const k = createNode.createNode(key, undefined, ctx);
10
+ const v = createNode.createNode(value, undefined, ctx);
11
+ return new Pair(k, v);
12
+ }
13
+ class Pair {
14
+ constructor(key, value = null) {
15
+ Object.defineProperty(this, Node.NODE_TYPE, { value: Node.PAIR });
16
+ this.key = key;
17
+ this.value = value;
18
+ }
19
+ toJSON(_, ctx) {
20
+ const pair = ctx && ctx.mapAsMap ? new Map() : {};
21
+ return addPairToJSMap.addPairToJSMap(ctx, pair, this);
22
+ }
23
+ toString(ctx, onComment, onChompKeep) {
24
+ return ctx && ctx.doc
25
+ ? stringifyPair.stringifyPair(this, ctx, onComment, onChompKeep)
26
+ : JSON.stringify(this);
27
+ }
28
+ }
29
+
30
+ exports.Pair = Pair;
31
+ exports.createPair = createPair;
@@ -0,0 +1,40 @@
1
+ import { NodeBase, Range } from './Node.js';
2
+ import { ToJSContext } from './toJS.js';
3
+ export declare const isScalarValue: (value: unknown) => boolean;
4
+ export declare namespace Scalar {
5
+ interface Parsed extends Scalar {
6
+ range: Range;
7
+ source: string;
8
+ }
9
+ type BLOCK_FOLDED = 'BLOCK_FOLDED';
10
+ type BLOCK_LITERAL = 'BLOCK_LITERAL';
11
+ type PLAIN = 'PLAIN';
12
+ type QUOTE_DOUBLE = 'QUOTE_DOUBLE';
13
+ type QUOTE_SINGLE = 'QUOTE_SINGLE';
14
+ type Type = BLOCK_FOLDED | BLOCK_LITERAL | PLAIN | QUOTE_DOUBLE | QUOTE_SINGLE;
15
+ }
16
+ export declare class Scalar<T = unknown> extends NodeBase {
17
+ static readonly BLOCK_FOLDED = "BLOCK_FOLDED";
18
+ static readonly BLOCK_LITERAL = "BLOCK_LITERAL";
19
+ static readonly PLAIN = "PLAIN";
20
+ static readonly QUOTE_DOUBLE = "QUOTE_DOUBLE";
21
+ static readonly QUOTE_SINGLE = "QUOTE_SINGLE";
22
+ value: T;
23
+ /** An optional anchor on this node. Used by alias nodes. */
24
+ anchor?: string;
25
+ /**
26
+ * By default (undefined), numbers use decimal notation.
27
+ * The YAML 1.2 core schema only supports 'HEX' and 'OCT'.
28
+ * The YAML 1.1 schema also supports 'BIN' and 'TIME'
29
+ */
30
+ format?: string;
31
+ /** If `value` is a number, use this value when stringifying this node. */
32
+ minFractionDigits?: number;
33
+ /** Set during parsing to the source string value */
34
+ source?: string;
35
+ /** The scalar style used for the node's string representation */
36
+ type?: Scalar.Type;
37
+ constructor(value: T);
38
+ toJSON(arg?: any, ctx?: ToJSContext): any;
39
+ toString(): string;
40
+ }
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ var Node = require('./Node.js');
4
+ var toJS = require('./toJS.js');
5
+
6
+ const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
7
+ class Scalar extends Node.NodeBase {
8
+ constructor(value) {
9
+ super(Node.SCALAR);
10
+ this.value = value;
11
+ }
12
+ toJSON(arg, ctx) {
13
+ return ctx && ctx.keep ? this.value : toJS.toJS(this.value, arg, ctx);
14
+ }
15
+ toString() {
16
+ return String(this.value);
17
+ }
18
+ }
19
+ Scalar.BLOCK_FOLDED = 'BLOCK_FOLDED';
20
+ Scalar.BLOCK_LITERAL = 'BLOCK_LITERAL';
21
+ Scalar.PLAIN = 'PLAIN';
22
+ Scalar.QUOTE_DOUBLE = 'QUOTE_DOUBLE';
23
+ Scalar.QUOTE_SINGLE = 'QUOTE_SINGLE';
24
+
25
+ exports.Scalar = Scalar;
26
+ exports.isScalarValue = isScalarValue;
@@ -0,0 +1,39 @@
1
+ import type { Schema } from '../schema/Schema.js';
2
+ import type { StringifyContext } from '../stringify/stringify.js';
3
+ import { Collection } from './Collection.js';
4
+ import { ParsedNode, Range } from './Node.js';
5
+ import { Pair } from './Pair.js';
6
+ import type { ToJSContext } from './toJS.js';
7
+ export declare function findPair<K = unknown, V = unknown>(items: Iterable<Pair<K, V>>, key: unknown): Pair<K, V> | undefined;
8
+ export declare namespace YAMLMap {
9
+ interface Parsed<K extends ParsedNode = ParsedNode, V extends ParsedNode | null = ParsedNode | null> extends YAMLMap<K, V> {
10
+ items: Pair<K, V>[];
11
+ range: Range;
12
+ }
13
+ }
14
+ export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
15
+ static get tagName(): 'tag:yaml.org,2002:map';
16
+ items: Pair<K, V>[];
17
+ constructor(schema?: Schema);
18
+ /**
19
+ * Adds a value to the collection.
20
+ *
21
+ * @param overwrite - If not set `true`, using a key that is already in the
22
+ * collection will throw. Otherwise, overwrites the previous value.
23
+ */
24
+ add(pair: Pair<K, V> | {
25
+ key: K;
26
+ value: V;
27
+ }, overwrite?: boolean): void;
28
+ delete(key: K): boolean;
29
+ get(key: K, keepScalar?: boolean): unknown;
30
+ has(key: K): boolean;
31
+ set(key: K, value: V): void;
32
+ /**
33
+ * @param ctx - Conversion context, originally set in Document#toJS()
34
+ * @param {Class} Type - If set, forces the returned collection type
35
+ * @returns Instance of Type, Map, or Object
36
+ */
37
+ toJSON(_?: unknown, ctx?: ToJSContext, Type?: any): any;
38
+ toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
39
+ }
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ var stringifyCollection = require('../stringify/stringifyCollection.js');
4
+ var addPairToJSMap = require('./addPairToJSMap.js');
5
+ var Collection = require('./Collection.js');
6
+ var Node = require('./Node.js');
7
+ var Pair = require('./Pair.js');
8
+ var Scalar = require('./Scalar.js');
9
+
10
+ function findPair(items, key) {
11
+ const k = Node.isScalar(key) ? key.value : key;
12
+ for (const it of items) {
13
+ if (Node.isPair(it)) {
14
+ if (it.key === key || it.key === k)
15
+ return it;
16
+ if (Node.isScalar(it.key) && it.key.value === k)
17
+ return it;
18
+ }
19
+ }
20
+ return undefined;
21
+ }
22
+ class YAMLMap extends Collection.Collection {
23
+ constructor(schema) {
24
+ super(Node.MAP, schema);
25
+ this.items = [];
26
+ }
27
+ static get tagName() {
28
+ return 'tag:yaml.org,2002:map';
29
+ }
30
+ /**
31
+ * Adds a value to the collection.
32
+ *
33
+ * @param overwrite - If not set `true`, using a key that is already in the
34
+ * collection will throw. Otherwise, overwrites the previous value.
35
+ */
36
+ add(pair, overwrite) {
37
+ let _pair;
38
+ if (Node.isPair(pair))
39
+ _pair = pair;
40
+ else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
41
+ // In TypeScript, this never happens.
42
+ _pair = new Pair.Pair(pair, pair.value);
43
+ }
44
+ else
45
+ _pair = new Pair.Pair(pair.key, pair.value);
46
+ const prev = findPair(this.items, _pair.key);
47
+ const sortEntries = this.schema && this.schema.sortMapEntries;
48
+ if (prev) {
49
+ if (!overwrite)
50
+ throw new Error(`Key ${_pair.key} already set`);
51
+ // For scalars, keep the old node & its comments and anchors
52
+ if (Node.isScalar(prev.value) && Scalar.isScalarValue(_pair.value))
53
+ prev.value.value = _pair.value;
54
+ else
55
+ prev.value = _pair.value;
56
+ }
57
+ else if (sortEntries) {
58
+ const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
59
+ if (i === -1)
60
+ this.items.push(_pair);
61
+ else
62
+ this.items.splice(i, 0, _pair);
63
+ }
64
+ else {
65
+ this.items.push(_pair);
66
+ }
67
+ }
68
+ delete(key) {
69
+ const it = findPair(this.items, key);
70
+ if (!it)
71
+ return false;
72
+ const del = this.items.splice(this.items.indexOf(it), 1);
73
+ return del.length > 0;
74
+ }
75
+ get(key, keepScalar) {
76
+ const it = findPair(this.items, key);
77
+ const node = it && it.value;
78
+ return !keepScalar && Node.isScalar(node) ? node.value : node;
79
+ }
80
+ has(key) {
81
+ return !!findPair(this.items, key);
82
+ }
83
+ set(key, value) {
84
+ this.add(new Pair.Pair(key, value), true);
85
+ }
86
+ /**
87
+ * @param ctx - Conversion context, originally set in Document#toJS()
88
+ * @param {Class} Type - If set, forces the returned collection type
89
+ * @returns Instance of Type, Map, or Object
90
+ */
91
+ toJSON(_, ctx, Type) {
92
+ const map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
93
+ if (ctx && ctx.onCreate)
94
+ ctx.onCreate(map);
95
+ for (const item of this.items)
96
+ addPairToJSMap.addPairToJSMap(ctx, map, item);
97
+ return map;
98
+ }
99
+ toString(ctx, onComment, onChompKeep) {
100
+ if (!ctx)
101
+ return JSON.stringify(this);
102
+ for (const item of this.items) {
103
+ if (!Node.isPair(item))
104
+ throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
105
+ }
106
+ if (!ctx.allNullValues && this.hasAllNullValues(false))
107
+ ctx = Object.assign({}, ctx, { allNullValues: true });
108
+ return stringifyCollection.stringifyCollection(this, ctx, {
109
+ blockItem: n => n.str,
110
+ flowChars: { start: '{', end: '}' },
111
+ itemIndent: ctx.indent || '',
112
+ onChompKeep,
113
+ onComment
114
+ });
115
+ }
116
+ }
117
+
118
+ exports.YAMLMap = YAMLMap;
119
+ exports.findPair = findPair;
@@ -0,0 +1,53 @@
1
+ import type { Schema } from '../schema/Schema.js';
2
+ import type { StringifyContext } from '../stringify/stringify.js';
3
+ import { Collection } from './Collection.js';
4
+ import { ParsedNode, Range } from './Node.js';
5
+ import type { Pair } from './Pair.js';
6
+ import { ToJSContext } from './toJS.js';
7
+ export declare namespace YAMLSeq {
8
+ interface Parsed<T extends ParsedNode | Pair<ParsedNode, ParsedNode | null> = ParsedNode> extends YAMLSeq<T> {
9
+ items: T[];
10
+ range: Range;
11
+ }
12
+ }
13
+ export declare class YAMLSeq<T = unknown> extends Collection {
14
+ static get tagName(): 'tag:yaml.org,2002:seq';
15
+ items: T[];
16
+ constructor(schema?: Schema);
17
+ add(value: T): void;
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: unknown): boolean;
27
+ /**
28
+ * Returns item at `key`, or `undefined` if not found. By default unwraps
29
+ * scalar values from their surrounding node; to disable set `keepScalar` to
30
+ * `true` (collections are always returned intact).
31
+ *
32
+ * `key` must contain a representation of an integer for this to succeed.
33
+ * It may be wrapped in a `Scalar`.
34
+ */
35
+ get(key: unknown, keepScalar?: boolean): unknown;
36
+ /**
37
+ * Checks if the collection includes a value with the key `key`.
38
+ *
39
+ * `key` must contain a representation of an integer for this to succeed.
40
+ * It may be wrapped in a `Scalar`.
41
+ */
42
+ has(key: unknown): boolean;
43
+ /**
44
+ * Sets a value in this collection. For `!!set`, `value` needs to be a
45
+ * boolean to add/remove the item from the set.
46
+ *
47
+ * If `key` does not contain a representation of an integer, this will throw.
48
+ * It may be wrapped in a `Scalar`.
49
+ */
50
+ set(key: unknown, value: T): void;
51
+ toJSON(_?: unknown, ctx?: ToJSContext): unknown[];
52
+ toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
53
+ }
@@ -0,0 +1,107 @@
1
+ 'use strict';
2
+
3
+ var stringifyCollection = require('../stringify/stringifyCollection.js');
4
+ var Collection = require('./Collection.js');
5
+ var Node = require('./Node.js');
6
+ var Scalar = require('./Scalar.js');
7
+ var toJS = require('./toJS.js');
8
+
9
+ class YAMLSeq extends Collection.Collection {
10
+ constructor(schema) {
11
+ super(Node.SEQ, schema);
12
+ this.items = [];
13
+ }
14
+ static get tagName() {
15
+ return 'tag:yaml.org,2002:seq';
16
+ }
17
+ add(value) {
18
+ this.items.push(value);
19
+ }
20
+ /**
21
+ * Removes a value from the collection.
22
+ *
23
+ * `key` must contain a representation of an integer for this to succeed.
24
+ * It may be wrapped in a `Scalar`.
25
+ *
26
+ * @returns `true` if the item was found and removed.
27
+ */
28
+ delete(key) {
29
+ const idx = asItemIndex(key);
30
+ if (typeof idx !== 'number')
31
+ return false;
32
+ const del = this.items.splice(idx, 1);
33
+ return del.length > 0;
34
+ }
35
+ /**
36
+ * Returns item at `key`, or `undefined` if not found. By default unwraps
37
+ * scalar values from their surrounding node; to disable set `keepScalar` to
38
+ * `true` (collections are always returned intact).
39
+ *
40
+ * `key` must contain a representation of an integer for this to succeed.
41
+ * It may be wrapped in a `Scalar`.
42
+ */
43
+ get(key, keepScalar) {
44
+ const idx = asItemIndex(key);
45
+ if (typeof idx !== 'number')
46
+ return undefined;
47
+ const it = this.items[idx];
48
+ return !keepScalar && Node.isScalar(it) ? it.value : it;
49
+ }
50
+ /**
51
+ * Checks if the collection includes a value with the key `key`.
52
+ *
53
+ * `key` must contain a representation of an integer for this to succeed.
54
+ * It may be wrapped in a `Scalar`.
55
+ */
56
+ has(key) {
57
+ const idx = asItemIndex(key);
58
+ return typeof idx === 'number' && idx < this.items.length;
59
+ }
60
+ /**
61
+ * Sets a value in this collection. For `!!set`, `value` needs to be a
62
+ * boolean to add/remove the item from the set.
63
+ *
64
+ * If `key` does not contain a representation of an integer, this will throw.
65
+ * It may be wrapped in a `Scalar`.
66
+ */
67
+ set(key, value) {
68
+ const idx = asItemIndex(key);
69
+ if (typeof idx !== 'number')
70
+ throw new Error(`Expected a valid index, not ${key}.`);
71
+ const prev = this.items[idx];
72
+ if (Node.isScalar(prev) && Scalar.isScalarValue(value))
73
+ prev.value = value;
74
+ else
75
+ this.items[idx] = value;
76
+ }
77
+ toJSON(_, ctx) {
78
+ const seq = [];
79
+ if (ctx && ctx.onCreate)
80
+ ctx.onCreate(seq);
81
+ let i = 0;
82
+ for (const item of this.items)
83
+ seq.push(toJS.toJS(item, String(i++), ctx));
84
+ return seq;
85
+ }
86
+ toString(ctx, onComment, onChompKeep) {
87
+ if (!ctx)
88
+ return JSON.stringify(this);
89
+ return stringifyCollection.stringifyCollection(this, ctx, {
90
+ blockItem: n => (n.comment ? n.str : `- ${n.str}`),
91
+ flowChars: { start: '[', end: ']' },
92
+ itemIndent: (ctx.indent || '') + ' ',
93
+ onChompKeep,
94
+ onComment
95
+ });
96
+ }
97
+ }
98
+ function asItemIndex(key) {
99
+ let idx = Node.isScalar(key) ? key.value : key;
100
+ if (idx && typeof idx === 'string')
101
+ idx = Number(idx);
102
+ return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
103
+ ? idx
104
+ : null;
105
+ }
106
+
107
+ exports.YAMLSeq = YAMLSeq;
@@ -0,0 +1,3 @@
1
+ import type { Pair } from './Pair.js';
2
+ import { ToJSContext } from './toJS.js';
3
+ export declare function addPairToJSMap(ctx: ToJSContext | undefined, map: Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>, { key, value }: Pair): Map<unknown, unknown> | Set<unknown> | Record<string | number | symbol, unknown>;
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+
3
+ var log = require('../log.js');
4
+ var stringify = require('../stringify/stringify.js');
5
+ var Node = require('./Node.js');
6
+ var Scalar = require('./Scalar.js');
7
+ var toJS = require('./toJS.js');
8
+
9
+ const MERGE_KEY = '<<';
10
+ function addPairToJSMap(ctx, map, { key, value }) {
11
+ if (ctx && ctx.doc.schema.merge && isMergeKey(key)) {
12
+ if (Node.isSeq(value))
13
+ for (const it of value.items)
14
+ mergeToJSMap(ctx, map, it);
15
+ else if (Array.isArray(value))
16
+ for (const it of value)
17
+ mergeToJSMap(ctx, map, it);
18
+ else
19
+ mergeToJSMap(ctx, map, value);
20
+ }
21
+ else {
22
+ const jsKey = toJS.toJS(key, '', ctx);
23
+ if (map instanceof Map) {
24
+ map.set(jsKey, toJS.toJS(value, jsKey, ctx));
25
+ }
26
+ else if (map instanceof Set) {
27
+ map.add(jsKey);
28
+ }
29
+ else {
30
+ const stringKey = stringifyKey(key, jsKey, ctx);
31
+ const jsValue = toJS.toJS(value, stringKey, ctx);
32
+ if (stringKey in map)
33
+ Object.defineProperty(map, stringKey, {
34
+ value: jsValue,
35
+ writable: true,
36
+ enumerable: true,
37
+ configurable: true
38
+ });
39
+ else
40
+ map[stringKey] = jsValue;
41
+ }
42
+ }
43
+ return map;
44
+ }
45
+ const isMergeKey = (key) => key === MERGE_KEY ||
46
+ (Node.isScalar(key) &&
47
+ key.value === MERGE_KEY &&
48
+ (!key.type || key.type === Scalar.Scalar.PLAIN));
49
+ // If the value associated with a merge key is a single mapping node, each of
50
+ // its key/value pairs is inserted into the current mapping, unless the key
51
+ // already exists in it. If the value associated with the merge key is a
52
+ // sequence, then this sequence is expected to contain mapping nodes and each
53
+ // of these nodes is merged in turn according to its order in the sequence.
54
+ // Keys in mapping nodes earlier in the sequence override keys specified in
55
+ // later mapping nodes. -- http://yaml.org/type/merge.html
56
+ function mergeToJSMap(ctx, map, value) {
57
+ const source = ctx && Node.isAlias(value) ? value.resolve(ctx.doc) : null;
58
+ if (!Node.isMap(source))
59
+ throw new Error('Merge sources must be map aliases');
60
+ const srcMap = source.toJSON(null, ctx, Map);
61
+ for (const [key, value] of srcMap) {
62
+ if (map instanceof Map) {
63
+ if (!map.has(key))
64
+ map.set(key, value);
65
+ }
66
+ else if (map instanceof Set) {
67
+ map.add(key);
68
+ }
69
+ else if (!Object.prototype.hasOwnProperty.call(map, key)) {
70
+ Object.defineProperty(map, key, {
71
+ value,
72
+ writable: true,
73
+ enumerable: true,
74
+ configurable: true
75
+ });
76
+ }
77
+ }
78
+ return map;
79
+ }
80
+ function stringifyKey(key, jsKey, ctx) {
81
+ if (jsKey === null)
82
+ return '';
83
+ if (typeof jsKey !== 'object')
84
+ return String(jsKey);
85
+ if (Node.isNode(key) && ctx && ctx.doc) {
86
+ const strCtx = stringify.createStringifyContext(ctx.doc, {});
87
+ strCtx.anchors = new Set();
88
+ for (const node of ctx.anchors.keys())
89
+ strCtx.anchors.add(node.anchor);
90
+ strCtx.inFlow = true;
91
+ strCtx.inStringifyKey = true;
92
+ const strKey = key.toString(strCtx);
93
+ if (!ctx.mapKeyWarned) {
94
+ let jsonStr = JSON.stringify(strKey);
95
+ if (jsonStr.length > 40)
96
+ jsonStr = jsonStr.substring(0, 36) + '..."';
97
+ log.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.`);
98
+ ctx.mapKeyWarned = true;
99
+ }
100
+ return strKey;
101
+ }
102
+ return JSON.stringify(jsKey);
103
+ }
104
+
105
+ exports.addPairToJSMap = addPairToJSMap;
@@ -0,0 +1,30 @@
1
+ import type { Document } from '../doc/Document.js';
2
+ import type { stringify } from '../stringify/stringify.js';
3
+ import { Node } from './Node.js';
4
+ export interface AnchorData {
5
+ aliasCount: number;
6
+ count: number;
7
+ res: unknown;
8
+ }
9
+ export interface ToJSContext {
10
+ anchors: Map<Node, AnchorData>;
11
+ doc: Document;
12
+ keep: boolean;
13
+ mapAsMap: boolean;
14
+ mapKeyWarned: boolean;
15
+ maxAliasCount: number;
16
+ onCreate?: (res: unknown) => void;
17
+ /** Requiring this directly in Pair would create circular dependencies */
18
+ stringify: typeof stringify;
19
+ }
20
+ /**
21
+ * Recursively convert any node or its contents to native JavaScript
22
+ *
23
+ * @param value - The input value
24
+ * @param arg - If `value` defines a `toJSON()` method, use this
25
+ * as its first argument
26
+ * @param ctx - Conversion context, originally set in Document#toJS(). If
27
+ * `{ keep: true }` is not set, output should be suitable for JSON
28
+ * stringification.
29
+ */
30
+ export declare function toJS(value: any, arg: string | null, ctx?: ToJSContext): any;
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ var Node = require('./Node.js');
4
+
5
+ /**
6
+ * Recursively convert any node or its contents to native JavaScript
7
+ *
8
+ * @param value - The input value
9
+ * @param arg - If `value` defines a `toJSON()` method, use this
10
+ * as its first argument
11
+ * @param ctx - Conversion context, originally set in Document#toJS(). If
12
+ * `{ keep: true }` is not set, output should be suitable for JSON
13
+ * stringification.
14
+ */
15
+ function toJS(value, arg, ctx) {
16
+ if (Array.isArray(value))
17
+ return value.map((v, i) => toJS(v, String(i), ctx));
18
+ if (value && typeof value.toJSON === 'function') {
19
+ if (!ctx || !Node.hasAnchor(value))
20
+ return value.toJSON(arg, ctx);
21
+ const data = { aliasCount: 0, count: 1, res: undefined };
22
+ ctx.anchors.set(value, data);
23
+ ctx.onCreate = res => {
24
+ data.res = res;
25
+ delete ctx.onCreate;
26
+ };
27
+ const res = value.toJSON(arg, ctx);
28
+ if (ctx.onCreate)
29
+ ctx.onCreate(res);
30
+ return res;
31
+ }
32
+ if (typeof value === 'bigint' && !(ctx && ctx.keep))
33
+ return Number(value);
34
+ return value;
35
+ }
36
+
37
+ exports.toJS = toJS;