πββοΈ Why I Wrote This
You know that moment when you're knee-deep in a side project and you just want a clean config file?
Something easier on the eyes than JSON, but more structured than old-school INI?
That was me β and that's how YINI started.
Not to reinvent configuration formats, but just to make one that I wouldn't dread editing.
In this post, I'll walk you through how to actually use YINI in a Node.js app, with real examples pulled straight from my own parser tests and tooling setup. It's simple, flexible, and (hopefully) a bit fun.
π¦ Installing the Parser
First, install the YINI parser from npm:
npm install yini-parser
π§ͺ Parsing a Basic Config
Let's start with a simple YINI config:
^ App
title = 'My App Title'
items = 25
isDarkTheme = true
Here's how you load it in your app:
import YINI from 'yini-parser';
const config = YINI.parse(`
^ App
title = 'My App Title'
items = 25
isDarkTheme = true
`);
console.log(config.App.title); // 'My App Title'
YINI requires strings to be quoted for clarity.
Numbers, booleans, and nulls are parsed correctly too β without weird surprises.
The above config corresponds to this JavaScript object:
{
App: {
title: 'My App Title',
items: 25,
isDarkTheme: true
}
}
π Loading from a File
In most real projects, you'll want to load your config from a file.
yini-parser
provides a helper for that: YINI.parseFile(..)
.
import YINI from 'yini-parser';
const config = YINI.parseFile('./config.yini');
console.log(config.App.title); // e.g., 'My App Title'
This works with .yini
files, and supports options like strict mode or metadata.
Under the hood, parseFile(..)
just reads the file and passes it to YINI.parse(..)
. Simple and clean.
π‘ Structured Config with Nested Sections
Sometimes your config grows β and when it does, structure helps.
YINI supports nested sections (similar as to in Markdown) but with using caret-based indentation:
@yini
^ App
name = "Nested Example"
version = "1.0.0"
debug = OFF
^^ Database
host = "db.example.com"
port = 3306
user = "appuser"
password = "dbpassword"
^^^ Pool
min = 2
max = 10
idleTimeout = 300
^^ Logging
level = "info"
logToFile = ON
filePath = "./logs/app.log"
/END
You can parse this in strict mode like this:
const config = YINI.parse(yiniString, true); // true = strict mode
The resulting object:
{
App: {
name: 'Nested Example',
version: '1.0.0',
debug: false,
Database: {
host: 'db.example.com',
port: 3306,
user: 'appuser',
password: 'dbpassword',
Pool: { min: 2, max: 10, idleTimeout: 300 }
},
Logging: { level: 'info', logToFile: true, filePath: './logs/app.log' }
}
}
π Optional Metadata
Want more than just key/values?
You can enable metadata mode to get some stats and some other info β useful for diagnostics, etc.
const configWithMeta = YINI.parse(yiniString, false, 'auto', true);
π¨οΈ Comments β Your Way
YINI supports a variety of comment styles:
// This is a comment
# So is this
-- And this
/*
Block comments
are supported too
*/
β Recap
YINI isn't trying to be better than JSON, TOML, or YAML.
It just tries to be clear, consistent, and pleasant to use β especially when you want nesting without indentation sensitivity, or strict validation without a mess of brackets.
If you like working with structured configs in Node.js and want something that's:
- Human-friendly
- Easy to read
- Tooling-ready
- Just structured enoughβ¦
π Links
- npm: yini-parser
- GitHub: YINI-lang/yini-parser-typescript
- Intro post: Intro to YINI Config Format
β Marko
Creator of YINI
Note: This article was originally published on Medium.
Top comments (0)