npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@devpossible/ton

v0.1.8

Published

JavaScript library for parsing, validating, and serializing TON (Text Object Notation) files. Full specification at https://tonspec.com. ALPHA RELEASE: Core functionality is complete but API may change before stable 1.0 release.

Readme

DevPossible.Ton - JavaScript / TypeScript Library

Developed by DevPossible, LLC
Contact: [email protected]

JavaScript/TypeScript implementation of the TON (Text Object Notation) parser, serializer, formatter, and validator.

Installation

npm install @devpossible/ton

or

yarn add @devpossible/ton

Quick Start

import { TonParser, TonDocument, TonObject, TonValue, TonSerializer, TonSerializeOptions } from '@devpossible/ton';

// Parse a TON file
const parser = new TonParser();
const document = parser.parseFile('config.ton');

// Access properties
const name = document.rootObject.getProperty('name')?.toString();
const port = document.rootObject.getProperty('port')?.toInt32() ?? 8080;

// Create a new document
const newDoc = new TonDocument();
newDoc.rootObject.setProperty('name', TonValue.from('My App'));
newDoc.rootObject.setProperty('port', TonValue.from(8080));

// Add nested object
const database = new TonObject('database');
database.setProperty('host', TonValue.from('localhost'));
newDoc.rootObject.addChild(database);

// Serialize and save
const serializer = new TonSerializer();
const tonContent = serializer.serializeDocument(newDoc, TonSerializeOptions.Pretty);
await serializer.serializeToFile(newDoc, 'config.ton', TonSerializeOptions.Pretty);

Features

  • ✅ Full TON specification parser with async support
  • ✅ Comprehensive schema validation with 30+ validation rule types
  • ✅ Code formatter with compact and pretty styles
  • ✅ Type annotations and hints
  • ✅ Enum and EnumSet support
  • ✅ Array support with mixed types
  • ✅ Pretty and compact serialization
  • ✅ Async file operations
  • ✅ Schema collections and definitions
  • ✅ Path-based schema validation
  • ✅ TypeScript type definitions included
  • ✅ ESM and CommonJS support

Advanced Usage

Schema Validation

import { TonParser, TonSchemaCollection, TonSchemaDefinition, TonPropertySchema, TonValidationRule, TonValidator, ValidationRuleType } from '@devpossible/ton';

// Define schema
const schemas = new TonSchemaCollection();
const userSchema = new TonSchemaDefinition('user');

// Add property validations
const nameSchema = new TonPropertySchema('/name', 'string');
nameSchema.addValidation(new TonValidationRule(ValidationRuleType.Required));
nameSchema.addValidation(new TonValidationRule(ValidationRuleType.MaxLength, 100));
userSchema.addProperty('/name', nameSchema);

const emailSchema = new TonPropertySchema('/email', 'string');
emailSchema.addValidation(new TonValidationRule(ValidationRuleType.Format, 'email'));
userSchema.addProperty('/email', emailSchema);

schemas.addSchema(userSchema);

// Validate document
const validator = new TonValidator();
const parser = new TonParser();
const document = parser.parse("{ (user) name = 'John', email = '[email protected]' }");
document.schemas = schemas;

const results = validator.validate(document);
if (!results.isValid) {
    results.errors.forEach(error => 
        console.log(`Error at ${error.path}: ${error.message}`)
    );
}

Formatting

import { TonFormatter, TonFormatStyle } from '@devpossible/ton';

// Format a TON string with pretty formatting (default)
const unformattedTon = "{name='MyApp',version=1.0,enabled=true}";
const prettyFormatted = TonFormatter.formatString(unformattedTon);

// Format with compact formatting
const compactFormatted = TonFormatter.formatString(unformattedTon, TonFormatStyle.Compact);

// Format files
TonFormatter.formatFileInPlace('config.ton', TonFormatStyle.Pretty);

// Async formatting
const formatted = await TonFormatter.formatStringAsync(unformattedTon, TonFormatStyle.Pretty);

Serialization Options

import { TonSerializeOptions } from '@devpossible/ton';

// Compact format - minimal size
const compact = TonSerializeOptions.Compact;

// Pretty format - human readable with all features
const pretty = TonSerializeOptions.Pretty;

// Custom options
const custom = new TonSerializeOptions({
    // Formatting
    indentation: '  ',           // Two spaces
    pretty: true,                // Enable formatting
    sortProperties: true,        // Alphabetical property order

    // Content control
    includeHeader: true,         // Include #@ header
    includeSchema: true,         // Include #! schema
    includeTypeHints: true,      // Add type hints ($, %, etc.)

    // Value handling
    omitNullValues: false,       // Include null values
    omitUndefinedValues: true,   // Skip undefined values
    omitEmptyCollections: false, // Include empty arrays

    // Formatting preferences
    useAtPrefix: true,           // Use @ for properties
    quoteChar: "'",              // Use single quotes
    useMultiLineStrings: true,   // Enable multi-line string format
    multiLineStringThreshold: 2, // Min lines to use multi-line format
    lowercaseHex: true,          // 0xff instead of 0xFF
    lowercaseGuids: true,        // Lowercase GUID format
    preferEnumNames: true        // Use names over indices
});

const serializer = new TonSerializer();
const output = serializer.serializeDocument(document, custom);

Working with Arrays

import { TonDocument, TonValue } from '@devpossible/ton';

// Create document with arrays
const document = new TonDocument();
const tags = ['production', 'web', 'api'];
const scores = [98.5, 87.2, 95.0];

document.rootObject.setProperty('tags', TonValue.from(tags));
document.rootObject.setProperty('scores', TonValue.from(scores));

// Format with different styles
const serializer = new TonSerializer();
const compact = serializer.serializeDocument(document, TonSerializeOptions.Compact);
const pretty = serializer.serializeDocument(document, TonSerializeOptions.Pretty);

// Or use the formatter directly
const formatted = TonFormatter.formatString(compact, TonFormatStyle.Pretty);

TypeScript Support

The library includes full TypeScript type definitions:

import { TonParser, TonDocument, TonValue, TonValueType } from '@devpossible/ton';

const parser: TonParser = new TonParser();
const document: TonDocument = parser.parseFile('config.ton');

const value: TonValue | undefined = document.rootObject.getProperty('name');
if (value?.type === TonValueType.String) {
    const name: string = value.toString();
}

API Reference

Core Classes

  • TonDocument - Represents a complete TON document with header, root object, and schemas
  • TonObject - Represents an object with properties and child objects
  • TonValue - Represents a typed value with conversion methods
  • TonParser - Parses TON content from various sources
  • TonSerializer - Serializes TON objects to string format
  • TonFormatter - Formats existing TON content with different styles
  • TonValidator - Validates TON documents against schemas

Key Enumerations

  • TonValueType - Types of TON values (String, Integer, Float, Boolean, etc.)
  • TonTokenType - Token types used by the lexer
  • ValidationRuleType - Types of validation rules
  • TonFormatStyle - Formatting styles (Compact, Pretty)

Requirements

  • Node.js 14.0.0 or later
  • Modern browsers with ES2015+ support

License

MIT License - Copyright © 2024 DevPossible, LLC

Support

DevPossible, LLC
Website: devpossible.com
Email: [email protected]
TON Specification: tonspec.com


© 2024 DevPossible, LLC. All rights reserved.