piml.js is a JavaScript library for parsing and stringifying data in the PIML format.
piml.js
Spec version: v1.1.0
piml.js is a JavaScript library for parsing and stringifying data in the PIML (Parenthesis Intended Markup Language) format. PIML is a human-readable, indentation-based data serialization format designed for configuration files and simple data structures.
Features
- Intuitive Syntax: Easy-to-read key-value pairs, supporting nested structures.
- Primitive Types: Supports strings, numbers, booleans, and null.
- Complex Types: Handles objects and arrays.
- Multi-line Strings: Supports multi-line string values with indentation.
- Comments: Allows single-line comments using
#(only lines starting with#are treated as comments).
PIML Format Overview
PIML uses a simple key-value structure. Keys are enclosed in parentheses () , and values follow. Indentation defines nesting.
(site_name) PIML Demo
(port) 8080
(is_production) false
(version) 1.2
(database)
(host) localhost
(port) 5432
(admins)
> (User)
(id) 1
(name) Alice
> (User)
(id) 2
(name) Bob
(features)
> auth
> logging
> metrics
(description)
This is a sample product description.
It spans multiple lines.
With an empty line in between.
(metadata)
(related_ids) nil
Installation
To use piml.js in your project, you can install it via npm:
npm install piml.js
Or via yarn:
yarn add piml.js
Usage
Parsing PIML to JavaScript Objects
import { parse } from 'piml.js';
const pimlString = `
(site_name) My Awesome Site
(port) 8080
(is_production) true
(admins)
> (User)
(id) 1
(name) Admin One
> (User)
(id) 2
(name) Admin Two
(description)
This is a multi-line
description for the site.
`;
const config = parse(pimlString);
console.log(config);
/*
Output:
{
site_name: 'My Awesome Site',
port: 8080,
is_production: true,
admins: [
{ id: 1, name: 'Admin One' },
{ id: 2, name: 'Admin Two' }
],
description: 'This is a multi-line\ndescription for the site.'
}
*/
Stringifying JavaScript Objects to PIML
import { stringify } from 'piml.js';
const config = {
site_name: 'My Awesome Site',
port: 8080,
is_production: true,
admins: [
{ id: 1, name: 'Admin One' },
{ id: 2, name: 'Admin Two' }
],
description: 'This is a multi-line\ndescription for the site.'
};
const pimlString = stringify(config);
console.log(pimlString);
/*
Output:
(site_name) My Awesome Site
(port) 8080
(is_production) true
(admins)
> (User)
(id) 1
(name) Admin One
> (User)
(id) 2
(name) Admin Two
(description)
This is a multi-line
description for the site.
*/
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.