1.0.2 • Published 2 years ago

@dotbox/format v1.0.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

@dotbox/format

A formatter for DotBox files.

Installation

# Using npm.
npm install @dotbox/format

# Using yarn.
yarn add @dotbox/format

Usage

format(text: string)

To format DotBox code, call the format function with the contents of your .box file.

import { format } from "@dotbox/format";

const text = `
// This is my awesome DotBox file.
my_key = {
	my_number = 1_000
}
`;

const { value, error } = format(text);

if (error) {
	// Handle compilation errors
	for (const err of error) {
		// ...
	}
} else {
	// Use `value` (the formatted text) somehow
	console.log("Formatted Text:");
	console.log(value);
}

class Formatter

It is typically unnecessary to use the Formatter class directly, but in the event you need it this package exports the class.

import { Formatter } from "@dotbox/format";

const text = `
// This is my awesome DotBox file.
my_key = {
	my_number = 1_000
}
`;

const formatter = new Formatter();

const { value, error } = formatter.format(text);

if (error) {
	// Handle compilation errors
	for (const err of error) {
		// ...
	}
} else {
	// Use `value` (the formatted text) somehow
	console.log("Formatted Text:");
	console.log(value);
}

// The Parser and Lexer instances can be accessed on the
// Formatter instance.
const parser = formatter.parser;
const lexer = formatter.lexer;