-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
53 lines (42 loc) · 1.23 KB
/
example.ts
File metadata and controls
53 lines (42 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { BufferBuilder } from "./buffers.js";
import { int8, string } from "./types.js";
// Create a new buffer builder
const builder = new BufferBuilder();
// Register the types
builder.registerType(int8);
builder.registerType(string);
// Create a buffer with the following structure
const descriptor = {
age: "int8",
name: "string",
};
// Create a new buffer with the same structure
const { buffer, ops } = builder.createBuffer<{
age: number;
name: string;
}>(descriptor);
// Set the properties
ops.age.set(25);
ops.name.set("John Doe");
// Get the properties
console.log(ops.age.get()); // Output: 25
console.log(ops.name.get()); // Output: John Doe
// Create function to build objects with the same structure
const build = builder.createBuild<{
age: number;
name: string;
}>(descriptor);
// Create a new buffer with the same structure and set the properties
const myObject = build({
age: 25,
name: "John Doe",
});
// copy the buffer
const copyBuffer = myObject.copy();
// Create a wrapper function to create objects from the buffer
const wrapper = builder.createWrapper<{
age: number;
name: string;
}>(descriptor);
// Create a new buffer with the same structure and set the properties
const myObject2 = wrapper(copyBuffer);