44// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
55// This module is browser compatible.
66
7- import type { Schema } from "./schema.ts" ;
8- import { type CbFunction , load , loadAll } from "./_loader/loader.ts" ;
7+ import { load , loadAll } from "./_loader/loader.ts" ;
98import { replaceSchemaNameWithSchemaClass } from "./mod.ts" ;
109
1110/**
@@ -24,18 +23,36 @@ export interface ParseOptions {
2423 *
2524 * Schema class or its name.
2625 */
27- schema ?: string | Schema ;
26+ schema ?: "core" | "default" | "failsafe" | "json" | "extended" | unknown ;
2827 /** compatibility with JSON.parse behaviour. */
2928 json ?: boolean ;
3029 /** function to call on warning messages. */
3130 onWarning ?( this : null , e ?: Error ) : void ;
3231}
3332
3433/**
35- * Parses `content` as single YAML document.
34+ * Parse `content` as single YAML document, and return it .
3635 *
37- * Returns a JavaScript object or throws `YAMLError` on error.
38- * By default, does not support regexps, functions and undefined. This method is safe for untrusted data.
36+ * This function does not support regexps, functions, and undefined by default.
37+ * This method is safe for parsing untrusted data.
38+ *
39+ * @example Usage
40+ * ```ts
41+ * import { parse } from "@std/yaml/parse";
42+ * import { assertEquals } from "@std/assert/assert-equals";
43+ *
44+ * const data = parse(`
45+ * id: 1
46+ * name: Alice
47+ * `);
48+ *
49+ * assertEquals(data, { id: 1, name: "Alice" });
50+ * ```
51+ *
52+ * @throws {YAMLError } Throws error on invalid YAML.
53+ * @param content YAML string to parse.
54+ * @param options Parsing options.
55+ * @returns Parsed document.
3956 */
4057export function parse ( content : string , options ?: ParseOptions ) : unknown {
4158 replaceSchemaNameWithSchemaClass ( options ) ;
@@ -47,11 +64,12 @@ export function parse(content: string, options?: ParseOptions): unknown {
4764 * Same as `parse()`, but understands multi-document sources.
4865 * Applies iterator to each document if specified, or returns array of documents.
4966 *
50- * @example
67+ * @example Usage
5168 * ```ts
5269 * import { parseAll } from "@std/yaml/parse";
70+ * import { assertEquals } from "@std/assert/assert-equals";
5371 *
54- * const data = parseAll(`
72+ * parseAll(`
5573 * ---
5674 * id: 1
5775 * name: Alice
@@ -61,20 +79,53 @@ export function parse(content: string, options?: ParseOptions): unknown {
6179 * ---
6280 * id: 3
6381 * name: Eve
64- * `);
65- * console.log(data);
66- * // => [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" } ]
82+ * `, (doc: any) => {
83+ * assertEquals(typeof doc, "object");
84+ * assertEquals(typeof doc.id, "number");
85+ * assertEquals(typeof doc.name, "string");
86+ * });
6787 * ```
88+ *
89+ * @param content YAML string to parse.
90+ * @param iterator Function to call on each document.
91+ * @param options Parsing options.
6892 */
6993export function parseAll (
7094 content : string ,
71- iterator : CbFunction ,
95+ iterator : ( doc : unknown ) => void ,
7296 options ?: ParseOptions ,
7397) : void ;
98+ /**
99+ * Same as `parse()`, but understands multi-document sources.
100+ * Applies iterator to each document if specified, or returns array of documents.
101+ *
102+ * @example Usage
103+ * ```ts
104+ * import { parseAll } from "@std/yaml/parse";
105+ * import { assertEquals } from "@std/assert/assert-equals";
106+ *
107+ * const data = parseAll(`
108+ * ---
109+ * id: 1
110+ * name: Alice
111+ * ---
112+ * id: 2
113+ * name: Bob
114+ * ---
115+ * id: 3
116+ * name: Eve
117+ * `);
118+ * assertEquals(data, [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" }]);
119+ * ```
120+ *
121+ * @param content YAML string to parse.
122+ * @param options Parsing options.
123+ * @returns Array of parsed documents.
124+ */
74125export function parseAll ( content : string , options ?: ParseOptions ) : unknown ;
75126export function parseAll (
76127 content : string ,
77- iteratorOrOption ?: CbFunction | ParseOptions ,
128+ iteratorOrOption ?: ( ( doc : unknown ) => void ) | ParseOptions ,
78129 options ?: ParseOptions ,
79130) : unknown {
80131 if ( typeof iteratorOrOption !== "function" ) {
0 commit comments