@amodx/binary 0.0.11 → 0.0.12

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.
Files changed (34) hide show
  1. package/Arrays/BitArray.d.ts +13 -0
  2. package/Arrays/BitArray.js +63 -0
  3. package/Arrays/HalfNibbleArray.d.ts +13 -0
  4. package/Arrays/HalfNibbleArray.js +63 -0
  5. package/Arrays/NibbleArray.d.ts +13 -0
  6. package/Arrays/NibbleArray.js +63 -0
  7. package/Object/BinaryObject.d.ts +2 -2
  8. package/Object/Functions/BufferToObject.d.ts +3 -3
  9. package/Struct/Classes/BinaryStruct.d.ts +4 -5
  10. package/Struct/Classes/BinaryStruct.js +11 -158
  11. package/Struct/Classes/BinraryStructBase.d.ts +3 -4
  12. package/Struct/Classes/BinraryStructBase.js +30 -144
  13. package/Struct/Classes/InstantiatedStruct.d.ts +7 -3
  14. package/Struct/Classes/InstantiatedStruct.js +18 -6
  15. package/Struct/Classes/RemoteBinaryStruct.d.ts +2 -3
  16. package/Struct/Classes/RemoteBinaryStruct.js +2 -3
  17. package/Struct/Constants/StructPropertyTypes.d.ts +4 -1
  18. package/Struct/Constants/StructPropertyTypes.js +3 -0
  19. package/Struct/Functions/CreateIndex.d.ts +3 -0
  20. package/Struct/Functions/CreateIndex.js +217 -0
  21. package/Struct/Functions/CreateInstance.d.ts +3 -0
  22. package/Struct/Functions/CreateInstance.js +323 -0
  23. package/Struct/Functions/GetIndexData.d.ts +2 -0
  24. package/Struct/Functions/GetIndexData.js +29 -0
  25. package/Struct/Types/{RemoveBinaryStructData.types.d.ts → BinaryStructData.types.d.ts} +4 -3
  26. package/Struct/Types/BinaryStructSchema.types.d.ts +37 -1
  27. package/Struct/Types/index.d.ts +1 -1
  28. package/Struct/Types/index.js +1 -1
  29. package/Util/BinaryArrays.d.ts +8 -0
  30. package/Util/BinaryArrays.js +39 -0
  31. package/Util/BinaryUtil.d.ts +2 -4
  32. package/Util/BinaryUtil.js +4 -18
  33. package/package.json +1 -1
  34. /package/Struct/Types/{RemoveBinaryStructData.types.js → BinaryStructData.types.js} +0 -0
