Licence
MIT
Version
1.0.2
Deps
2
Size
257 kB
Vulns
0
Weekly
0
Install scriptsThis package runs scripts during installation (preinstall/install/postinstall)
yaml-kit
A personal learning/practice fork exploring YAML parsing and serialization in JavaScript. Not an official or published package — built as a packaging and API-design exercise.
Installation
npm install yaml-kit
Usage
const yaml = require('yaml-kit');
const fs = require('fs');
// Parse a YAML string, throwing on untrusted/unsafe content
const doc = yaml.safeLoad('name: John\nage: 30\ntags:\n - admin\n - user');
console.log(doc); // { name: 'John', age: 30, tags: [ 'admin', 'user' ] }
// Serialize a JS object to a YAML string
const str = yaml.safeDump({ name: 'John', age: 30 });
console.log(str);
// name: John
// age: 30
// Parse a YAML file
const config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'));
// Parse a stream containing multiple YAML documents
yaml.safeLoadAll(fs.readFileSync('multi-doc.yml', 'utf8'), function (doc) {
console.log(doc);
});
load/loadAll also exist alongside the safeLoad/safeLoadAll variants,
but they use the full schema (which can construct arbitrary JS types) rather
than the safe schema — prefer the safe* functions unless you fully trust
the input.
API
| Export | Description |
|---|---|
load(str, options) |
Parse a single YAML document using the full schema |
loadAll(str, iterator, options) |
Parse multiple documents using the full schema |
safeLoad(str, options) |
Parse a single document using the safe schema |
safeLoadAll(str, iterator, options) |
Parse multiple documents using the safe schema |
dump(obj, options) |
Serialize an object using the full schema |
safeDump(obj, options) |
Serialize an object using the safe schema |
Type, Schema |
Building blocks for defining custom YAML types/schemas |
FAILSAFE_SCHEMA, JSON_SCHEMA, CORE_SCHEMA, DEFAULT_SAFE_SCHEMA, DEFAULT_FULL_SCHEMA |
Built-in schemas |
YAMLException |
Error type thrown on parse failures |
CLI
yaml-kit [-c] [-t] [file]
Reads a file (or stdin if no file is given) and converts YAML to JSON or JSON to YAML, depending on the input.
-c, --compact— display errors in compact mode-t, --trace— show a stack trace on error
License
MIT