What is toml?
The 'toml' npm package is a library for parsing and stringifying TOML (Tom's Obvious, Minimal Language) data. TOML is a configuration file format that is easy to read due to its simplicity and human-readable syntax. This package allows you to convert TOML data to JavaScript objects and vice versa.
What are toml's main functionalities?
Parsing TOML to JavaScript Object
This feature allows you to parse a TOML string into a JavaScript object. The example demonstrates parsing a TOML string containing an 'owner' table with 'name' and 'dob' fields.
const toml = require('toml');
const tomlString = `
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z
`;
const parsedData = toml.parse(tomlString);
console.log(parsedData);
Stringifying JavaScript Object to TOML
This feature allows you to convert a JavaScript object into a TOML string. The example demonstrates stringifying a JavaScript object with an 'owner' property into a TOML formatted string.
const toml = require('toml');
const jsObject = {
owner: {
name: "Tom Preston-Werner",
dob: new Date('1979-05-27T07:32:00Z')
}
};
const tomlString = toml.stringify(jsObject);
console.log(tomlString);
Other packages similar to toml
toml-js
The 'toml-js' package is another library for parsing and stringifying TOML data. It offers similar functionality to the 'toml' package but may have different performance characteristics and API design.
tomlify-j0.4
The 'tomlify-j0.4' package focuses on converting JavaScript objects to TOML strings. It is useful if you primarily need to generate TOML data from JavaScript objects.
toml-node
The 'toml-node' package provides TOML parsing and stringifying capabilities. It is another alternative to the 'toml' package with its own implementation and features.
TOML Parser for Node.js
If you haven't heard of TOML, well you're just missing out. Go check it out now. Back? Good.
Instalation
toml-node is available via npm.
npm install toml
Usage
Standalone
Say you have some awesome TOML in a variable called someTomlString
. Maybe it came from the web; maybe it came from a file; wherever it came from, it came asynchronously! Let's turn that sucker into a JavaScript object.
var toml = require('toml');
var data = toml.parse(someTomlString);
console.dir(data);
Streaming
You can pipe a stream of TOML text into toml-node and it will emit a single data
event with the parsed results once the stream is complete.
var toml = require('toml');
var fs = require('fs');
fs.createReadStream('tomlFile.toml').pipe(toml.createStream()).on('data', function(results) {
// `results` is your parsed TOML
});
The stream will emit an error
event in the case of an error while parsing the TOML document.
TOML Spec Support
toml-node supports the TOML spec as specified by mojombo/toml@4a6ed394
Building & Tests
toml-node uses the Jison parser generator.
npm install -g jison
jison src/toml.jison -o lib/toml.js
npm test
toml-node runs on Travis CI and is tested against:
License
toml-node is licensed under the MIT license agreement. See the LICENSE file for more information.