How to use the load function from js-yaml

Find comprehensive JavaScript js-yaml.load code examples handpicked from public code repositorys.

js-yaml.load is a function that parses a YAML string into a JavaScript object.

105
106
107
108
109
110
111
112
113
if (!data && !current_cfg) {
  return;
}

if (data) {
  let config = yaml.load(data, { filename: cfg_path });

  // Normalize config - convert all types to array
  config.type = config.type || {};
fork icon851
star icon0
watch icon246

56
57
58
59
60
61
62
63
64
65
const yaml = require('js-yaml');
const fs   = require('fs');

// Get document, or throw exception on error
try {
  const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8'));
  console.log(doc);
} catch (e) {
  console.log(e);
}
fork icon793
star icon0
watch icon77

How does js-yaml.load work?

js-yaml.load is a function provided by the js-yaml library that takes a YAML string as input and returns a JavaScript object. The function uses a parser to convert the YAML syntax into an abstract syntax tree (AST), which is then converted into a JavaScript object. The resulting object will have keys and values that correspond to the keys and values in the YAML document. The parser supports many features of the YAML format, such as complex data structures, references, and aliases. You can also provide options to the js-yaml.load function to customize its behavior, such as disabling certain features or specifying a custom schema for the data.

5
6
7
8
9
10
11
12
13
14
const typeFolder = path.join(__dirname, '../src/universal/types')
const languageFile = path.join(i18nFolder, languageFileName)

const langFile = fs.readFileSync(languageFile, 'utf8')

const obj = yaml.load(langFile)

const keys = Object.keys(obj)

const types =
fork icon30
star icon462
watch icon8

19
20
21
22
23
24
25
26
27
28
const splitTextStyleAndMetadata = (brew)=>{
	brew.text = brew.text.replaceAll('\r\n', '\n');
	if(brew.text.startsWith('```metadata')) {
		const index = brew.text.indexOf('```\n\n');
		const metadataSection = brew.text.slice(12, index - 1);
		const metadata = yaml.load(metadataSection);
		Object.assign(brew, _.pick(metadata, ['title', 'description', 'tags', 'systems', 'renderer', 'theme', 'lang']));
		brew.text = brew.text.slice(index + 5);
	}
	if(brew.text.startsWith('```css')) {
fork icon296
star icon859
watch icon36

+ 3 other calls in file

Ai Example

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

const yamlString = `
- name: John Doe
age: 42
- name: Jane Smith
age: 35
`;

const people = yaml.load(yamlString);

console.log(people);

In this example, we have a YAML string that represents an array of people with names and ages. We use the js-yaml library to load the YAML string into a JavaScript object using the yaml.load function. The resulting people variable is an array of objects that corresponds to the data in the YAML string. We can then manipulate this data using standard JavaScript operations, such as iterating over the array or accessing individual properties.

41
42
43
44
45
46
47
48
49
let name = m[2]
let locale = m[3]

say(`importing ${locale} labels into ${name} menu...`)
let labels = load(join(home, file))[locale]
let menu = yaml.load(yaml.dump(open(name).en, { noRefs: true }))
translate(menu, labels)
save({ [locale]: menu }, name, locale)
rm(join(home, file))
fork icon46
star icon713
watch icon29

+ 7 other calls in file

377
378
379
380
381
382
383
384
385
386
    e.repo = repo;
    e.ref = ref;
    throw e;
  }

  return yaml.load(Buffer.from(response.data.content, 'base64').toString());
}

_handlerStarted(name) {
  if (typeof this.handlersCount[name] === 'undefined') {
fork icon239
star icon317
watch icon18

+ 4 other calls in file

278
279
280
281
282
283
284
285
286
        constants.DEBUG && console.log("File: " + fileToCheck + " exists loading it.");

        constants.DEBUG && console.log("Loading as YAML file.");

        // Load in as yml or yaml file and return this object
        return YAML.load(fs.readFileSync(fileToCheck), 'utf8');
    }
} else {
    constants.DEBUG && console.log("Trying to load as json or js.");
fork icon67
star icon550
watch icon15

+ 4 other calls in file

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


require('dotenv').config();


PAPI_URL = "https://api.segmentapis.com"


const regionalSupport = yaml.load(fs.readFileSync(path.resolve(__dirname, `../src/_data/regional-support.yml`)))
const slugOverrides = yaml.load(fs.readFileSync(path.resolve(__dirname, `../src/_data/catalog/slugs.yml`)))




const slugify = (displayName) => {
fork icon189
star icon33
watch icon10

+ 7 other calls in file

37
38
39
40
41
42
43
44
45
46
47


// bump only when is not snapshot
if(!isSnapshot) {
    // bump docker-compose.js version
    const dockerComposePath = 'docker-compose.yml';
    let dockerComposeYml = yaml.load(fs.readFileSync(dockerComposePath));
    dockerComposeYml.services.relay.image = `ghcr.io/hashgraph/hedera-json-rpc-relay:${newVersion}`
    fs.writeFileSync(dockerComposePath, yaml.dump(dockerComposeYml));
    console.log(dockerComposePath);

fork icon31
star icon26
watch icon11

-1
fork icon20
star icon21
watch icon3

+ 8 other calls in file

600
601
602
603
604
605
606
607
608
609
610
    end: end.toISOString().slice(0, 10)
  }
}


export function yamlToJson(str) {
  return yaml.load(str)
}
export function jsonToYaml(data) {
  return yaml.dump(data)
}
fork icon6
star icon83
watch icon5

+ 5 other calls in file

214
215
216
217
218
219
220
221
222
223
return __awaiter(this, void 0, void 0, function* () {
    yield getAccessToken("https://management.core.windows.net");
    YamlPath = core.getInput('loadTestConfigFile');
    if (!(pathLib.extname(YamlPath) === ".yaml" || pathLib.extname(YamlPath) === ".yml"))
        throw new Error("The Load Test configuration file should be of type .yaml or .yml");
    const config = yaml.load(fs.readFileSync(YamlPath, 'utf8'));
    if (util_1.isNullOrUndefined(config.testName) && util_1.isNullOrUndefined(config.testId))
        throw new Error("The required field testId is missing in " + YamlPath + ".");
    if (!util_1.isNullOrUndefined(config.testName)) {
        testId = config.testName;
fork icon10
star icon13
watch icon5

+ 4 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
40


module.exports = function (eleventyConfig) {
    let pathPrefix = "/";


    eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
    eleventyConfig.addDataExtension("yml", contents => yaml.load(contents));


    eleventyConfig.addPlugin(pluginRss);
    //Blog excerpts
    eleventyConfig.addPlugin(description);
fork icon3
star icon6
watch icon2

+ 3 other calls in file

108
109
110
111
112
113
114
115
116
117
console.log("Reading changelogs file");
const file = fs.readFileSync(
    `../../${process.env.CHANGELOG_DIR}`,
    "utf8"
);
const data = yaml.load(file);

const changelogs = data && data.Entries ? Array.from(data.Entries) : [];

// Check if 'Entries:' already exists and remove it
fork icon13
star icon5
watch icon1

+ 9 other calls in file

9
10
11
12
13
14
15
16
17
18
19
try {
  internal = require('@next-theme/plugins');
} catch (error) {
}
const vendorsFile = fs.readFileSync(path.join(__dirname, '../../../_vendors.yml'));
const dependencies = yaml.load(vendorsFile);


module.exports = hexo => {
  const { vendors, creative_commons, pace } = hexo.theme.config;
  if (typeof internal === 'function') {
fork icon0
star icon2
watch icon1

46
47
48
49
50
51
52
53
54
55
let contentPath = contentDir + parsedPath.replace('.html', '.json');

// frontmatter and url: /explore
const pathMarkdown =
  'docs/' + path.split('docs/.vitepress/dist/')[1].replace('.html', '.md');
let frontmatter = yaml.load(
  fs.readFileSync(pathMarkdown, 'utf8').split('---')[1]
);

// Get the html files and extract the text from the html
fork icon6
star icon1
watch icon6

+ 11 other calls in file

23
24
25
26
27
28
29
30
31
32
	break;

case 'yaml':
case 'yml':
	try {
		template = jsYaml.load(
			readFileSync(templatePath, 'utf8'),
			{ schema: schema }
		);
		break;
fork icon0
star icon2
watch icon1

+ 4 other calls in file

37
38
39
40
41
42
43
44
45
}

load_blocks() {
    fs.readdirSync(this.blocks_path).forEach(file => {
        let file_name = file.split('.')[0];
        let module = yaml.load(fs.readFileSync(path.join(this.blocks_path, file), 'utf8'));
        this.blocks[file_name] = module[file_name];
    });
}
fork icon0
star icon2
watch icon1

+ 7 other calls in file

44
45
46
47
48
49
50
51
52
}

load_modules() {
    fs.readdirSync(this.modules_path).forEach(file => {
        let file_name = file.split('.')[0];
        let module = yaml.load(fs.readFileSync(path.join(this.modules_path, file), 'utf8'));
        this.modules[file_name] = module[file_name];
    });
}
fork icon0
star icon2
watch icon1

+ 15 other calls in file

248
249
250
251
252
253
254
255
256
257

const notion = new Client({ auth: secret });
const nowFormat = moment().format("YYYY-MM-DD HH:mm:ss");


const projectInfo = yaml.load(fs.readFileSync('./info.yml', "utf8"));

const title = projectInfo.title;
const _version = ver;
const host = projectInfo.host;
fork icon2
star icon3
watch icon1

+ 2 other calls in file