Skip to content

Commit 9411d09

Browse files
kt3kiuioiua
andauthored
docs(yaml): improve yaml document (denoland#5127)
Co-authored-by: Asher Gomez <[email protected]>
1 parent f3ef2b1 commit 9411d09

File tree

4 files changed

+85
-17
lines changed

4 files changed

+85
-17
lines changed

_tools/check_docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ const ENTRY_POINTS = [
7171
"../url/mod.ts",
7272
"../uuid/mod.ts",
7373
"../webgpu/mod.ts",
74+
"../yaml/parse.ts",
75+
"../yaml/stringify.ts",
7476
] as const;
7577

7678
const TS_SNIPPET = /```ts[\s\S]*?```/g;

yaml/parse.ts

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
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";
98
import { 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
*/
4057
export 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
*/
6993
export 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+
*/
74125
export function parseAll(content: string, options?: ParseOptions): unknown;
75126
export 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") {

yaml/schema/mod.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
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";
87
import { CORE_SCHEMA } from "./core.ts";
98
import { DEFAULT_SCHEMA } from "./default.ts";
109
import { EXTENDED_SCHEMA } from "./extended.ts";
@@ -19,7 +18,9 @@ export {
1918
};
2019

2120
export function replaceSchemaNameWithSchemaClass(
22-
options?: { schema?: string | Schema },
21+
options?: {
22+
schema?: "core" | "default" | "failsafe" | "json" | "extended" | unknown;
23+
},
2324
) {
2425
switch (options?.schema) {
2526
case "core":

yaml/stringify.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
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";
87
import { dump } from "./_dumper/dumper.ts";
98
import { replaceSchemaNameWithSchemaClass } from "./mod.ts";
109

@@ -33,7 +32,7 @@ export type DumpOptions = {
3332
*
3433
* Schema class or its name.
3534
*/
36-
schema?: string | Schema;
35+
schema?: "core" | "default" | "failsafe" | "json" | "extended" | unknown;
3736
/**
3837
* If true, sort keys when dumping YAML in ascending, ASCII character order.
3938
* If a function, use the function to sort the keys. (default: false)
@@ -68,6 +67,21 @@ export type DumpOptions = {
6867
* Serializes `data` as a YAML document.
6968
*
7069
* You can disable exceptions by setting the skipInvalid option to true.
70+
*
71+
* @example Usage
72+
* ```ts
73+
* import { stringify } from "@std/yaml/stringify";
74+
* import { assertEquals } from "@std/assert/assert-equals";
75+
*
76+
* const data = { id: 1, name: "Alice" };
77+
* const yaml = stringify(data);
78+
*
79+
* assertEquals(yaml, "id: 1\nname: Alice\n");
80+
* ```
81+
*
82+
* @param data The data to serialize.
83+
* @param options The options for serialization.
84+
* @returns A YAML string.
7185
*/
7286
export function stringify(
7387
data: unknown,

0 commit comments

Comments
 (0)