Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xpodev/quickstruct-js
https://github.com/xpodev/quickstruct-js
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/xpodev/quickstruct-js
- Owner: xpodev
- Created: 2023-11-05T00:27:59.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-06T16:58:17.000Z (about 1 year ago)
- Last Synced: 2023-11-07T01:02:18.656Z (about 1 year ago)
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Quick Struct
Quick Struct is a simple and fast way to create cpp structs in TypeScript.
> [!WARNING]
> This package is still in development and is not published to npm and yarn yet.## Installation
```bash
npm install quickstruct
```## Usage
```typescript
import { Int, Char, Struct, toBytes, fromBytes } from 'quickstruct';@Struct()
class SimpleStruct {
int1 = Int();
char1 = Char();
char2 = Char();
}const simpleStruct = new SimpleStruct();
simpleStruct.int1 = 1234;
simpleStruct.char1 = 'a'; // 97
simpleStruct.char2 = 600; // Overflow: 600 % 256 = 88const bytes = toBytes(simpleStruct);
console.log(bytes);
const simpleStruct2 = fromBytes(SimpleStruct, bytes);
console.log({...simpleStruct2});console.log(simpleStruct2.int1 + 100);
```
Output:
```bash
[ 210, 0, 0, 0, 97, 88 ]
SimpleStruct { int1: 1234, char1: 'a', char2: 88 }
1334
```## Types
The following types are supported:
| Type | Description | Displayed as |
| --- | --- | --- |
| Bool | 1 byte boolean | boolean |
| Char | 1 byte character | string |
| Byte | 1 byte character | number |
| Short | 2 byte integer | number |
| Int | 4 byte integer | number |
| Float | 4 byte float | number |
| Long | 8 byte integer | number |
| Double | 8 byte float | number |
| LongLong | 16 byte integer | number |
| LongDouble | 16 byte float | number |
| Str | String with dynamic length | string |
| Null | 1 byte null | null |## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.