mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
docs(encoding/yaml): add usage document (denoland/deno#3529)
This commit is contained in:
parent
e24c39a7f0
commit
c0485bf863
@ -226,17 +226,76 @@ YAML parser / dumper for Deno
|
||||
|
||||
Heavily inspired from [js-yaml]
|
||||
|
||||
### Example
|
||||
### Basic usage
|
||||
|
||||
See [`./yaml/example`](./yaml/example) folder and [js-yaml] repository.
|
||||
`parse` parses the yaml string, and `stringify` dumps the given object to YAML
|
||||
string.
|
||||
|
||||
```ts
|
||||
import { parse, stringify } from "https://deno.land/std/encoding/yaml.ts";
|
||||
|
||||
const data = parse(`
|
||||
foo: bar
|
||||
baz:
|
||||
- qux
|
||||
- quux
|
||||
`);
|
||||
console.log(data);
|
||||
// => { foo: "bar", baz: [ "qux", "quux" ] }
|
||||
|
||||
const yaml = stringify({ foo: "bar", baz: ["qux", "quux"] });
|
||||
console.log(yaml);
|
||||
// =>
|
||||
// foo: bar
|
||||
// baz:
|
||||
// - qux
|
||||
// - quux
|
||||
```
|
||||
|
||||
If your YAML contains multiple documents in it, you can use `parseAll` for
|
||||
handling it.
|
||||
|
||||
```ts
|
||||
import { parseAll } from "https://deno.land/std/encoding/yaml.ts";
|
||||
|
||||
const data = parseAll(`
|
||||
---
|
||||
id: 1
|
||||
name: Alice
|
||||
---
|
||||
id: 2
|
||||
name: Bob
|
||||
---
|
||||
id: 3
|
||||
name: Eve
|
||||
`);
|
||||
console.log(data);
|
||||
// => [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" } ]
|
||||
// TODO(kt3k): This doesn't work now
|
||||
```
|
||||
|
||||
### API
|
||||
|
||||
#### `parse(str: string, opts?: ParserOption): unknown`
|
||||
|
||||
Parses the YAML string with a single document.
|
||||
|
||||
#### `parseAll(str: string, iterator?: Function, opts?: ParserOption): unknown`
|
||||
|
||||
Parses the YAML string with multiple documents. If the iterator is given, it's
|
||||
applied to every document instead of returning the array of parsed objects.
|
||||
|
||||
#### `stringify(obj: object, opts?: DumpOption): string`
|
||||
|
||||
Serializes `object` as a YAML document.
|
||||
|
||||
### :warning: Limitations
|
||||
|
||||
- `binary` type is currently not stable
|
||||
- `function`, `regexp`, and `undefined` type are currently not supported
|
||||
|
||||
# Basic usage
|
||||
### More example
|
||||
|
||||
TBD
|
||||
See [`./yaml/example`](./yaml/example) folder and [js-yaml] repository.
|
||||
|
||||
[js-yaml]: https://github.com/nodeca/js-yaml
|
||||
|
Loading…
Reference in New Issue
Block a user