summaryrefslogtreecommitdiff
path: root/std/encoding
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2019-12-21 17:57:51 +0900
committerRy Dahl <ry@tinyclouds.org>2019-12-21 03:57:51 -0500
commit80da2ac8dea9f2e77bd81273530b8f7ff42d8a93 (patch)
treeebba5d39d2f5ae2a1531280b3af7210b28e8a80d /std/encoding
parentb7b0668c782dd4bc92237d91116f033e57536238 (diff)
fix(std/encoding/yaml): support document separator in parseAll (#3535)
Diffstat (limited to 'std/encoding')
-rw-r--r--std/encoding/README.md1
-rw-r--r--std/encoding/yaml/loader/loader.ts2
-rw-r--r--std/encoding/yaml/parse_test.ts54
-rw-r--r--std/encoding/yaml/utils.ts4
4 files changed, 44 insertions, 17 deletions
diff --git a/std/encoding/README.md b/std/encoding/README.md
index ee06aa3ec..6f0704b04 100644
--- a/std/encoding/README.md
+++ b/std/encoding/README.md
@@ -192,7 +192,6 @@ 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
diff --git a/std/encoding/yaml/loader/loader.ts b/std/encoding/yaml/loader/loader.ts
index e06581b7e..a81395a8e 100644
--- a/std/encoding/yaml/loader/loader.ts
+++ b/std/encoding/yaml/loader/loader.ts
@@ -577,7 +577,7 @@ function readPlainScalar(
captureSegment(state, captureStart, captureEnd, false);
- if (!common.isNullOrUndefined(state.result)) {
+ if (state.result) {
return true;
}
diff --git a/std/encoding/yaml/parse_test.ts b/std/encoding/yaml/parse_test.ts
index 029665e44..ec1b386f9 100644
--- a/std/encoding/yaml/parse_test.ts
+++ b/std/encoding/yaml/parse_test.ts
@@ -3,23 +3,55 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { parse } from "./parse.ts";
+import { parse, parseAll } from "./parse.ts";
import { test } from "../../testing/mod.ts";
import { assertEquals } from "../../testing/asserts.ts";
test({
- name: "parsed correctly",
+ name: "`parse` parses single document yaml string",
fn(): void {
- const FIXTURE = `
- test: toto
- foo:
- bar: True
- baz: 1
- qux: ~
- `;
+ const yaml = `
+ test: toto
+ foo:
+ bar: True
+ baz: 1
+ qux: ~
+ `;
- const ASSERTS = { test: "toto", foo: { bar: true, baz: 1, qux: null } };
+ const expected = { test: "toto", foo: { bar: true, baz: 1, qux: null } };
- assertEquals(parse(FIXTURE), ASSERTS);
+ assertEquals(parse(yaml), expected);
+ }
+});
+
+test({
+ name: "`parseAll` parses the yaml string with multiple documents",
+ fn(): void {
+ const yaml = `
+---
+id: 1
+name: Alice
+---
+id: 2
+name: Bob
+---
+id: 3
+name: Eve
+ `;
+ const expected = [
+ {
+ id: 1,
+ name: "Alice"
+ },
+ {
+ id: 2,
+ name: "Bob"
+ },
+ {
+ id: 3,
+ name: "Eve"
+ }
+ ];
+ assertEquals(parseAll(yaml), expected);
}
});
diff --git a/std/encoding/yaml/utils.ts b/std/encoding/yaml/utils.ts
index 2f4ea00b5..0f2d7abe7 100644
--- a/std/encoding/yaml/utils.ts
+++ b/std/encoding/yaml/utils.ts
@@ -22,10 +22,6 @@ export function isNull(value: unknown): value is null {
return value === null;
}
-export function isNullOrUndefined(value: unknown): value is null | undefined {
- return value === null || value === undefined;
-}
-
export function isNumber(value: unknown): value is number {
return typeof value === "number" || value instanceof Number;
}