~kiito/bare-js

ref: 4bc58daa969ce2a73bd5a40123a707567b6c3de5 bare-js/library/example.mjs -rw-r--r-- 2.5 KiB
4bc58daa — Emma Implemented schema converter 7 months ago
                                                                                
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Bare from './bare.mjs';

class Point extends Bare.ArrayFixed {
	static length = 3;
	static type = Bare.F32;
}

class Test1 extends Bare.Struct {
	static entries = [
		['str', Bare.String],
		['pos', Point],
		['verts', class extends Bare.Array {
			static type = Point;
		}],
		['test', Bare.F64],
		['flag', Bare.Bool],
	];
}

let test1 = {
	str: "Hello World",
	pos: [0, -3, 2.5],
	verts: [
		[-.5, 6900, 2e-6],
		[Math.PI, Infinity, NaN],
	],
	test: Math.PI,
	flag: false,
};

class Test2 extends Bare.Struct {
	static entries = [
		['vals', class extends Bare.Array {
			static type = Bare.Int;
		}],
		['uint', Bare.UInt],
	];
}

let test2 = {
	'vals': [420, -360, 0xFF_FFFF_FFFF],
	'uint': 365555,
};

class ChannelEnum extends Bare.Enum {
	static keys = Bare.mapEnum(this, {
		0: 'RED',
		1: 'BLUE',
		2: 'GREEN',
		10: 'ALL',
	});
}

class Pair extends Bare.Struct {
	static entries = [
		['channel', ChannelEnum],
		['value', Bare.F64],
	];
}

class Test3 extends Bare.ArrayFixed {
	static length = 2;
	static type = Pair;
}

let test3 = [
	{
		channel: ChannelEnum.RED,
		value: 2 * Math.PI,
	},
	{
		channel: ChannelEnum.ALL,
		value: 6.9,
	},
];

class Address extends Bare.Struct {
	static entries = [
		['address', class extends Bare.ArrayFixed {
			static length = 4;
			static type = Bare.String;
		}],
		['city', Bare.String],
		['state', Bare.String],
		['country', Bare.String],
	];
}

let addrTest = Uint8Array.from([
	0o016, 0o101, 0o144, 0o144, 0o162, 0o145, 0o163, 0o163, 0o040, 0o154, 0o151, 0o156, 0o145, 0o040, 0o061, 0o000,
	0o000, 0o000, 0o014, 0o124, 0o150, 0o145, 0o040, 0o142, 0o151, 0o147, 0o040, 0o143, 0o151, 0o164, 0o171, 0o007,
	0o104, 0o162, 0o145, 0o156, 0o164, 0o150, 0o145, 0o017, 0o124, 0o150, 0o145, 0o040, 0o116, 0o145, 0o164, 0o150,
	0o145, 0o162, 0o154, 0o141, 0o156, 0o144, 0o163,
]);

/**
 * hexdump -b to array:
 * find: '([0-7]{3}) ?'
 * replace: '0o$1, '
 */


(() => {
	console.log(test1);
	let test1_bin = Test1.pack(test1);
	console.log(test1_bin);
	let [test1_un, t1l] = Test1.unpack(test1_bin);
	console.log(test1_un);

	console.log("-------------------");
	console.log(test2);
	let test2_bin = Test2.pack(test2);
	console.log(test2_bin);
	let [test2_un, t2l] = Test2.unpack(test2_bin);
	console.log(test2_un);

	console.log("-------------------");
	console.log(test3);
	let test3_bin = Test3.pack(test3);
	console.log(test3_bin);
	let [test3_un, t3l] = Test3.unpack(test3_bin);
	console.log(test3_un);

	console.log("-------------------");
	let [addr, al] = Address.unpack(addrTest);
	console.log(addr);
})();