@@ -0,0 +1,323 @@
1
+ import { InstantiatedStruct } from "../Classes/InstantiatedStruct";
2
+ import { StructPropertyTypes } from "../Constants/StructPropertyTypes";
3
+ import { BinaryUtil } from "../../Util/BinaryUtil";
4
+ import { GetIndexData } from "./GetIndexData";
5
+ import { TypedArrayClassMap } from "../../Util/TypedArrayMap";
6
+ import { BinaryArrays } from "../../Util/BinaryArrays";
7
+ import { BitArray } from "../../Arrays/BitArray";
8
+ const vector2Indexes = { x: 0, y: 1 };
9
+ const vector3Indexes = { x: 0, y: 1, z: 2 };
10
+ const vector4Indexes = { x: 0, y: 1, z: 2, w: 3 };
11
+ export function CreateInstance(data) {
12
+ const index = new DataView(data.indexBuffer);
13
+ const GeneratedClass = class extends InstantiatedStruct {
14
+ constructor() {
15
+ super();
16
+ this.structSize = data.structSize;
17
+ this.structArrayIndexes = data.structArrayIndexes;
18
+ }
19
+ _props = new Map();
20
+ };
21
+ const keys = [];
22
+ Object.defineProperty(GeneratedClass.prototype, "createClone", {
23
+ get() {
24
+ return () => {
25
+ const clone = new GeneratedClass();
26
+ if (this.structData?.buffer)
27
+ clone.setBuffer(this.structData.buffer);
28
+ clone.setIndex(this.structArrayIndex);
29
+ return clone;
30
+ };
31
+ },
32
+ });
33
+ Object.defineProperty(GeneratedClass.prototype, "getKeys", {
34
+ get() {
35
+ return () => keys;
36
+ },
37
+ });
38
+ const seralize = (parent) => {
39
+ const object = {};
40
+ for (const [key, propertyByteIndex] of Object.entries(data.indexMap)) {
41
+ const [byteIndex, bitOffSet, bitSize, length, type] = GetIndexData(index, propertyByteIndex);
42
+ if (type == StructPropertyTypes.Boolean) {
43
+ object[key] = Boolean(parent[key]);
44
+ }
45
+ if (type == StructPropertyTypes.TypedNumber) {
46
+ object[key] = Number(parent[key]);
47
+ }
48
+ if (type == StructPropertyTypes.TypedNumberArray) {
49
+ const array = new TypedArrayClassMap[bitSize](length);
50
+ for (let i = 0; i < length; i++) {
51
+ array[i] = parent[key][i];
52
+ }
53
+ object[key] = array;
54
+ }
55
+ if (type == StructPropertyTypes.BitArray) {
56
+ const array = new Uint8Array(Math.ceil(length / 8));
57
+ const view = new DataView(array.buffer);
58
+ for (let i = 0; i < length; i++) {
59
+ BinaryArrays.setBitArrayIndex(view, 0, i, parent[key][i]);
60
+ }
61
+ object[key] = array;
62
+ }
63
+ if (type == StructPropertyTypes.Vector2) {
64
+ object[key] = {
65
+ x: parent[key].x,
66
+ y: parent[key].y,
67
+ };
68
+ }
69
+ if (type == StructPropertyTypes.Vector3) {
70
+ object[key] = {
71
+ x: parent[key].x,
72
+ y: parent[key].y,
73
+ z: parent[key].z,
74
+ };
75
+ }
76
+ if (type == StructPropertyTypes.Vector4) {
77
+ object[key] = {
78
+ x: parent[key].x,
79
+ y: parent[key].y,
80
+ z: parent[key].z,
81
+ w: parent[key].w,
82
+ };
83
+ }
84
+ }
85
+ return object;
86
+ };
87
+ const deserialize = (parent, seralized) => {
88
+ for (const [key, propertyByteIndex] of Object.entries(data.indexMap)) {
89
+ const [byteIndex, bitOffSet, bitSize, length, type] = GetIndexData(index, propertyByteIndex);
90
+ if (seralized[key] === undefined)
91
+ continue;
92
+ if (type == StructPropertyTypes.Boolean) {
93
+ parent[key] = seralized[key] ? 1 : 0;
94
+ }
95
+ if (type == StructPropertyTypes.TypedNumber) {
96
+ parent[key] = Number(seralized[key]);
97
+ }
98
+ if (type == StructPropertyTypes.TypedNumberArray) {
99
+ for (let i = 0; i < seralized[key].length; i++) {
100
+ parent[key][i] = seralized[key][i];
101
+ }
102
+ }
103
+ if (type == StructPropertyTypes.BitArray) {
104
+ const bitArray = new BitArray(seralized[key]);
105
+ const t1 = [];
106
+ const t2 = [];
107
+ for (let i = 0; i < length; i++) {
108
+ parent[key][i] = bitArray[i];
109
+ t1[i] = parent[key][i];
110
+ t2[i] = bitArray[i];
111
+ }
112
+ }
113
+ if (type == StructPropertyTypes.Vector2) {
114
+ parent[key].x = seralized[key].x;
115
+ parent[key].y = seralized[key].y;
116
+ }
117
+ if (type == StructPropertyTypes.Vector3) {
118
+ parent[key].x = seralized[key].x;
119
+ parent[key].y = seralized[key].y;
120
+ parent[key].z = seralized[key].z;
121
+ }
122
+ if (type == StructPropertyTypes.Vector4) {
123
+ parent[key].x = seralized[key].x;
124
+ parent[key].y = seralized[key].y;
125
+ parent[key].z = seralized[key].z;
126
+ parent[key].w = seralized[key].w;
127
+ }
128
+ }
129
+ };
130
+ Object.defineProperty(GeneratedClass.prototype, "serialize", {
131
+ get() {
132
+ return () => seralize(this);
133
+ },
134
+ });
135
+ Object.defineProperty(GeneratedClass.prototype, "deserialize", {
136
+ get() {
137
+ return (data) => deserialize(this, data);
138
+ },
139
+ });
140
+ Object.defineProperty(GeneratedClass.prototype, "setDefaults", {
141
+ get() {
142
+ return () => {
143
+ for (const [id, value] of Object.entries(data.propertyDefaults)) {
144
+ const v = this[id];
145
+ if (Array.isArray(v) && Array.isArray(value)) {
146
+ for (let i = 0; i < value.length; i++) {
147
+ v[i] = value[i];
148
+ }
149
+ continue;
150
+ }
151
+ if (typeof v == "object" && typeof value == "object") {
152
+ for (const key of Object.keys(value)) {
153
+ v[key] = value[key];
154
+ }
155
+ continue;
156
+ }
157
+ if (typeof v == "number" && typeof value == "number") {
158
+ this[id] = value;
159
+ }
160
+ }
161
+ };
162
+ },
163
+ });
164
+ for (const [key, propertyByteIndex] of Object.entries(data.indexMap)) {
165
+ keys.push(key);
166
+ const [byteIndex, bitOffSet, bitSize, length, type] = GetIndexData(index, propertyByteIndex);
167
+ if (type == StructPropertyTypes.Boolean) {
168
+ Object.defineProperty(GeneratedClass.prototype, key, {
169
+ get() {
170
+ return BinaryUtil.getBitValue(this.structData.getUint8(byteIndex + this.structByteOffSet), bitOffSet, bitSize);
171
+ },
172
+ set(value) {
173
+ this.structData.setUint8(byteIndex + this.structByteOffSet, BinaryUtil.setBitValue(this.structData.getUint8(byteIndex + this.structByteOffSet), bitOffSet, value, bitSize));
174
+ },
175
+ });
176
+ }
177
+ if (type == StructPropertyTypes.TypedNumber) {
178
+ Object.defineProperty(GeneratedClass.prototype, key, {
179
+ get() {
180
+ return BinaryUtil.getTypedNumber(this.structData, byteIndex + this.structByteOffSet, bitSize);
181
+ },
182
+ set(value) {
183
+ BinaryUtil.setTypedNumber(this.structData, byteIndex + this.structByteOffSet, bitSize, value);
184
+ },
185
+ });
186
+ }
187
+ if (type == StructPropertyTypes.BitArray) {
188
+ Object.defineProperty(GeneratedClass.prototype, key, {
189
+ get() {
190
+ const self = this;
191
+ if (!self._props.has(key)) {
192
+ const proxy = new Proxy(new Array(length), {
193
+ get(target, index) {
194
+ return BinaryArrays.getBitArrayIndex(self.structData, byteIndex + self.structByteOffSet, +index);
195
+ },
196
+ set(target, index, value) {
197
+ BinaryArrays.setBitArrayIndex(self.structData, byteIndex + self.structByteOffSet, +index, value);
198
+ return true;
199
+ },
200
+ });
201
+ self._props.set(key, proxy);
202
+ }
203
+ return self._props.get(key);
204
+ },
205
+ });
206
+ }
207
+ if (type == StructPropertyTypes.TypedNumberArray) {
208
+ const typedNumberSize = BinaryUtil.getTypedSize(bitSize);
209
+ Object.defineProperty(GeneratedClass.prototype, key, {
210
+ get() {
211
+ if (!this._props)
212
+ this._props = new Map();
213
+ const self = this;
214
+ if (!self._props.has(key)) {
215
+ const proxy = new Proxy(new Array(length), {
216
+ get(target, index) {
217
+ return BinaryUtil.getTypedNumber(self.structData, byteIndex +
218
+ self.structByteOffSet +
219
+ +index * typedNumberSize, bitSize);
220
+ },
221
+ set(target, index, value) {
222
+ BinaryUtil.setTypedNumber(self.structData, byteIndex +
223
+ self.structByteOffSet +
224
+ +index * typedNumberSize, bitSize, value);
225
+ return true;
226
+ },
227
+ });
228
+ self._props.set(key, proxy);
229
+ }
230
+ return self._props.get(key);
231
+ },
232
+ });
233
+ }
234
+ if (type == StructPropertyTypes.Vector2) {
235
+ const typedNumberSize = BinaryUtil.getTypedSize(bitSize);
236
+ Object.defineProperty(GeneratedClass.prototype, key, {
237
+ get() {
238
+ if (!this._props)
239
+ this._props = new Map();
240
+ const self = this;
241
+ if (!self._props.has(key)) {
242
+ const proxy = new Proxy({}, {
243
+ get(target, property) {
244
+ return BinaryUtil.getTypedNumber(self.structData, byteIndex +
245
+ self.structByteOffSet +
246
+ vector2Indexes[property] *
247
+ typedNumberSize, bitSize);
248
+ },
249
+ set(target, property, value) {
250
+ BinaryUtil.setTypedNumber(self.structData, byteIndex +
251
+ self.structByteOffSet +
252
+ vector2Indexes[property] *
253
+ typedNumberSize, bitSize, value);
254
+ return true;
255
+ },
256
+ });
257
+ self._props.set(key, proxy);
258
+ }
259
+ return self._props.get(key);
260
+ },
261
+ });
262
+ }
263
+ if (type == StructPropertyTypes.Vector3) {
264
+ const typedNumberSize = BinaryUtil.getTypedSize(bitSize);
265
+ Object.defineProperty(GeneratedClass.prototype, key, {
266
+ get() {
267
+ if (!this._props)
268
+ this._props = new Map();
269
+ const self = this;
270
+ if (!self._props.has(key)) {
271
+ const proxy = new Proxy({}, {
272
+ get(target, property) {
273
+ return BinaryUtil.getTypedNumber(self.structData, byteIndex +
274
+ self.structByteOffSet +
275
+ vector3Indexes[property] *
276
+ typedNumberSize, bitSize);
277
+ },
278
+ set(target, property, value) {
279
+ BinaryUtil.setTypedNumber(self.structData, byteIndex +
280
+ self.structByteOffSet +
281
+ vector3Indexes[property] *
282
+ typedNumberSize, bitSize, value);
283
+ return true;
284
+ },
285
+ });
286
+ self._props.set(key, proxy);
287
+ }
288
+ return self._props.get(key);
289
+ },
290
+ });
291
+ }
292
+ if (type == StructPropertyTypes.Vector4) {
293
+ const typedNumberSize = BinaryUtil.getTypedSize(bitSize);
294
+ Object.defineProperty(GeneratedClass.prototype, key, {
295
+ get() {
296
+ if (!this._props)
297
+ this._props = new Map();
298
+ const self = this;
299
+ if (!self._props.has(key)) {
300
+ const proxy = new Proxy({}, {
301
+ get(target, property) {
302
+ return BinaryUtil.getTypedNumber(self.structData, byteIndex +
303
+ self.structByteOffSet +
304
+ vector4Indexes[property] *
305
+ typedNumberSize, bitSize);
306
+ },
307
+ set(target, property, value) {
308
+ BinaryUtil.setTypedNumber(self.structData, byteIndex +
309
+ self.structByteOffSet +
310
+ vector4Indexes[property] *
311
+ typedNumberSize, bitSize, value);
312
+ return true;
313
+ },
314
+ });
315
+ self._props.set(key, proxy);
316
+ }
317
+ return self._props.get(key);
318
+ },
319
+ });
320
+ }
321
+ }
322
+ return new GeneratedClass();
323
+ }
@@ -0,0 +1,2 @@
1
+ export declare const GetIndexData: (data: DataView, indexBufferIndex: number) => [byteIndex: number, bitOffSet: number, bitSize: number, length: number, type: number];
2
+ export declare const SetIndexData: (data: DataView, indexBufferIndex: number, byteIndex: number, bitOffSet: number, bitSize: number, length: number, type: number) => number;
@@ -0,0 +1,29 @@
1
+ import { BinaryNumberTypes, ByteCounts } from "../../Constants/BinaryTypes";
2
+ import { BinaryUtil } from "../../Util/BinaryUtil";
3
+ const StuctIndexData = [0, 0, 0, 0, 0];
4
+ export const GetIndexData = (data, indexBufferIndex) => {
5
+ StuctIndexData[0] = data.getUint32(indexBufferIndex);
6
+ indexBufferIndex += BinaryUtil.getTypedSize(BinaryNumberTypes.Uint32);
7
+ StuctIndexData[1] = data.getUint8(indexBufferIndex);
8
+ indexBufferIndex += BinaryUtil.getTypedSize(BinaryNumberTypes.Uint8);
9
+ StuctIndexData[2] = data.getUint8(indexBufferIndex);
10
+ indexBufferIndex += BinaryUtil.getTypedSize(BinaryNumberTypes.Uint8);
11
+ StuctIndexData[3] = data.getUint32(indexBufferIndex);
12
+ indexBufferIndex += BinaryUtil.getTypedSize(BinaryNumberTypes.Uint32);
13
+ StuctIndexData[4] = data.getUint8(indexBufferIndex);
14
+ indexBufferIndex += BinaryUtil.getTypedSize(BinaryNumberTypes.Uint8);
15
+ return StuctIndexData;
16
+ };
17
+ export const SetIndexData = (data, indexBufferIndex, byteIndex, bitOffSet, bitSize, length, type) => {
18
+ data.setUint32(indexBufferIndex, byteIndex);
19
+ indexBufferIndex += ByteCounts.Uint32;
20
+ data.setUint8(indexBufferIndex, bitOffSet);
21
+ indexBufferIndex += ByteCounts.Uint8;
22
+ data.setUint8(indexBufferIndex, bitSize);
23
+ indexBufferIndex += ByteCounts.Uint8;
24
+ data.setUint32(indexBufferIndex, length);
25
+ indexBufferIndex += ByteCounts.Uint32;
26
+ data.setUint8(indexBufferIndex, type);
27
+ indexBufferIndex += ByteCounts.Uint8;
28
+ return indexBufferIndex;
29
+ };
@@ -1,9 +1,10 @@
1
1
  import { BufferTypes } from "../../Util/BufferTypes";
