@toonify/toonify
v1.0.1
Published
Parse TOON format to JSON and JSON to TOON format - A Node.js library
Maintainers
Readme
toonify
Parse TOON format to JSON and JSON to TOON format - A Node.js library.
Installation
npm install @toonify/toonifyUsage
Decode TOON to JSON
import { decode } from "@toonify/toonify";
const toon = `users[2]{id,name,email}:
1,Alice,[email protected]
2,Bob,[email protected]`;
const json = decode(toon);
// Returns: {
// users: [
// { id: 1, name: 'Alice', email: '[email protected]' },
// { id: 2, name: 'Bob', email: '[email protected]' }
// ]
// }Encode JSON to TOON
import { encode } from "@toonify/toonify";
const json = {
users: [
{ id: 1, name: "Alice", email: "[email protected]" },
{ id: 2, name: "Bob", email: "[email protected]" },
],
};
const toon = encode(json);
// Returns: "users[2]{id,name,email}:\n 1,Alice,[email protected]\n 2,Bob,[email protected]"TOON Format
TOON is a human-readable data format that uses indentation and special syntax for arrays and objects.
Basic Syntax
- Objects: Use
key: valuepairs with indentation for nesting - Arrays: Use
name[N]{fields}:for tabular arrays orname[N]:for list arrays - Delimiters: Comma (default), tab (
\t), or pipe (|)
Examples
Simple Object
name: Alice
age: 30
active: trueNested Object
user:
name: Alice
profile:
email: [email protected]
role: adminTabular Array
users[2]{id,name,email}:
1,Alice,[email protected]
2,Bob,[email protected]List Array
tags[3]:
javascript
nodejs
toonMixed Data
config:
name: My App
version: 1.0.0
users[2]{id,name}:
1,Alice
2,Bob
tags[2]:
frontend
backendAPI
decode(toonString, options?)
Decode a TOON format string to a JavaScript object.
Parameters:
toonString(string): TOON formatted stringoptions(object, optional):strict(boolean, default:true): Validate structure strictlyexpand_paths(string, default:'off'): Path expansion mode ('off'|'safe')default_delimiter(string, default:','): Default delimiter for arraysindent(number): Indent size (auto-detected if not provided)
Returns: JavaScript object or array
encode(data, options?)
Encode a JavaScript object or array to TOON format.
Parameters:
data(any): JavaScript object or array to encodeoptions(object, optional):indent(number, default:2): Indentation sizedelimiter(string, default:','): Default delimiter for arrayscompact(boolean, default:false): Use compact format
Returns: TOON formatted string
detectIndent(toonString)
Detect the indentation size used in a TOON string.
Parameters:
toonString(string): TOON formatted string
Returns: Detected indent size (default 2 if unable to detect)
Features
- ✅ Parse TOON format to JavaScript objects
- ✅ Convert JavaScript objects to TOON format
- ✅ Support for tabular and list arrays
- ✅ Support for nested objects and arrays
- ✅ Multiple delimiter support (comma, tab, pipe)
- ✅ Auto-detection of indentation
- ✅ Type-safe with JSDoc annotations
- ✅ ES Modules support
License
MIT
