|
| 1 | +// Ported from js-yaml v3.13.1: |
| 2 | +// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da |
| 3 | +// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. |
| 4 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 5 | +// This module is browser compatible. |
| 6 | + |
| 7 | +import { isEOL } from "./_chars.ts"; |
| 8 | +import { LoaderState } from "./_loader_state.ts"; |
| 9 | +import type { ParseOptions as StableParseOptions } from "./parse.ts"; |
| 10 | +import { getSchema, type ImplicitType, type SchemaType } from "./_schema.ts"; |
| 11 | +import type { KindType, RepresentFn, Type } from "./_type.ts"; |
| 12 | + |
| 13 | +export type { ImplicitType, KindType, RepresentFn, SchemaType, Type }; |
| 14 | + |
| 15 | +/** Options for {@linkcode parse}. */ |
| 16 | +export type ParseOptions = StableParseOptions & { |
| 17 | + /** |
| 18 | + * Extra types to be added to the schema. |
| 19 | + */ |
| 20 | + extraTypes?: ImplicitType[]; |
| 21 | +}; |
| 22 | + |
| 23 | +function sanitizeInput(input: string) { |
| 24 | + input = String(input); |
| 25 | + |
| 26 | + if (input.length > 0) { |
| 27 | + // Add trailing `\n` if not exists |
| 28 | + if (!isEOL(input.charCodeAt(input.length - 1))) input += "\n"; |
| 29 | + |
| 30 | + // Strip BOM |
| 31 | + if (input.charCodeAt(0) === 0xfeff) input = input.slice(1); |
| 32 | + } |
| 33 | + |
| 34 | + // Use 0 as string terminator. That significantly simplifies bounds check. |
| 35 | + input += "\0"; |
| 36 | + |
| 37 | + return input; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Parse and return a YAML string as a parsed YAML document object. |
| 42 | + * |
| 43 | + * Note: This does not support functions. Untrusted data is safe to parse. |
| 44 | + * |
| 45 | + * @example Usage |
| 46 | + * ```ts |
| 47 | + * import { parse } from "@std/yaml/parse"; |
| 48 | + * import { assertEquals } from "@std/assert"; |
| 49 | + * |
| 50 | + * const data = parse(` |
| 51 | + * id: 1 |
| 52 | + * name: Alice |
| 53 | + * `); |
| 54 | + * |
| 55 | + * assertEquals(data, { id: 1, name: "Alice" }); |
| 56 | + * ``` |
| 57 | + * |
| 58 | + * @throws {SyntaxError} Throws error on invalid YAML. |
| 59 | + * @param content YAML string to parse. |
| 60 | + * @param options Parsing options. |
| 61 | + * @returns Parsed document. |
| 62 | + */ |
| 63 | +export function parse( |
| 64 | + content: string, |
| 65 | + options: ParseOptions = {}, |
| 66 | +): unknown { |
| 67 | + content = sanitizeInput(content); |
| 68 | + const state = new LoaderState(content, { |
| 69 | + ...options, |
| 70 | + schema: getSchema(options.schema, options.extraTypes), |
| 71 | + }); |
| 72 | + const documentGenerator = state.readDocuments(); |
| 73 | + const document = documentGenerator.next().value; |
| 74 | + if (!documentGenerator.next().done) { |
| 75 | + throw new SyntaxError( |
| 76 | + "Found more than 1 document in the stream: expected a single document", |
| 77 | + ); |
| 78 | + } |
| 79 | + return document ?? null; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Same as {@linkcode parse}, but understands multi-document YAML sources, and |
| 84 | + * returns multiple parsed YAML document objects. |
| 85 | + * |
| 86 | + * @example Usage |
| 87 | + * ```ts |
| 88 | + * import { parseAll } from "@std/yaml/parse"; |
| 89 | + * import { assertEquals } from "@std/assert"; |
| 90 | + * |
| 91 | + * const data = parseAll(` |
| 92 | + * --- |
| 93 | + * id: 1 |
| 94 | + * name: Alice |
| 95 | + * --- |
| 96 | + * id: 2 |
| 97 | + * name: Bob |
| 98 | + * --- |
| 99 | + * id: 3 |
| 100 | + * name: Eve |
| 101 | + * `); |
| 102 | + * assertEquals(data, [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Eve" }]); |
| 103 | + * ``` |
| 104 | + * |
| 105 | + * @param content YAML string to parse. |
| 106 | + * @param options Parsing options. |
| 107 | + * @returns Array of parsed documents. |
| 108 | + */ |
| 109 | +export function parseAll(content: string, options: ParseOptions = {}): unknown { |
| 110 | + content = sanitizeInput(content); |
| 111 | + const state = new LoaderState(content, { |
| 112 | + ...options, |
| 113 | + schema: getSchema(options.schema, options.extraTypes), |
| 114 | + }); |
| 115 | + return [...state.readDocuments()]; |
| 116 | +} |
0 commit comments