Monocart Formatter
JS/CSS/HTML/JSON formatter with mapping for both browser (Web Worker) and Node.js (Worker Threads).
Usage
import { format, MappingParser } from 'monocart-formatter';
const text = "var a = 1;";
const type = "js";
const options = {};
const { content, mapping } = await format(text, type, options);
console.log("formatted content", content);
const mappingParser = new MappingParser(mapping);
const formattedPosition = mappingParser.originalToFormatted(10);
const originalPosition = mappingParser.formattedToOriginal(formattedPosition);
API
export type FormatterMapping = {
original: number[],
formatted: number[]
}
export function generateMapping(
originalText: string,
formattedText: string
): FormatterMapping
export function format(
text: string,
type?: string,
options?: any
): Promise<{
content: string,
mapping: FormatterMapping,
error?: Error
}>
export type CommentItem = {
block: boolean,
start: number,
end: number,
text: string
}
export class CommentParser {
constructor(source: string);
comments: CommentItem[];
isComment(start: number, end: number): boolean;
}
export type LineItem = {
line: number,
length: number,
indent: number,
start: number,
end: number,
blank?: boolean,
comment?: boolean,
text: string
}
export class LineParser {
constructor(source: string);
lines: LineItem[];
comments: CommentItem[];
findLine(pos: number): LineItem;
}
export type LocationItem = LineItem & {
line: number,
column: number
}
export class Locator {
constructor(source: string);
source: string;
lineParser: LineParser;
lines: LineItem[];
comments: CommentItem[];
locationToOffset(loc: LocationItem): number;
offsetToLocation(offset: number): LocationItem;
getSlice(start: number, end?: number): string;
getLine(line: number): LineItem
}
export class MappingParser {
constructor(mapping: FormatterMapping);
originalToFormatted(originalPosition: number): number
formattedToOriginal(formattedPosition: number): number
}