Skip to content

Axorax/toml-parse.js

Repository files navigation

toml-parse.js

Simple TOML parser

⚙️ Installation

npm i toml-parse

CDN Links:

📖 Usage

● Import

// ES6
import tomlParse from "toml-parse";

// commonjs
const tomlParse = require("toml-parse");

● From TOML to JSON

const tomlCode = `
# Example TOML code
title = "Hello, World"
[author]
name = "John Doe"
age = 30
`;

const json = tomlParse.toJSON(tomlCode);

console.log(json);

● From JSON to TOML

const json = {
  title: 'Hello, World',
  author: { name: 'John Doe', age: 30 }
};

const tomlCode = tomlParse.fromJSON(json);

console.log(tomlCode);

● From TOML to CSS

const tomlCode = `
# Example TOML code
[body]
background-color = "white"
color = "black"
`;

const cssCode = tomlParse.toCSS(tomlCode);

console.log(cssCode);

● From CSS to TOML

const cssCode = `
body {
  background-color: white;
  color: black;
}
h1, h2 {
  font-weight: bold;
}
`;

const tomlCode = tomlParse.fromCSS(cssCode);

console.log(tomlCode);

● JavaScript value to TOML value

const value = 'Hello, World!';

const tomlValue = tomlParse.toTomlValue(value);

console.log(tomlValue);

● TOML value to JavaScript value

const value = 'true';

const parsed = tomlParse.parseValue(value);

console.log(parsed);

● Get table

const json = {
  fruits: {
    apple: 'red',
    banana: 'yellow'
  }
};

const tableName = 'fruits.apple';

const table = tomlParse.getTable(json, tableName);

console.log(table);

● Set key

const json = {
  fruits: {
    apple: 'red',
    banana: 'yellow'
  }
};

const key = 'fruits.apple';
const value = 'green';

tomlParse.setKey(json, key, value);

console.log(json);

Support me on PatreonCheck out my socials