Skip to content

Commit 1c0d5ca

Browse files
committed
Upgraded dependencies, added initial tests, updated how arrays are build.
1 parent 2ca67f1 commit 1c0d5ca

File tree

10 files changed

+298
-83
lines changed

10 files changed

+298
-83
lines changed

.eslintignore

Lines changed: 0 additions & 10 deletions
This file was deleted.

.eslintrc.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"es2021": true,
5-
"node": true
6-
},
2+
"env": { "browser": true, "es2021": true, "node": true },
73
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
84
"parser": "@typescript-eslint/parser",
9-
"parserOptions": {
10-
"ecmaFeatures": { "jsx": true },
11-
"ecmaVersion": "latest",
12-
"sourceType": "module"
13-
},
5+
"parserOptions": { "ecmaFeatures": { "jsx": true }, "ecmaVersion": "latest", "sourceType": "module" },
146
"plugins": ["@typescript-eslint"],
157
"rules": {
168
"@typescript-eslint/explicit-module-boundary-types": "off",

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v18.13.0

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsserver.experimental.enableProjectDiagnostics": true
3+
}

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
moduleNameMapper: {
6+
"^_/(.*)$": "<rootDir>/src/$1",
7+
},
8+
};

package.json

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typed-buffer-struct",
3-
"version": "0.0.8",
3+
"version": "0.1.0",
44
"description": "This is a package which helps the user to create a Struct with automatic Buffer creation.",
55
"author": {
66
"name": "Nelson Faiçal Rechdan",
@@ -13,18 +13,22 @@
1313
"main": "dist/index.js",
1414
"license": "MIT",
1515
"scripts": {
16-
"upgrade": "clear && ncu -t greatest -f \"/eslint/\" -u && ncu -u && yarn --check-files",
17-
"build": "ttsc && rm -rf \"./dist\" && mv \"./temp\" \"./dist\"",
18-
"pub:alpha": "clear && yarn run build && npm publish --tag alpha --access public"
16+
"upgrade": "ncu -u && yarn --check-files",
17+
"test": "jest",
18+
"test:watch": "jest --watch",
19+
"build": "ttsc && rm -rf \"./dist\" && mv \"./temp\" \"./dist\""
1920
},
2021
"devDependencies": {
21-
"@types/node": "16.11.9",
22-
"@typescript-eslint/eslint-plugin": "5.4.1-alpha.18",
23-
"@typescript-eslint/parser": "5.4.1-alpha.18",
24-
"eslint": "8.3.0",
25-
"ttypescript": "1.5.12",
26-
"typescript": "4.5.2",
27-
"typescript-transform-paths": "3.3.1"
22+
"@types/jest": "29.2.5",
23+
"@types/node": "18.11.18",
24+
"@typescript-eslint/eslint-plugin": "5.48.2",
25+
"@typescript-eslint/parser": "5.48.2",
26+
"eslint": "8.32.0",
27+
"jest": "29.3.1",
28+
"ts-jest": "29.0.5",
29+
"ttypescript": "1.5.15",
30+
"typescript": "4.9.4",
31+
"typescript-transform-paths": "3.4.6"
2832
},
2933
"files": [
3034
"package.json",

src/struct/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const structString: StructString = (builder, size, name, strLength) => {
5151

5252
const structStruct: StructStruct = (builder, size, name, struct) => {
5353
builder.push((obj, buffer) => {
54-
const structObj = struct(buffer.slice(size, size + struct.size));
54+
const structObj = struct(buffer.subarray(size, size + struct.size));
5555
delete (structObj as { buffer: unknown }).buffer;
5656
Object.defineProperty(obj, name, {
5757
get: () => structObj,
@@ -97,15 +97,15 @@ const arrayBigNumberFunction: BuildArrayResponseBigNumberFunction = (arrLength,
9797
};
9898
};
9999

100-
const buildArray: BuildArray = () => ({
100+
const buildArray: BuildArray = (arrLength) => ({
101101
// arrays
102-
array: (arrLength, build) => {
103-
const { size: buildSize, builder: buildBuilder } = build(buildArray());
102+
array: (arrLength2, build) => {
103+
const { size: buildSize, builder: buildBuilder } = build(buildArray(arrLength2));
104104
return {
105105
size: buildSize * arrLength,
106106
builder: (arr, buffer) => {
107107
for (let i = 0; i < arrLength; i++) {
108-
const arr2 = buildBuilder([], buffer.slice(buildSize * i, buildSize * (i + 1)));
108+
const arr2 = buildBuilder([], buffer.subarray(buildSize * i, buildSize * (i + 1)));
109109
Object.defineProperty(arr, i, {
110110
get: () => arr2,
111111
enumerable: true,
@@ -116,17 +116,17 @@ const buildArray: BuildArray = () => ({
116116
};
117117
},
118118
// numbers
119-
sbyte: (arrLength) => arrayNumberFunction(arrLength, "sbyte"),
120-
byte: (arrLength) => arrayNumberFunction(arrLength, "byte"),
121-
short: (arrLength) => arrayNumberFunction(arrLength, "short"),
122-
ushort: (arrLength) => arrayNumberFunction(arrLength, "ushort"),
123-
int: (arrLength) => arrayNumberFunction(arrLength, "int"),
124-
uint: (arrLength) => arrayNumberFunction(arrLength, "uint"),
119+
sbyte: () => arrayNumberFunction(arrLength, "sbyte"),
120+
byte: () => arrayNumberFunction(arrLength, "byte"),
121+
short: () => arrayNumberFunction(arrLength, "short"),
122+
ushort: () => arrayNumberFunction(arrLength, "ushort"),
123+
int: () => arrayNumberFunction(arrLength, "int"),
124+
uint: () => arrayNumberFunction(arrLength, "uint"),
125125
// big numbers
126-
long: (arrLength) => arrayBigNumberFunction(arrLength, "long"),
127-
ulong: (arrLength) => arrayBigNumberFunction(arrLength, "ulong"),
126+
long: () => arrayBigNumberFunction(arrLength, "long"),
127+
ulong: () => arrayBigNumberFunction(arrLength, "ulong"),
128128
// extras
129-
string: (arrLength, stringLength) => ({
129+
string: (stringLength) => ({
130130
size: arrLength * stringLength,
131131
builder: (arr, buffer) => {
132132
for (let i = 0; i < arrLength; i++) {
@@ -139,11 +139,11 @@ const buildArray: BuildArray = () => ({
139139
return arr;
140140
},
141141
}),
142-
struct: (arrLength, struct) => ({
142+
struct: (struct) => ({
143143
size: arrLength * struct.size,
144144
builder: (arr, buffer) => {
145145
for (let i = 0; i < arrLength; i++) {
146-
const structObj = struct(buffer.slice(i * struct.size, (i + 1) * struct.size));
146+
const structObj = struct(buffer.subarray(i * struct.size, (i + 1) * struct.size));
147147
delete (structObj as { buffer: unknown }).buffer;
148148
Object.defineProperty(arr, i, {
149149
get: () => structObj,
@@ -155,10 +155,10 @@ const buildArray: BuildArray = () => ({
155155
}),
156156
});
157157

158-
const structArray: StructArray = (builder, size, name, build) => {
159-
const { size: buildSize, builder: buildBuilder } = build(buildArray());
158+
const structArray: StructArray = (builder, size, name, arrLength, build) => {
159+
const { size: buildSize, builder: buildBuilder } = build(buildArray(arrLength));
160160
builder.push((obj, buffer) => {
161-
const arr = buildBuilder([], buffer.slice(size, buildSize + size));
161+
const arr = buildBuilder([], buffer.subarray(size, buildSize + size));
162162
Object.defineProperty(obj, name, {
163163
get: () => arr,
164164
enumerable: true,
@@ -183,7 +183,7 @@ const createStruct: CreateStruct<{}> = (builder = [], size = 0) => ({
183183
// extras
184184
string: (name, length) => structString(builder, size, name, length),
185185
struct: (name, struct) => structStruct(builder, size, name, struct),
186-
array: (name, build) => structArray(builder, size, name, build),
186+
array: (name, arrLength, build) => structArray(builder, size, name, arrLength, build),
187187
offset: (length) => createStruct(builder, size + length),
188188
// build
189189
build: () => {

src/struct/types.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,30 @@ export type BuildArrayResponseBigNumberFunction = (length: number, type: BigNumb
4141

4242
export type BuildArrayResponse = {
4343
// numbers
44-
sbyte: (arrLength: number) => BuildArrayResponseNumberResult;
45-
byte: (arrLength: number) => BuildArrayResponseNumberResult;
46-
short: (arrLength: number) => BuildArrayResponseNumberResult;
47-
ushort: (arrLength: number) => BuildArrayResponseNumberResult;
48-
int: (arrLength: number) => BuildArrayResponseNumberResult;
49-
uint: (arrLength: number) => BuildArrayResponseNumberResult;
44+
sbyte: () => BuildArrayResponseNumberResult;
45+
byte: () => BuildArrayResponseNumberResult;
46+
short: () => BuildArrayResponseNumberResult;
47+
ushort: () => BuildArrayResponseNumberResult;
48+
int: () => BuildArrayResponseNumberResult;
49+
uint: () => BuildArrayResponseNumberResult;
5050
// big number
51-
long: (arrLength: number) => BuildArrayResponseBigNumberResult;
52-
ulong: (arrLength: number) => BuildArrayResponseBigNumberResult;
51+
long: () => BuildArrayResponseBigNumberResult;
52+
ulong: () => BuildArrayResponseBigNumberResult;
5353
// extra
5454
array: <T>(arrLength: number, build: ArrayBuilder<T>) => ArrayBuilderResponse<T[]>;
55-
string: (arrLength: number, stringLength: number) => ArrayBuilderResponse<string[]>;
56-
struct: <T>(arrLength: number, struct: BuildStructResult<T>) => ArrayBuilderResponse<T[]>;
55+
string: (stringLength: number) => ArrayBuilderResponse<string[]>;
56+
struct: <T>(struct: BuildStructResult<T>) => ArrayBuilderResponse<T[]>;
5757
};
5858

59-
export type BuildArray = () => BuildArrayResponse;
59+
export type BuildArray = (length: number) => BuildArrayResponse;
6060

61-
export type StructArray = <T, T2, N extends string>(builder: Builder[], size: number, name: N, build: ArrayBuilder<T2>) => CreateStructResultOf<T, T2, N>;
61+
export type StructArray = <T, T2, N extends string>(
62+
builder: Builder[],
63+
size: number,
64+
name: N,
65+
arrLength: number,
66+
build: ArrayBuilder<T2>
67+
) => CreateStructResultOf<T, T2, N>;
6268

6369
// STRUCT
6470

@@ -83,7 +89,7 @@ export type CreateStructResult<T> = {
8389
// extras
8490
string: <N extends string>(name: N, length: number) => CreateStructResultOf<T, string, N>;
8591
struct: <T2, N extends string>(name: N, struct: BuildStructResult<T2>) => CreateStructResultOf<T, T2, N>;
86-
array: <T2, N extends string>(name: N, build: ArrayBuilder<T2[]>) => CreateStructResultOf<T, T2[], N>;
92+
array: <T2, N extends string>(name: N, arrLength: number, build: ArrayBuilder<T2[]>) => CreateStructResultOf<T, T2[], N>;
8793
offset: (length: number) => CreateStructResult<T>;
8894
// build
8995
build: () => BuildStructResult<{ [K in keyof T]: T[K] }>;

src/tests/index.test.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import newStruct from "_/struct";
2+
3+
const basicStruct = newStruct()
4+
/* 0 to 15 = 16 */ .string("name", 16)
5+
/* 16 = 1 */ .sbyte("sbyte")
6+
/* 17 = 1 */ .byte("byte")
7+
/* 18 to 19 = 2 */ .short("short")
8+
/* 20 to 21 = 2 */ .ushort("ushort")
9+
/* 22 to 25 = 4 */ .int("int")
10+
/* 26 to 29 = 4 */ .uint("uint")
11+
/* 30 to 37 = 8 */ .long("long")
12+
/* 38 to 45 = 8 */ .ulong("ulong")
13+
.build();
14+
15+
describe("basic struct", () => {
16+
test("buffer size", () => {
17+
const structToTest = basicStruct();
18+
expect(basicStruct.size).toBe(46);
19+
expect(structToTest.buffer.byteLength).toBe(46);
20+
});
21+
22+
test("is buffer empty", () => {
23+
const structToTest = basicStruct();
24+
expect(structToTest.buffer.every((v) => v === 0)).toBe(true);
25+
});
26+
27+
test("buffer updated after name set", () => {
28+
const structToTest = basicStruct();
29+
structToTest.name = "ABCDEF";
30+
expect(structToTest.buffer[0]).toBe(0x41);
31+
expect(structToTest.buffer[1]).toBe(0x42);
32+
expect(structToTest.buffer[2]).toBe(0x43);
33+
expect(structToTest.buffer[3]).toBe(0x44);
34+
expect(structToTest.buffer[4]).toBe(0x45);
35+
expect(structToTest.buffer[5]).toBe(0x46);
36+
expect(structToTest.sbyte).toBe(0);
37+
expect(structToTest.byte).toBe(0);
38+
expect(structToTest.short).toBe(0);
39+
expect(structToTest.ushort).toBe(0);
40+
expect(structToTest.int).toBe(0);
41+
expect(structToTest.uint).toBe(0);
42+
expect(structToTest.long).toBe(0n);
43+
expect(structToTest.ulong).toBe(0n);
44+
});
45+
46+
test("buffer updated after values set", () => {
47+
const structToTest = basicStruct();
48+
structToTest.sbyte = 1;
49+
structToTest.byte = 2;
50+
structToTest.short = 3;
51+
structToTest.ushort = 4;
52+
structToTest.int = 5;
53+
structToTest.uint = 6;
54+
structToTest.long = 7n;
55+
structToTest.ulong = 8n;
56+
expect(structToTest.sbyte).toBe(1);
57+
expect(structToTest.byte).toBe(2);
58+
expect(structToTest.short).toBe(3);
59+
expect(structToTest.ushort).toBe(4);
60+
expect(structToTest.int).toBe(5);
61+
expect(structToTest.uint).toBe(6);
62+
expect(structToTest.long).toBe(7n);
63+
expect(structToTest.ulong).toBe(8n);
64+
expect(structToTest.buffer.readInt8(16)).toBe(1);
65+
expect(structToTest.buffer.readUInt8(17)).toBe(2);
66+
expect(structToTest.buffer.readInt16LE(18)).toBe(3);
67+
expect(structToTest.buffer.readUInt16LE(20)).toBe(4);
68+
expect(structToTest.buffer.readInt32LE(22)).toBe(5);
69+
expect(structToTest.buffer.readUInt32LE(26)).toBe(6);
70+
expect(structToTest.buffer.readBigInt64LE(30)).toBe(7n);
71+
expect(structToTest.buffer.readBigUInt64LE(38)).toBe(8n);
72+
});
73+
});
74+
75+
const structWithArray = newStruct()
76+
/* 0 to 7 = 8 */ .array("arr", 4, (b) => b.ushort())
77+
.build();
78+
79+
describe("struct with array", () => {
80+
test("buffer size", () => {
81+
const structToTest = structWithArray();
82+
expect(structWithArray.size).toBe(8);
83+
expect(structToTest.buffer.byteLength).toBe(8);
84+
});
85+
86+
test("is buffer empty", () => {
87+
const structToTest = structWithArray();
88+
expect(structToTest.buffer.every((v) => v === 0)).toBe(true);
89+
});
90+
91+
test("check array length", () => {
92+
const structToTest = structWithArray();
93+
expect(structToTest.arr.length).toBe(4);
94+
});
95+
96+
test("check array values set", () => {
97+
const structToTest = structWithArray();
98+
structToTest.arr[0] = 1;
99+
structToTest.arr[1] = 2;
100+
structToTest.arr[2] = 3;
101+
structToTest.arr[3] = 4;
102+
expect(structToTest.arr[0]).toBe(1);
103+
expect(structToTest.arr[1]).toBe(2);
104+
expect(structToTest.arr[2]).toBe(3);
105+
expect(structToTest.arr[3]).toBe(4);
106+
expect(structToTest.buffer.readUInt16LE(0)).toBe(1);
107+
expect(structToTest.buffer.readUInt16LE(2)).toBe(2);
108+
expect(structToTest.buffer.readUInt16LE(4)).toBe(3);
109+
expect(structToTest.buffer.readUInt16LE(6)).toBe(4);
110+
});
111+
});
112+
113+
const structWith2DArray = newStruct()
114+
/* 0 to 159 = 160 */ .array("arr", 4, (b) => b.array(10, (b) => b.uint()))
115+
.build();
116+
117+
describe("struct with 2D array", () => {
118+
test("buffer size", () => {
119+
const structToTest = structWith2DArray();
120+
expect(structWith2DArray.size).toBe(160);
121+
expect(structToTest.buffer.byteLength).toBe(160);
122+
});
123+
124+
test("is buffer empty", () => {
125+
const structToTest = structWith2DArray();
126+
expect(structToTest.buffer.every((v) => v === 0)).toBe(true);
127+
});
128+
129+
test("check array length", () => {
130+
const structToTest = structWith2DArray();
131+
expect(structToTest.arr.length).toBe(4);
132+
for (const subArr of structToTest.arr) {
133+
expect(subArr.length).toBe(10);
134+
}
135+
});
136+
});

0 commit comments

Comments
 (0)