https://github.com/butterdebugger/trufflebyte
https://github.com/butterdebugger/trufflebyte
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/butterdebugger/trufflebyte
- Owner: ButterDebugger
- License: mit
- Created: 2024-11-12T15:00:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-01T18:29:38.000Z (8 months ago)
- Last Synced: 2025-07-09T05:07:31.115Z (7 months ago)
- Language: TypeScript
- Size: 115 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TruffleByte
[](https://jsr.io/@debutter/trufflebyte)
[](https://jsr.io/@debutter/trufflebyte)
A simple and lightweight library designed to provide efficient binary
serialization for JavaScript objects. This makes it easy to encode JavaScript
data structures into compact binary formats, saving space and improving
transmission speeds.
## Installation
For Node.js:
```bash
npx jsr add @debutter/trufflebyte
```
For Deno:
```bash
deno add jsr:@debutter/trufflebyte
```
For Browsers:
```javascript
import * as TruffleByte from "https://esm.sh/jsr/@debutter/trufflebyte@VERSION";
```
## Usage
Example usage for encoding and decoding objects:
```javascript
import { decode, encode } from "@debutter/trufflebyte";
const data = {
message: "Hello, world!"
};
// Encode to binary
const encoded = encode(data);
// Decode from binary
const decoded = decode(encoded);
console.log(decoded); // { message: "Hello, world!" }
```
## Supported Data Types
TruffleByte supports a wide range of JavaScript data types, including:
- Objects
- Arrays
- Strings
- Numbers
- BigInts
- Booleans
- Null
- Undefined
- Dates
- Sets
- Maps
- Regular Expressions
- URLs
## Extensibility
TruffleByte is designed to be extensible, allowing you to add support for custom data
types. You can do this by creating your own transformers to handle specific data
types.
```javascript
import { registerTransformer } from "@debutter/trufflebyte";
// Define a custom transformer for a specific data type
const MyCustomTransformer = registerTransformer(0, {
isApplicable: (value) => value instanceof MyCustomType,
serialize: (encoder, value) => {
encoder.write(/* Encode the value */);
// ...
},
deserialize: (decoder) => {
return new MyCustomType(/* Decode the value */);
}
});
```
Additionally, you can also chain together other transformers to encode and
decode more complex data structures.
```javascript
import { NumberTransformer, registerTransformer } from "@debutter/trufflebyte";
// Define a custom transformer for a specific data type
const Vector2dTransformer = registerTransformer(0, {
isApplicable: (value) => value instanceof Vector2d,
serialize: (encoder, vector) => {
encoder.chain(NumberTransformer, vector.x);
encoder.chain(NumberTransformer, vector.y);
},
deserialize: (decoder) => {
const x = decoder.chain(NumberTransformer);
const y = decoder.chain(NumberTransformer);
return new Vector2d(x, y);
}
});
```
Note that custom transformer tags ranging from 0 to 127 are reserved by this
library and are subject to being taken.
# Contributing
Contributions are welcome! Please feel free to submit a Pull Request.