2
- export type RemoteBinaryStructData = {
2
+ export type BinaryStructData = {
3
3
  buffer: BufferTypes;
4
4
  bufferSize: number;
5
5
  indexBuffer: BufferTypes;
6
- indexMap: Map<string, number>;
7
- structArrayLength: number;
6
+ indexMap: Record<string, number>;
7
+ propertyDefaults: Record<string, any>;
8
+ structArrayIndexes: number;
8
9
  structSize: number;
9
10
  };
@@ -3,32 +3,68 @@ export type BinaryPropertyValueTypes = "number" | "boolean";
3
3
  export type BinaryBooleanProperty = {
4
4
  id: string;
5
5
  type: "boolean";
6
+ default?: boolean;
6
7
  };
7
8
  export type BinaryNumberProperty = {
8
9
  id: string;
9
10
  type: "number";
10
11
  range: [min: number, max: number];
12
+ default?: number;
11
13
  };
12
14
  export type BinaryTypedNumberProperty = {
13
15
  id: string;
14
16
  type: "typed-number";
15
17
  numberType: BinaryNumberTypes;
18
+ default?: number;
19
+ };
20
+ export type BinaryTypedVector2Property = {
21
+ id: string;
22
+ type: "vector-2";
23
+ numberType: BinaryNumberTypes;
24
+ default?: {
25
+ x: number;
26
+ y: number;
27
+ };
28
+ };
29
+ export type BinaryTypedVector3Property = {
30
+ id: string;
31
+ type: "vector-3";
32
+ numberType: BinaryNumberTypes;
33
+ default?: {
34
+ x: number;
35
+ y: number;
36
+ z: number;
37
+ };
38
+ };
39
+ export type BinaryTypedVector4Property = {
40
+ id: string;
41
+ type: "vector-4";
42
+ numberType: BinaryNumberTypes;
43
+ default?: {
44
+ x: number;
45
+ y: number;
46
+ z: number;
47
+ w: number;
48
+ };
16
49
  };
17
50
  export type BinaryTypedNumberArrayProperty = {
18
51
  id: string;
19
52
  type: "typed-number-array";
20
53
  numberType: BinaryNumberTypes;
21
54
  length: number;
55
+ default?: number[];
22
56
  };
23
57
  export type BinaryBitArrayProperty = {
24
58
  id: string;
25
59
  type: "bit-array";
26
60
  length: number;
61
+ default?: boolean[];
27
62
  };
28
63
  export type BinaryHeaderProperty = {
29
64
  id: string;
30
65
  type: "header";
31
66
  numberType: BinaryNumberTypes;
67
+ default?: number;
32
68
  };
33
- export type BinaryPropertyNodes = BinaryBooleanProperty | BinaryNumberProperty | BinaryBitArrayProperty | BinaryTypedNumberProperty | BinaryTypedNumberArrayProperty | BinaryHeaderProperty;
69
+ export type BinaryPropertyNodes = BinaryBooleanProperty | BinaryNumberProperty | BinaryBitArrayProperty | BinaryTypedNumberProperty | BinaryTypedNumberArrayProperty | BinaryHeaderProperty | BinaryTypedVector2Property | BinaryTypedVector3Property | BinaryTypedVector4Property;
34
70
  export type BinaryPropertySchema = Map<string, BinaryPropertyNodes>;
@@ -1,2 +1,2 @@
1
1
  export * from "./BinaryStructSchema.types";
2
- export * from "./RemoveBinaryStructData.types";
2
+ export * from "./BinaryStructData.types";
@@ -1,2 +1,2 @@
1
1
  export * from "./BinaryStructSchema.types";
2
- export * from "./RemoveBinaryStructData.types";
2
+ export * from "./BinaryStructData.types";
@@ -0,0 +1,8 @@
1
+ export declare class BinaryArrays {
2
+ static getBitArrayIndex(data: DataView, byteOffset: number, arrayIndex: number): number;
3
+ static setBitArrayIndex(data: DataView, byteOffset: number, arrayIndex: number, value: number): void;
4
+ static getNibbleArrayIndex(data: DataView, byteOffset: number, arrayIndex: number): number;
5
+ static setNibbleArrayIndex(data: DataView, byteOffset: number, arrayIndex: number, value: number): void;
6
+ static getHalfNibbleArrayIndex(data: DataView, byteOffset: number, arrayIndex: number): number;
7
+ static setHalfNibbleArrayIndex(data: DataView, byteOffset: number, arrayIndex: number, value: number): void;
8
+ }
@@ -0,0 +1,39 @@
1
+ import { BinaryUtil } from "./BinaryUtil";
2
+ export class BinaryArrays {
3
+ static getBitArrayIndex(data, byteOffset, arrayIndex) {
4
+ const arrayByteIndex = (arrayIndex / 8) >> 0;
5
+ const arrayBitIndex = arrayIndex - arrayByteIndex * 8;
6
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
7
+ return BinaryUtil.getBitValue(arrayByte, arrayBitIndex, 1);
8
+ }
9
+ static setBitArrayIndex(data, byteOffset, arrayIndex, value) {
10
+ const arrayByteIndex = (arrayIndex / 8) >> 0;
11
+ const arrayBitIndex = arrayIndex - arrayByteIndex * 8;
12
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
13
+ data.setUint8(arrayByteIndex + byteOffset, BinaryUtil.setBitValue(arrayByte, arrayBitIndex, value, 1));
14
+ }
15
+ static getNibbleArrayIndex(data, byteOffset, arrayIndex) {
16
+ const arrayByteIndex = (arrayIndex / 2) >> 0;
17
+ const isHighNibble = arrayIndex % 2 === 0;
18
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
19
+ return BinaryUtil.getBitValue(arrayByte, isHighNibble ? 4 : 0, 4);
20
+ }
21
+ static setNibbleArrayIndex(data, byteOffset, arrayIndex, value) {
22
+ const arrayByteIndex = (arrayIndex / 2) >> 0;
23
+ const isHighNibble = arrayIndex % 2 === 0;
24
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
25
+ data.setUint8(arrayByteIndex + byteOffset, BinaryUtil.setBitValue(arrayByte, isHighNibble ? 4 : 0, value, 4));
26
+ }
27
+ static getHalfNibbleArrayIndex(data, byteOffset, arrayIndex) {
28
+ const arrayByteIndex = (arrayIndex / 4) >> 0;
29
+ const nibbleIndex = arrayIndex % 4;
30
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
31
+ return BinaryUtil.getBitValue(arrayByte, nibbleIndex * 2, 2);
32
+ }
33
+ static setHalfNibbleArrayIndex(data, byteOffset, arrayIndex, value) {
34
+ const arrayByteIndex = (arrayIndex / 4) >> 0;
35
+ const nibbleIndex = arrayIndex % 4;
36
+ const arrayByte = data.getUint8(arrayByteIndex + byteOffset);
37
+ data.setUint8(arrayByteIndex + byteOffset, BinaryUtil.setBitValue(arrayByte, nibbleIndex * 2, value, 2));
38
+ }
39
+ }
@@ -4,8 +4,6 @@ export declare class BinaryUtil {
4
4
  static getTypedNumber(data: DataView, index: number, numberType: BinaryNumberTypes): number;
5
5
  static calculateBitsNeeded(min: number, max: number): number;
6
6
  static getTypedSize(type: BinaryNumberTypes): import("../Constants/BinaryTypes").ByteCounts;
7
- static getBitValue(data: number, index: number, bitSize: number): number;
8
- static setBitValue(data: number, index: number, value: number, bitSize: number): number;
9
- static getBitArrayIndex(data: DataView, byteIndex: number, arrayIndex: number): number;
10
- static setBitArrayIndex(data: DataView, byteIndex: number, arrayIndex: number, value: number): void;
7
+ static getBitValue(data: number, bitIndex: number, bitSize: number): number;
8
+ static setBitValue(data: number, bitIndex: number, value: number, bitSize: number): number;
11
9
  }
@@ -15,26 +15,12 @@ export class BinaryUtil {
15
15
  static getTypedSize(type) {
16
16
  return MappedByteCounts[type];
17
17
  }
18
- static getBitValue(data, index, bitSize) {
19
- index *= bitSize;
18
+ static getBitValue(data, bitIndex, bitSize) {
20
19
  const mask = 2 ** bitSize - 1;
21
- return ((mask << index) & data) >>> index;
20
+ return ((mask << bitIndex) & data) >>> bitIndex;
22
21
  }
23
- static setBitValue(data, index, value, bitSize) {
24
- index *= bitSize;
22
+ static setBitValue(data, bitIndex, value, bitSize) {
25
23
  const mask = 2 ** bitSize - 1;
26
- return (data & ~(mask << index)) | ((value & mask) << index);
27
- }
28
- static getBitArrayIndex(data, byteIndex, arrayIndex) {
29
- const arrayByteIndex = (arrayIndex / 8) >> 0;
30
- const arrayBitIndex = arrayIndex - arrayByteIndex * 8;
31
- const arrayByte = data.getUint8(arrayByteIndex + byteIndex);
32
- return this.getBitValue(arrayByte, arrayBitIndex, 1);
33
- }
34
- static setBitArrayIndex(data, byteIndex, arrayIndex, value) {
35
- const arrayByteIndex = (arrayIndex / 8) >> 0;
36
- const arrayBitIndex = arrayIndex - arrayByteIndex * 8;
37
- const arrayByte = data.getUint8(arrayByteIndex + byteIndex);
38
- data.setUint8(arrayByteIndex + byteIndex, this.setBitValue(arrayByte, arrayBitIndex, value, 1));
24
+ return (data & ~(mask << bitIndex)) | ((value & mask) << bitIndex);
39
25
  }
40
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amodx/binary",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "module": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",