Create YAML Nodes
Anemos provides various ways to create YAML nodes. It is possible to create all nodes using constructors.
It is also possible to create Mapping and Sequence
using the parseMapping
and parseSequence
methods. These methods are useful when you want to create a node
from a YAML string.
- TypeScript
- JavaScript
mapping.ts
import * as anemos from "@ohayocorp/anemos";
// Create an empty mapping.
const emptyMapping = new anemos.Mapping();
// Create a mapping from simple key-value pairs.
const mappingFromKeyValuePairs = new anemos.Mapping({
"key1": "value1",
"key2": "value2",
});
// Create a mapping from an object. Values are converted to YAML nodes.
const mappingFromObject = new anemos.Mapping({
key1: "value1",
key2: {
key1: "value1",
key2: [
"value1",
"value2",
],
},
});
// Parse a YAML string as a mapping.
const mappingParsed = anemos.parseMapping(`
key1: value1
key2:
key1: value1
key2: value2
`);
// Modify a mapping.
const mapping = new anemos.Mapping();
// Set simple key-value pairs.
mapping.set("key1", "value1");
// Set a mapping as a value.
mapping.set("key2", {
key1: "value1",
key2: "value2",
});
// Set a sequence as a value.
mapping.set("key3", [
"value1",
"value2",
]);
// Remove a key from a mapping.
mapping.remove("key1");
// Insert a key-value pair at the given index.
mapping.insert(0, "key1", "value1");
mapping.js
const anemos = require("@ohayocorp/anemos");
// Create an empty mapping.
const emptyMapping = new anemos.Mapping();
// Create a mapping from simple key-value pairs.
const mappingFromKeyValuePairs = new anemos.Mapping({
"key1": "value1",
"key2": "value2",
});
// Create a mapping from an object. Values are converted to YAML nodes.
const mappingFromObject = new anemos.Mapping({
key1: "value1",
key2: {
key1: "value1",
key2: [
"value1",
"value2",
],
},
});
// Parse a YAML string as a mapping.
const mappingParsed = anemos.parseMapping(`
key1: value1
key2:
key1: value1
key2: value2
`);
// Modify a mapping.
const mapping = new anemos.Mapping();
// Set simple key-value pairs.
mapping.set("key1", "value1");
// Set a mapping as a value.
mapping.set("key2", {
key1: "value1",
key2: "value2",
});
// Set a sequence as a value.
mapping.set("key3", [
"value1",
"value2",
]);
// Remove a key from a mapping.
mapping.remove("key1");
// Insert a key-value pair at the given index.
mapping.insert(0, "key1", "value1");
- TypeScript
- JavaScript
sequence.ts
import * as anemos from "@ohayocorp/anemos";
// Create an empty sequence.
const emptySequence = new anemos.Sequence();
// Create a sequence from an array.
const sequenceFromArray = new anemos.Sequence([
"value1",
{
key1: "mapping",
},
[
"another sequence"
],
]);
const sequenceParsed = anemos.parseSequence(`
- key1: value1
- key2:
key1: value1
key2: value2
`);
sequence.js
const anemos = require("@ohayocorp/anemos");
// Create an empty sequence.
const emptySequence = new anemos.Sequence();
// Create a sequence from an array.
const sequenceFromArray = new anemos.Sequence([
"value1",
{
key1: "mapping",
},
[
"another sequence"
],
]);
const sequenceParsed = anemos.parseSequence(`
- key1: value1
- key2:
key1: value1
key2: value2
`);
- TypeScript
- JavaScript
scalar.ts
import * as anemos from "@ohayocorp/anemos";
const emptyScalar = new anemos.Scalar();
const scalarFromString = new anemos.Scalar("string");
const scalarFromNumber = new anemos.Scalar(1);
const scalarFromBoolean = new anemos.Scalar(true);
const scalarWithStyle = new anemos.Scalar("literal", anemos.YamlStyle.Literal);
const scalar = new anemos.Scalar();
// Set the value of the scalar.
scalar.setValue("string");
// Set the style of the scalar.
scalar.setStyle(anemos.YamlStyle.Literal);
scalar.js
const anemos = require("@ohayocorp/anemos");
const emptyScalar = new anemos.Scalar();
const scalarFromString = new anemos.Scalar("string");
const scalarFromNumber = new anemos.Scalar(1);
const scalarFromBoolean = new anemos.Scalar(true);
const scalarWithStyle = new anemos.Scalar("literal", anemos.YamlStyle.Literal);
const scalar = new anemos.Scalar();
// Set the value of the scalar.
scalar.setValue("string");
// Set the style of the scalar.
scalar.setStyle(anemos.YamlStyle.Literal);