How to use the parseAllDocuments function from yaml

Find comprehensive JavaScript yaml.parseAllDocuments code examples handpicked from public code repositorys.

yaml.parseAllDocuments is a function that parses multiple YAML documents from a string and returns an array of JavaScript objects representing each document.

22
23
24
25
26
27
28
29
30
31
    method: 'GET',
  },
);

const txt = await response.text();
const manifest = YAML.parseAllDocuments(txt);

const [chartRef, pipelineRunner] = await Promise.all([
  jq.run(
    '..|objects| select(.metadata != null) | select( .metadata.name | contains("pipeline")) | .spec.chart.ref//empty',
fork icon21
star icon23
watch icon0

+ 2 other calls in file

252
253
254
255
256
257
258
259
260

// run the helm template command to generate the component envelope
const output = execSync('helm template ' + releaseName + ' ' + testDataFolder + componentHelmChart, { encoding: 'utf-8' });  
  
// parse the template
documentArray = YAML.parseAllDocuments(output)

// assert that the documentArray is an array
assert.equal(Array.isArray(documentArray), true, "The file should contain at least one YAML document")
fork icon13
star icon8
watch icon0

+ 4 other calls in file

How does yaml.parseAllDocuments work?

yaml.parseAllDocuments works by taking a YAML string as input and parsing each YAML document in the string using the YAML parsing rules, which define how YAML syntax is mapped to JavaScript objects. The resulting JavaScript objects are collected into an array, with one array element for each parsed YAML document. The parsing process includes steps such as lexing, parsing, and interpreting the YAML syntax, and converting it to the corresponding JavaScript objects based on the YAML type system. The resulting JavaScript objects can be used in JavaScript applications to represent structured data that was originally stored in YAML format.

26
27
28
29
30
31
32
33
34
35
  expect(nameArray[nameArray.length - 1], "Filename should end '.component.yaml'").to.equal('yaml')
  done()
})

it('Valid YAML document(s)', function (done) { // check that the file contains 1 or more YAML documents and that documents parse with zero errors
  documentArray = YAML.parseAllDocuments(file)
  expect(documentArray, 'The file should contain at least one YAML document.').to.be.a('array')
  expect(documentArray.length, 'The file should contain at least one YAML document.').to.be.greaterThan(0)
  // go through each document checking for errors
  for (const docKey in documentArray) {
fork icon4
star icon6
watch icon0

+ 11 other calls in file

32
33
34
35
36
37
38
39
40
41
child.on('exit', (code) => {
    // console.log(stdout)
    // console.log(stderr)
    // console.log('exit', code)
    if (code === 0) {
        const documents = yaml.parseAllDocuments(stdout)
        const docs = documents.map(d => d.toJS())
        resolve({
            docs
        })
fork icon6
star icon4
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const yaml = require("js-yaml");

const yamlStr = `
---
name: Alice
age: 30
---
name: Bob
age: 35
`;

const docs = yaml.parseAllDocuments(yamlStr);

console.log(docs[0]); // { name: 'Alice', age: 30 }
console.log(docs[1]); // { name: 'Bob', age: 35 }

In this example, we define a YAML string containing two YAML documents, separated by ---. We pass this string to yaml.parseAllDocuments to parse both documents, and store the resulting array of JavaScript objects in the docs variable. We then print the contents of the first and second documents to the console using console.log. The output will be: css Copy code

18
19
20
21
22
23
24
25
26
27
28
29
30


(async () => {
    const scores = await readFile(`${__dirname}/../data/scores.yaml`, "utf8");
    const results = [];


    parseAllDocuments(scores).forEach(doc => {
        const {date, games} = doc.toJS();
        const [year, month, day] = date.split("-").map(s => +s);


        results.push(
fork icon0
star icon1
watch icon0

+ 2 other calls in file