topazcube 0.0.2 → 0.1.0

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.
package/src/utils.js DELETED
@@ -1,216 +0,0 @@
1
- export function reactive(name, object, callback, path = "", ID = null) {
2
- if (object === null || typeof object !== "object") {
3
- return object
4
- }
5
- for (const property in object) {
6
- object[property] = reactive(name, object[property], callback, path + "/" + property, ID)
7
- }
8
-
9
- return new Proxy(object, {
10
- get(target, property) {
11
- return Reflect.get(...arguments)
12
- },
13
- set(target, property, value) {
14
- let pn = path + "/" + property
15
- let newvalue = reactive(name, value, callback, pn, ID)
16
- callback(name, "replace", target, pn, newvalue, ID)
17
- return Reflect.set(target, property, newvalue)
18
- },
19
- deleteProperty(target, property) {
20
- let pn = path + "/" + property
21
- delete target[property]
22
- callback(name, "delete", target, pn, null, ID)
23
- return true
24
- },
25
- })
26
- }
27
-
28
- export function deepGet(obj, path) {
29
- let paths = ("" + path).split("/")
30
- let len = paths.length
31
- for (let i = 0; i < len; i++) {
32
- if (obj[paths[i]] == undefined) {
33
- return undefined
34
- } else {
35
- obj = obj[paths[i]]
36
- }
37
- }
38
- return obj
39
- }
40
-
41
- export function deepSet(obj, path, value) {
42
- let paths = ("" + path).split("/")
43
- let len = paths.length
44
- let i
45
- for (i = 0; i < len - 1; i++) {
46
- obj = obj[paths[i]]
47
- }
48
- obj[paths[i]] = value
49
- }
50
-
51
- // recursive clone oject, without properties that starts with _ (or __)
52
-
53
- export function clonewo_(obj, excludeStart = "_") {
54
- if (obj === null || typeof obj !== "object") {
55
- return obj
56
- }
57
-
58
- if (obj instanceof Map) {
59
- const mapClone = new Map()
60
- for (let [key, value] of obj) {
61
- mapClone.set(clonewo_(key, excludeStart), clonewo_(value, excludeStart))
62
- }
63
- return mapClone
64
- }
65
-
66
- let clone
67
- if (Array.isArray(obj)) {
68
- clone = []
69
- for (let i = 0; i < obj.length; i++) {
70
- clone[i] = clonewo_(obj[i], excludeStart)
71
- }
72
- } else {
73
- clone = {}
74
- for (let key in obj) {
75
- if (obj.hasOwnProperty(key) && !key.startsWith(excludeStart)) {
76
- if (typeof obj[key] === "object") {
77
- clone[key] = clonewo_(obj[key], excludeStart)
78
- } else {
79
- clone[key] = obj[key]
80
- }
81
- }
82
- }
83
- }
84
-
85
- return clone
86
- }
87
-
88
- export function msgop(op) {
89
- let nop = {}
90
- if (!op.o) {
91
- nop.op = "replace"
92
- } else {
93
- nop.op = {
94
- a: "add",
95
- r: "remove",
96
- d: "delete",
97
- t: "test",
98
- }[op.o]
99
- }
100
- nop.path = op.p
101
- nop.value = op.v
102
- return nop
103
- }
104
-
105
- export function opmsg(op, target, path, value) {
106
- let c = { p: path, v: value }
107
- if (op != "replace") {
108
- c.o = {
109
- add: "a",
110
- remove: "r",
111
- delete: "d",
112
- test: "t",
113
- }[op]
114
- }
115
- return c
116
- }
117
-
118
- var _lastUID = 1
119
-
120
- export function sdate() {
121
- return (Date.now() / 1000 - 1715000000) | 0
122
- }
123
-
124
- export function getUID() {
125
- let uid = sdate()
126
- if (uid <= _lastUID) {
127
- uid = _lastUID + 1
128
- }
129
- _lastUID = uid
130
- return uid
131
- }
132
-
133
- // - Fixed point encoding/decoding functions
134
- export function encode_uint32(uint, byteArray, offset = 0) {
135
- if (!byteArray) {
136
- byteArray = new Uint8Array(4)
137
- }
138
- let p = offset + 3
139
- byteArray[p--] = uint & 0xff
140
- uint >>= 8
141
- byteArray[p--] = uint & 0xff
142
- uint >>= 8
143
- byteArray[p--] = uint & 0xff
144
- uint >>= 8
145
- byteArray[p] = uint
146
- return byteArray
147
- }
148
-
149
- export function decode_uint32(byteArray, offset = 0) {
150
- let p = offset
151
- return ((byteArray[p++] & 0x7f) << 24) | (byteArray[p++] << 16) | (byteArray[p++] << 8) | byteArray[p]
152
- }
153
-
154
- export function encode_uint16(uint, byteArray, offset = 0) {
155
- if (!byteArray) {
156
- byteArray = new Uint8Array(2)
157
- }
158
- let p = offset + 1
159
- byteArray[p--] = uint & 0xff
160
- uint >>= 8
161
- byteArray[p] = uint
162
- return byteArray
163
- }
164
-
165
- export function decode_uint16(byteArray, offset = 0) {
166
- let p = offset
167
- return (byteArray[p++] << 8) | byteArray[p]
168
- }
169
-
170
- export function encode_fp248(float, byteArray, offset = 0) {
171
- const fp = Math.round(Math.abs(float) * 256)
172
- const enc = encode_uint32(fp, byteArray, offset)
173
- if (float < 0) {
174
- enc[offset] |= 0x80
175
- }
176
- return enc
177
- }
178
-
179
- export function decode_fp248(byteArray, offset = 0) {
180
- const divider = (byteArray[offset] & 0x80) === 0x80 ? -256 : 256
181
- byteArray[offset] &= 0x7f
182
- const fp = decode_uint32(byteArray, offset)
183
- return fp / divider
184
- }
185
-
186
- export function encode_fp1616(float, byteArray, offset = 0) {
187
- const fp = Math.round(Math.abs(float) * 65536)
188
- const enc = encode_uint32(fp, byteArray, offset)
189
- if (float < 0) {
190
- enc[offset] |= 0x80
191
- }
192
- return enc
193
- }
194
-
195
- export function decode_fp1616(byteArray, offset = 0) {
196
- const divider = (byteArray[offset] & 0x80) === 0x80 ? -65536 : 65536
197
- byteArray[offset] &= 0x7f
198
- const fp = decode_uint32(byteArray, offset)
199
- return fp / divider
200
- }
201
-
202
- export function encode_fp88(float, byteArray, offset = 0) {
203
- const fp = Math.round(Math.abs(float) * 256)
204
- const enc = encode_uint16(fp, byteArray, offset)
205
- if (float < 0) {
206
- enc[offset] |= 0x80
207
- }
208
- return enc
209
- }
210
-
211
- export function decode_fp88(byteArray, offset = 0) {
212
- const divider = (byteArray[offset] & 0x80) === 0x80 ? -256 : 256
213
- byteArray[offset] &= 0x7f
214
- const fp = decode_uint16(byteArray, offset)
215
- return fp / divider
216
- }