-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
public-api.ts
231 lines (217 loc) · 6.86 KB
/
public-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { Composer } from './compose/composer.js'
import type { Reviver } from './doc/applyReviver.js'
import { Document, Replacer } from './doc/Document.js'
import { prettifyError, YAMLParseError } from './errors.js'
import { warn } from './log.js'
import { isDocument } from './nodes/identity.js'
import type { Node, ParsedNode } from './nodes/Node.js'
import type {
CreateNodeOptions,
DocumentOptions,
ParseOptions,
SchemaOptions,
ToJSOptions,
ToStringOptions
} from './options.js'
import { LineCounter } from './parse/line-counter.js'
import { Parser } from './parse/parser.js'
export interface EmptyStream
extends Array<Document.Parsed>,
ReturnType<Composer['streamInfo']> {
empty: true
}
function parseOptions(options: ParseOptions) {
const prettyErrors = options.prettyErrors !== false
const lineCounter =
options.lineCounter || (prettyErrors && new LineCounter()) || null
return { lineCounter, prettyErrors }
}
/**
* Parse the input as a stream of YAML documents.
*
* Documents should be separated from each other by `...` or `---` marker lines.
*
* @returns If an empty `docs` array is returned, it will be of type
* EmptyStream and contain additional stream information. In
* TypeScript, you should use `'empty' in docs` as a type guard for it.
*/
export function parseAllDocuments<
Contents extends Node = ParsedNode,
Strict extends boolean = true
>(
source: string,
options: ParseOptions & DocumentOptions & SchemaOptions = {}
):
| Array<
Contents extends ParsedNode
? Document.Parsed<Contents, Strict>
: Document<Contents, Strict>
>
| EmptyStream {
const { lineCounter, prettyErrors } = parseOptions(options)
const parser = new Parser(lineCounter?.addNewLine)
const composer = new Composer(options)
const docs = Array.from(composer.compose(parser.parse(source)))
if (prettyErrors && lineCounter)
for (const doc of docs) {
doc.errors.forEach(prettifyError(source, lineCounter))
doc.warnings.forEach(prettifyError(source, lineCounter))
}
type DocType = Contents extends ParsedNode
? Document.Parsed<Contents, Strict>
: Document<Contents, Strict>
if (docs.length > 0) return docs as DocType[]
return Object.assign<
DocType[],
{ empty: true },
ReturnType<Composer['streamInfo']>
>([], { empty: true }, composer.streamInfo())
}
/** Parse an input string into a single YAML.Document */
export function parseDocument<
Contents extends Node = ParsedNode,
Strict extends boolean = true
>(
source: string,
options: ParseOptions & DocumentOptions & SchemaOptions = {}
): Contents extends ParsedNode
? Document.Parsed<Contents, Strict>
: Document<Contents, Strict> {
const { lineCounter, prettyErrors } = parseOptions(options)
const parser = new Parser(lineCounter?.addNewLine)
const composer = new Composer(options)
type DocType = Contents extends ParsedNode
? Document.Parsed<Contents, Strict>
: Document<Contents, Strict>
// `doc` is always set by compose.end(true) at the very latest
let doc: DocType = null as any
for (const _doc of composer.compose(
parser.parse(source),
true,
source.length
)) {
if (!doc) doc = _doc as DocType
else if (doc.options.logLevel !== 'silent') {
doc.errors.push(
new YAMLParseError(
_doc.range.slice(0, 2) as [number, number],
'MULTIPLE_DOCS',
'Source contains multiple documents; please use YAML.parseAllDocuments()'
)
)
break
}
}
if (prettyErrors && lineCounter) {
doc.errors.forEach(prettifyError(source, lineCounter))
doc.warnings.forEach(prettifyError(source, lineCounter))
}
return doc
}
/**
* Parse an input string into JavaScript.
*
* Only supports input consisting of a single YAML document; for multi-document
* support you should use `YAML.parseAllDocuments`. May throw on error, and may
* log warnings using `console.warn`.
*
* @param str - A string with YAML formatting.
* @param reviver - A reviver function, as in `JSON.parse()`
* @returns The value will match the type of the root value of the parsed YAML
* document, so Maps become objects, Sequences arrays, and scalars result in
* nulls, booleans, numbers and strings.
*/
export function parse(
src: string,
options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
): any
export function parse(
src: string,
reviver: Reviver,
options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
): any
export function parse(
src: string,
reviver?:
| Reviver
| (ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions),
options?: ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions
): any {
let _reviver: Reviver | undefined = undefined
if (typeof reviver === 'function') {
_reviver = reviver
} else if (options === undefined && reviver && typeof reviver === 'object') {
options = reviver
}
const doc = parseDocument(src, options)
if (!doc) return null
doc.warnings.forEach(warning => warn(doc.options.logLevel, warning))
if (doc.errors.length > 0) {
if (doc.options.logLevel !== 'silent') throw doc.errors[0]
else doc.errors = []
}
return doc.toJS(Object.assign({ reviver: _reviver }, options))
}
/**
* Stringify a value as a YAML document.
*
* @param replacer - A replacer array or function, as in `JSON.stringify()`
* @returns Will always include `\n` as the last character, as is expected of YAML documents.
*/
export function stringify(
value: any,
options?: DocumentOptions &
SchemaOptions &
ParseOptions &
CreateNodeOptions &
ToStringOptions
): string
export function stringify(
value: any,
replacer?: Replacer | null,
options?:
| string
| number
| (DocumentOptions &
SchemaOptions &
ParseOptions &
CreateNodeOptions &
ToStringOptions)
): string
export function stringify(
value: any,
replacer?:
| Replacer
| (DocumentOptions &
SchemaOptions &
ParseOptions &
CreateNodeOptions &
ToStringOptions)
| null,
options?:
| string
| number
| (DocumentOptions &
SchemaOptions &
ParseOptions &
CreateNodeOptions &
ToStringOptions)
) {
let _replacer: Replacer | null = null
if (typeof replacer === 'function' || Array.isArray(replacer)) {
_replacer = replacer
} else if (options === undefined && replacer) {
options = replacer
}
if (typeof options === 'string') options = options.length
if (typeof options === 'number') {
const indent = Math.round(options)
options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent }
}
if (value === undefined) {
const { keepUndefined } = options ?? (replacer as CreateNodeOptions) ?? {}
if (!keepUndefined) return undefined
}
if (isDocument(value) && !_replacer) return value.toString(options)
return new Document(value, _replacer, options).toString(options)
}