loft-codec
v1.1.4
Published
LOFT: Language Optimized Format for Tokenization - A token-efficient serialization format for LLMs.
Maintainers
Readme
LOFT - Language Optimized Format for Tokenization
A TypeScript library for serializing JavaScript objects into the LOFT format - a highly readable, token-efficient serialization format optimized for LLMs and data transmission.
🌟 Features
- ✅ Primitive Types: Booleans (
+/-), Null (~), Numbers, Strings - ✅ Smart String Handling: Automatic backtick wrapping for special characters and whitespace
- ✅ Array Formats: Simple lists and matrix format for structured data
- ✅ Object Serialization: Nested objects with readable indentation
- ✅ Pure LOFT Format: Indentation-based structure (default) or legacy braced format
- ✅ Matrix Format: Efficient representation of arrays with identical keys
- ✅ Customizable Indentation: Configure spacing to your preference
- ✅ Recursive Processing: Handles deeply nested structures
- ✅ TypeScript Support: Full type definitions included
- ✅ Zero Dependencies: Lightweight and fast
- ✅ Multiple Import Styles: Named imports, default imports, or class instantiation
📦 Installation
npm install loft-codec🚀 Quick Start
LOFT supports multiple import styles to fit your coding preferences:
Style A: Named Import (Recommended)
import { stringify } from "loft-codec";
const data = {
name: "Alice",
age: 30,
active: true,
balance: null,
};
console.log(stringify(data));Style B: Default Import with Static Method
import Loft from "loft-codec";
const data = { name: "Bob", age: 25 };
const loftString = Loft.stringify(data);
console.log(loftString);Style C: Class Instantiation
import Loft from "loft-codec";
const loft = new Loft();
const data = { name: "Charlie", age: 28 };
console.log(loft.stringify(data));Output (Pure LOFT Format - Default):
name: Alice
age: 30
active: +
balance: ~⚡ Key Example: See the Difference
Here's a real-world example showing LOFT's advantages over JSON:
import { stringify } from "loft-codec";
// Typical API response with user data
const users = [
{ id: 1, name: "Alice", active: true, role: "admin" },
{ id: 2, name: "Bob", active: false, role: "user" },
{ id: 3, name: "Charlie", active: true, role: "user" },
];
console.log("JSON:", JSON.stringify(users, null, 2));
console.log("\nLOFT:", stringify(users));JSON Output (263 characters):
[
{
"id": 1,
"name": "Alice",
"active": true,
"role": "admin"
},
{
"id": 2,
"name": "Bob",
"active": false,
"role": "user"
},
{
"id": 3,
"name": "Charlie",
"active": true,
"role": "user"
}
]LOFT Output (89 characters - 66% smaller!):
[id, name, active, role] {
1, Alice, +, admin;
2, Bob, -, user;
3, Charlie, +, user
}Note: Matrix format uses braces
{}even in pure LOFT mode for clarity in tabular data.
📖 Format Rules
Booleans
true→+false→-
Null
null→~
Strings
- Simple strings (no special chars):
hello,world,Alice - Strings with special characters (
:,{,},,,;,[,]): Wrapped in backticks- Example:
key:value→`key:value` - Example:
Hello, World!→`Hello, World!`
- Example:
Numbers
- Rendered as-is:
42,3.14,-10,0.001
Arrays
Simple List Format
When an array contains simple values or mixed types:
[1, 2, 3, 4, 5];LOFT Output:
{
1;
2;
3;
4;
5
}Matrix Format (Array of Objects with Identical Keys)
When an array contains objects with the same keys, LOFT uses an optimized tabular format:
[
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
];LOFT Output:
[name, age] {
Alice, 30;
Bob, 25
}Benefits:
- Reduces redundancy
- More readable for tabular data
- Significantly fewer tokens
Objects
Objects are represented with clear key-value pairs and proper indentation.
Pure LOFT Format (Default)
Indentation-based structure without braces:
{
user: 'John',
settings: {
theme: 'dark',
notifications: true
}
}LOFT Output:
user: John
settings:
theme: dark
notifications: +Legacy Format (with useBraces: true)
Braced structure for backward compatibility:
{
user: John
settings {
theme: dark
notifications: +
}
}💡 Examples
Basic Serialization
import { stringify } from "loft-codec";
const apiResponse = {
status: "success",
code: 200,
data: {
users: [
{ id: 1, username: "alice123", verified: true },
{ id: 2, username: "bob_smith", verified: false },
],
total: 2,
},
errors: null,
};
console.log(stringify(apiResponse));Output (Pure LOFT Format):
status: success
code: 200
data:
users:
[id, username, verified] {
1, alice123, +;
2, bob_smith, -
}
total: 2
errors: ~Nested Structures
const company = {
name: "TechCorp",
founded: 2020,
active: true,
address: {
street: "123 Main St",
city: "San Francisco",
country: "USA",
},
};
console.log(stringify(company));Output (Pure LOFT Format):
name: TechCorp
founded: 2020
active: +
address:
street: `123 Main St`
city: San Francisco
country: USAComplex Data with Arrays
const dataset = {
metadata: {
version: "2.0",
timestamp: 1700000000,
},
records: [
{ id: 1, type: "A", value: 100, active: true },
{ id: 2, type: "B", value: 200, active: false },
{ id: 3, type: "A", value: 150, active: true },
],
tags: ["important", "reviewed", "archived"],
};
console.log(stringify(dataset));Output (Pure LOFT Format):
metadata:
version: `2.0`
timestamp: 1700000000
records:
[id, type, value, active] {
1, A, 100, +;
2, B, 200, -;
3, A, 150, +
}
tags: {
important;
reviewed;
archived
}🎯 Design Principles
- Token Efficiency: Minimize token usage for LLM contexts
- Readability: Human-readable format with proper indentation
- Lossless: Preserve all data during serialization
- Type Safety: Clear type representation for all JavaScript types
- Smart Formatting: Automatic detection of optimal array format
- Simplicity: Minimal syntax with maximum clarity
🤔 Why LOFT?
Traditional JSON can be verbose and token-inefficient. LOFT optimizes for:
| Feature | JSON | LOFT | Savings |
| --------------- | ------------------- | ----------------- | ------- |
| Boolean true | true (4 chars) | + (1 char) | 75% |
| Boolean false | false (5 chars) | - (1 char) | 80% |
| Null value | null (4 chars) | ~ (1 char) | 75% |
| Simple strings | "hello" (7 chars) | hello (5 chars) | 29% |
| Object keys | "key": (6 chars) | key: (4 chars) | 33% |
Additional Benefits:
- Matrix format for arrays reduces redundancy in tabular data
- No quotes needed for simple strings (no special characters)
- Cleaner syntax improves human readability
- Smaller payload size for data transmission
Use Cases
- 🤖 LLM Prompts: Reduce token count in AI conversations
- 📡 Data Transmission: Smaller payload sizes
- 📝 Configuration Files: More readable than JSON
- 📊 Logging: Compact and human-friendly logs
- 💾 Data Storage: Efficient serialization format
