topazcube 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ /*
2
+ ┌─────────┬─────────┬──────┬───────────┬─────────────┬──────────────────┐
3
+ │ Color │ Example │ Text │ Background│ Bright Text │ Bright Background│
4
+ ├─────────┼─────────┼──────┼───────────┼─────────────┼──────────────────┤
5
+ │ Black │ Black │ 30 │ 40 │ 90 │ 100 │
6
+ │ Red │ Red │ 31 │ 41 │ 91 │ 101 │
7
+ │ Green │ Green │ 32 │ 42 │ 92 │ 102 │
8
+ │ Yellow │ Yellow │ 33 │ 43 │ 93 │ 103 │
9
+ │ Blue │ Blue │ 34 │ 44 │ 94 │ 104 │
10
+ │ Magenta │ Magenta │ 35 │ 45 │ 95 │ 105 │
11
+ │ Cyan │ Cyan │ 36 │ 46 │ 96 │ 106 │
12
+ │ White │ White │ 37 │ 47 │ 97 │ 107 │
13
+ │ Default │ │ 39 │ 49 │ 99 │ 109 │
14
+ └─────────┴─────────┴──────┴───────────┴─────────────┴──────────────────┘
15
+
16
+ ┌───────────┬─────┬─────┐
17
+ │ Effect │ On │ Off │
18
+ ├───────────┼─────┼─────┤
19
+ │ Bold │ 1 │ 21 │
20
+ │ Dim │ 2 │ 22 │
21
+ │ Underline │ 4 │ 24 │
22
+ │ Blink │ 5 │ 25 │
23
+ │ Reverse │ 7 │ 27 │
24
+ │ Hide │ 8 │ 28 │
25
+ └───────────┴─────┴─────┘
26
+ */
27
+
28
+ // Terminal control code constants
29
+ export const T_RESET = 0;
30
+ export const T_BOLD = 1;
31
+ export const T_DIM = 2;
32
+ export const T_UNDERLINE = 4;
33
+ export const T_BLINK = 5;
34
+ export const T_REVERSE = 7;
35
+ export const T_HIDE = 8;
36
+ export const T_BOLD_OFF = 21;
37
+ export const T_DIM_OFF = 22;
38
+ export const T_UNDERLINE_OFF = 24;
39
+ export const T_BLINK_OFF = 25;
40
+ export const T_REVERSE_OFF = 27;
41
+ export const T_HIDE_OFF = 28;
42
+
43
+ export const T_FG_BLACK = 30;
44
+ export const T_FG_RED = 31;
45
+ export const T_FG_GREEN = 32;
46
+ export const T_FG_YELLOW = 33;
47
+ export const T_FG_BLUE = 34;
48
+ export const T_FG_MAGENTA = 35;
49
+ export const T_FG_CYAN = 36;
50
+ export const T_FG_WHITE = 37;
51
+ export const T_FG_DEFAULT = 39;
52
+
53
+ export const T_BG_BLACK = 40;
54
+ export const T_BG_RED = 41;
55
+ export const T_BG_GREEN = 42;
56
+ export const T_BG_YELLOW = 43;
57
+ export const T_BG_BLUE = 44;
58
+ export const T_BG_MAGENTA = 45;
59
+ export const T_BG_CYAN = 46;
60
+ export const T_BG_WHITE = 47;
61
+ export const T_BG_DEFAULT = 49;
62
+
63
+ export const T_FG_BRIGHT_BLACK = 90;
64
+ export const T_FG_BRIGHT_RED = 91;
65
+ export const T_FG_BRIGHT_GREEN = 92;
66
+ export const T_FG_BRIGHT_YELLOW = 93;
67
+ export const T_FG_BRIGHT_BLUE = 94;
68
+ export const T_FG_BRIGHT_MAGENTA = 95;
69
+ export const T_FG_BRIGHT_CYAN = 96;
70
+ export const T_FG_BRIGHT_WHITE = 97;
71
+ export const T_FG_BRIGHT_DEFAULT = 99;
72
+
73
+ export const T_BG_BRIGHT_BLACK = 100;
74
+ export const T_BG_BRIGHT_RED = 101;
75
+ export const T_BG_BRIGHT_GREEN = 102;
76
+ export const T_BG_BRIGHT_YELLOW = 103;
77
+ export const T_BG_BRIGHT_BLUE = 104;
78
+ export const T_BG_BRIGHT_MAGENTA = 105;
79
+ export const T_BG_BRIGHT_CYAN = 106;
80
+ export const T_BG_BRIGHT_WHITE = 107;
81
+ export const T_BG_BRIGHT_DEFAULT = 109;
82
+
83
+
84
+ // Set terminal title
85
+ export function setTitle(title) {
86
+ process.stdout.write("\x1b]0;" + title + "\x07");
87
+ }
88
+
89
+ // Set terminal color / effect
90
+ export function setColor(color) {
91
+ process.stdout.write(`\x1b[${color}m`);
92
+ }
93
+
94
+ // Reset terminal color
95
+ export function resetColor() {
96
+ setColor(T_RESET);
97
+ }
98
+
99
+ // Clear line
100
+ export function clearLine() {
101
+ process.stdout.write("\x1b[2K");
102
+ }
103
+
104
+ // Erase to end of line
105
+ export function eraseEOL() {
106
+ process.stdout.write("\x1b[K");
107
+ }
108
+
109
+ // Clear screen
110
+ export function clear() {
111
+ process.stdout.write("\x1b[2J");
112
+ }
113
+
114
+ // Set cursor position
115
+ export function cursorPosition(line, column) {
116
+ process.stdout.write(`\x1b[${line};${column}H`);
117
+ }
118
+ // Move cursor up
119
+ export function cursorUp(lines = 1) {
120
+ process.stdout.write(`\x1b[${lines}A`);
121
+ }
122
+ // Move cursor down
123
+ export function cursorDown(lines = 1) {
124
+ process.stdout.write(`\x1b[${lines}B`);
125
+ }
126
+
127
+ // Move cursor forward
128
+ export function cursorForward(columns = 1) {
129
+ process.stdout.write(`\x1b[${columns}C`);
130
+ }
131
+ // Move cursor backward
132
+ export function cursorBackward(columns = 1) {
133
+ process.stdout.write(`\x1b[${columns}D`);
134
+ }
135
+
136
+ // Save cursor position
137
+ export function saveCursor() {
138
+ process.stdout.write("\x1b[s");
139
+ }
140
+
141
+ // Restore cursor position
142
+ export function restoreCursor() {
143
+ process.stdout.write("\x1b[u");
144
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,402 @@
1
+ import { Packr } from 'msgpackr';
2
+ import { FLOAT32_OPTIONS } from 'msgpackr';
3
+ const { ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
4
+
5
+ let packr = new Packr({
6
+ useFloat32: ALWAYS
7
+ });
8
+
9
+ export function encode(obj) {
10
+ return packr.pack(obj)
11
+ }
12
+
13
+ export function decode(data) {
14
+ return packr.unpack(data)
15
+ }
16
+
17
+ export function reactive(name, object, callback, path = '', excludedProperties = false) {
18
+ if (object === null || typeof object !== 'object') {
19
+ //console.log('--- Type not object', typeof object)
20
+ return object
21
+ }
22
+
23
+ function isReactive(p) {
24
+ let r = true
25
+ if (p.startsWith('_')) {
26
+ r = false
27
+ }
28
+ if (excludedProperties) {
29
+ if (excludedProperties[p]) {
30
+ r = false
31
+ }
32
+ }
33
+ if (path == '/entities') {
34
+ r = false
35
+ }
36
+ return r
37
+ }
38
+
39
+ for (const property in object) {
40
+ if (isReactive(property)) {
41
+ //console.log(`path '${path}', prop '${property}' is reactive`)
42
+ object[property] = reactive(
43
+ name,
44
+ object[property],
45
+ callback,
46
+ path + '/' + property,
47
+ excludedProperties
48
+ )
49
+ } else {
50
+ //console.log(`--- path '${path}', property '${property}' is NOT reactive`)
51
+ }
52
+ }
53
+ //console.log(`path '${path}' is reactive`)
54
+ return new Proxy(object, {
55
+ get(target, property, receiver) { // ...arguments
56
+ return Reflect.get(target, property, receiver)
57
+ },
58
+ set(target, property, value) {
59
+ let newvalue
60
+ let pn = path + '/' + String(property)
61
+ if (isReactive(property)) {
62
+ newvalue = reactive(name, value, callback, pn, excludedProperties)
63
+ callback(name, 'replace', target, pn, newvalue)
64
+ } else {
65
+ newvalue = value
66
+ }
67
+ return Reflect.set(target, property, newvalue)
68
+ },
69
+ deleteProperty(target, property) {
70
+ let pn = path + '/' + String(property)
71
+ delete target[property]
72
+ if (isReactive(property)) {
73
+ callback(name, 'delete', target, pn, null)
74
+ }
75
+ return true
76
+ },
77
+ })
78
+
79
+ }
80
+
81
+ export function deepGet(obj, path) {
82
+ path = path.trimLeft('/')
83
+ let paths = ('' + path).split('/').filter((p) => p)
84
+ let len = paths.length
85
+ for (let i = 0; i < len; i++) {
86
+ if (obj[paths[i]] == undefined) {
87
+ return undefined
88
+ } else {
89
+ obj = obj[paths[i]]
90
+ }
91
+ }
92
+ return obj
93
+ }
94
+
95
+ export function deepSet(obj, path, value) {
96
+ path = path.trimLeft('/')
97
+ let paths = ('' + path).split('/').filter((p) => p)
98
+ let len = paths.length
99
+ let i
100
+ for (i = 0; i < len - 1; i++) {
101
+ obj = obj[paths[i]]
102
+ }
103
+ obj[paths[i]] = value
104
+ }
105
+
106
+ // recursive clone oject, without properties that starts with _ (or __)
107
+
108
+ export function clonewo_(obj, excludeStart = '_') {
109
+ if (obj === null || typeof obj !== 'object') {
110
+ return obj
111
+ }
112
+
113
+ function isExcluded(key) {
114
+ let e = false
115
+ if (typeof (excludeStart) == 'string' && key.startsWith(excludeStart)) {
116
+ e = true
117
+ } else if (typeof(excludeStart) == 'object') {
118
+ if (excludeStart[key] || key.startsWith('_')) {
119
+ e = true
120
+ }
121
+ }
122
+ return e
123
+ }
124
+
125
+ if (obj instanceof Map) {
126
+ const mapClone = new Map()
127
+ for (let [key, value] of obj) {
128
+ mapClone.set(clonewo_(key, excludeStart), clonewo_(value, excludeStart))
129
+ }
130
+ return mapClone
131
+ }
132
+
133
+ let clone
134
+ if (Array.isArray(obj)) {
135
+ clone = []
136
+ for (let i = 0; i < obj.length; i++) {
137
+ clone[i] = clonewo_(obj[i], excludeStart)
138
+ }
139
+ } else {
140
+ clone = {}
141
+ for (let key in obj) {
142
+ if (obj.hasOwnProperty(key) && !isExcluded(key)) {
143
+ if (typeof obj[key] === 'object') {
144
+ clone[key] = clonewo_(obj[key], excludeStart)
145
+ } else {
146
+ clone[key] = obj[key]
147
+ }
148
+ }
149
+ }
150
+ }
151
+
152
+ return clone
153
+ }
154
+
155
+ export function limitPrecision(obj) {
156
+ if (Array.isArray(obj)) {
157
+ return obj.map(limitPrecision)
158
+ } else if (obj !== null && typeof obj === 'object') {
159
+ const result = {}
160
+ for (const key in obj) {
161
+ result[key] = limitPrecision(obj[key])
162
+ }
163
+ return result
164
+ } else if (typeof obj === 'number') {
165
+ if (Number.isInteger(obj)) {
166
+ return obj
167
+ } else {
168
+ // Limit to max 3 decimal digits, not fixed
169
+ return parseFloat(obj.toFixed(3))
170
+ }
171
+ } else {
172
+ return obj
173
+ }
174
+ }
175
+
176
+ export function msgop(op:any) {
177
+ let nop:any = {}
178
+ if (!op.o) {
179
+ nop.op = 'replace'
180
+ } else {
181
+ nop.op = {
182
+ a: 'add',
183
+ r: 'remove',
184
+ d: 'delete',
185
+ t: 'test',
186
+ }[op.o]
187
+ }
188
+ nop.path = op.p
189
+ nop.value = op.v
190
+ return nop
191
+ }
192
+
193
+ export function opmsg(op, target, path, value) {
194
+ let c:any = { p: path, v: value }
195
+ if (op != 'replace') {
196
+ c.o = {
197
+ add: 'a',
198
+ remove: 'r',
199
+ delete: 'd',
200
+ test: 't',
201
+ }[op]
202
+ }
203
+ return c
204
+ }
205
+
206
+ // a function that converts an int to a hexa string
207
+ // (8 characters long)
208
+ export function int2hex(int) {
209
+ return int.toString(16)
210
+ }
211
+
212
+ // a function that converts a hexa string to an int
213
+ export function hex2int(str) {
214
+ if (str.length % 2) {
215
+ str = '0' + str
216
+ }
217
+ return BigInt('0x' + str)
218
+ }
219
+
220
+ // - Fixed point encoding/decoding functions
221
+
222
+ // 32-bit unsigned integer encoding
223
+ export function encode_uint32(uint, byteArray, offset = 0) {
224
+ if (!byteArray) {
225
+ byteArray = new Uint8Array(4)
226
+ }
227
+ let p = offset + 3
228
+ byteArray[p--] = uint & 0xff
229
+ uint >>= 8
230
+ byteArray[p--] = uint & 0xff
231
+ uint >>= 8
232
+ byteArray[p--] = uint & 0xff
233
+ uint >>= 8
234
+ byteArray[p] = uint
235
+ return byteArray
236
+ }
237
+
238
+ // 32-bit unsigned integer decoding
239
+ export function decode_uint32(byteArray, offset = 0) {
240
+ let p = offset
241
+ return (
242
+ ((byteArray[p++] & 0x7f) << 24) |
243
+ (byteArray[p++] << 16) |
244
+ (byteArray[p++] << 8) |
245
+ byteArray[p]
246
+ )
247
+ }
248
+
249
+ // 24-bit unsigned integer encoding
250
+ export function encode_uint24(uint, byteArray, offset = 0) {
251
+ if (!byteArray) {
252
+ byteArray = new Uint8Array(3)
253
+ }
254
+ let p = offset + 2
255
+ byteArray[p--] = uint & 0xff
256
+ uint >>= 8
257
+ byteArray[p--] = uint & 0xff
258
+ uint >>= 8
259
+ byteArray[p] = uint
260
+ return byteArray
261
+ }
262
+
263
+ // 24-bit unsigned integer decoding
264
+ export function decode_uint24(byteArray, offset = 0) {
265
+ let p = offset
266
+ return (
267
+ (byteArray[p++] << 16) |
268
+ (byteArray[p++] << 8) |
269
+ byteArray[p]
270
+ )
271
+ }
272
+
273
+ // 16-bit unsigned integer encoding
274
+ export function encode_uint16(uint, byteArray, offset = 0) {
275
+ if (!byteArray) {
276
+ byteArray = new Uint8Array(2)
277
+ }
278
+ let p = offset + 1
279
+ byteArray[p--] = uint & 0xff
280
+ uint >>= 8
281
+ byteArray[p] = uint
282
+ return byteArray
283
+ }
284
+
285
+ // 16-bit unsigned integer decoding
286
+ export function decode_uint16(byteArray, offset = 0) {
287
+ let p = offset
288
+ return (byteArray[p++] << 8) | byteArray[p]
289
+ }
290
+
291
+ // 24.8 bit ====================================================================
292
+
293
+ // 24.8-bit fixed point encoding
294
+ export function encode_fp248(float, byteArray, offset = 0) {
295
+ const fp = Math.round(Math.abs(float) * 256)
296
+ encode_uint32(fp, byteArray, offset)
297
+ if (float < 0) {
298
+ byteArray[offset] |= 0x80
299
+ }
300
+ }
301
+
302
+ // 24.8-bit fixed point decoding
303
+ export function decode_fp248(byteArray, offset = 0) {
304
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -256 : 256
305
+ byteArray[offset] &= 0x7f
306
+ const fp = decode_uint32(byteArray, offset)
307
+ return fp / divider
308
+ }
309
+
310
+ // 16.8 bit ====================================================================
311
+
312
+ // 16.8-bit fixed point encoding (3 bytes)
313
+ export function encode_fp168(float, byteArray, offset = 0) {
314
+ const fp = Math.round(Math.abs(float) * 256)
315
+ encode_uint24(fp, byteArray, offset)
316
+ if (float < 0) {
317
+ byteArray[offset] |= 0x80
318
+ }
319
+ }
320
+
321
+ // 16.8-bit fixed point decoding (3 bytes)
322
+ export function decode_fp168(byteArray, offset = 0) {
323
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -256 : 256
324
+ byteArray[offset] &= 0x7f
325
+ const fp = decode_uint24(byteArray, offset)
326
+ return fp / divider
327
+ }
328
+
329
+ // 16.16 bit ===================================================================
330
+
331
+ // 16.16-bit fixed point encoding
332
+ export function encode_fp1616(float, byteArray, offset = 0) {
333
+ const fp = Math.round(Math.abs(float) * 65536)
334
+ encode_uint32(fp, byteArray, offset)
335
+ if (float < 0) {
336
+ byteArray[offset] |= 0x80
337
+ }
338
+ }
339
+
340
+ // 16.16-bit fixed point decoding
341
+ export function decode_fp1616(byteArray, offset = 0) {
342
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -65536 : 65536
343
+ byteArray[offset] &= 0x7f
344
+ const fp = decode_uint32(byteArray, offset)
345
+ return fp / divider
346
+ }
347
+
348
+ // 8.8 bit =====================================================================
349
+
350
+ // 8.8-bit fixed point encoding
351
+ export function encode_fp88(float, byteArray, offset = 0) {
352
+ const fp = Math.round(Math.abs(float) * 256)
353
+ encode_uint16(fp, byteArray, offset)
354
+ if (float < 0) {
355
+ byteArray[offset] |= 0x80
356
+ }
357
+ }
358
+
359
+ // 8.8-bit fixed point decoding
360
+ export function decode_fp88(byteArray, offset = 0) {
361
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -256 : 256
362
+ byteArray[offset] &= 0x7f
363
+ const fp = decode_uint16(byteArray, offset)
364
+ return fp / divider
365
+ }
366
+
367
+ // 4.12 bit ====================================================================
368
+
369
+ // 4.12-bit fixed point encoding
370
+ export function encode_fp412(float, byteArray, offset = 0) {
371
+ const fp = Math.round(Math.abs(float) * 4096)
372
+ encode_uint16(fp, byteArray, offset)
373
+ if (float < 0) {
374
+ byteArray[offset] |= 0x80
375
+ }
376
+ }
377
+
378
+ // 4.12-bit fixed point decoding
379
+ export function decode_fp412(byteArray, offset = 0) {
380
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -4096 : 4096
381
+ byteArray[offset] &= 0x7f
382
+ const fp = decode_uint16(byteArray, offset)
383
+ return fp / divider
384
+ }
385
+
386
+ // 1.7 bit =====================================================================
387
+
388
+ // 1.7-bit fixed point encoding
389
+ export function encode_fp17(float, byteArray, offset = 0) {
390
+ const fp = Math.round(Math.abs(float) * 128)
391
+ byteArray[offset] = fp
392
+ if (float < 0) {
393
+ byteArray[offset] |= 0x80
394
+ }
395
+ }
396
+
397
+ // 1.7-bit fixed point decoding
398
+ export function decode_fp17(byteArray, offset = 0) {
399
+ const divider = (byteArray[offset] & 0x80) === 0x80 ? -128.0 : 128.0
400
+ byteArray[offset] &= 0x7f
401
+ return byteArray[offset] / divider
402
+ }