Skip to content

Commit 8da4ef3

Browse files
committed
feat: write data to the specified byte in the buffer.
1 parent 6e99863 commit 8da4ef3

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/dynamicBuffer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ export class DynamicBuffer {
203203

204204
return Reflect.get(target, p, receiver);
205205
},
206+
set: (target: this, p: string | symbol, newValue: any, receiver: any) => {
207+
if (Reflect.has(target, p)) {
208+
return Reflect.set(target, p, newValue, receiver);
209+
}
210+
if (Number(p) >= 0) {
211+
Reflect.apply(Reflect.get(target, 'writeByte', receiver), target, [newValue, Number(p)]);
212+
return true;
213+
}
214+
215+
return Reflect.set(target, p, newValue, receiver);
216+
},
206217
});
207218
}
208219

@@ -1048,6 +1059,20 @@ export class DynamicBuffer {
10481059
this.size = newSize;
10491060
}
10501061

1062+
/**
1063+
* Writes data to the specified position in the buffer, and skip if out of used range.
1064+
*
1065+
* @param data Data to write to buffer.
1066+
* @param offset The position to write data.
1067+
*/
1068+
private writeByte(data: any, offset: number) {
1069+
if (!this.buffer || this.length === 0 || offset >= this.length) {
1070+
return;
1071+
}
1072+
1073+
this.buffer[offset] = data;
1074+
}
1075+
10511076
/**
10521077
* Write data into internal buffer with the specified offset.
10531078
*

test/read.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it } from 'mocha';
44
import { DynamicBuffer } from '../src';
55

66
describe('Get by index tests', () => {
7-
it('Test getting by index with legal ranges', () => {
7+
it('Test getting by index with valid position', () => {
88
const buffer = new DynamicBuffer();
99
const str = 'Hello world';
1010

test/write.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import { describe, it } from 'mocha';
44

55
import { DynamicBuffer } from '../src';
66

7+
describe('Set by index tests', () => {
8+
it('Test set by index with valid position', () => {
9+
const buffer = new DynamicBuffer('hello world');
10+
11+
buffer[0] = 72;
12+
assert.equal(buffer.toString(), 'Hello world');
13+
});
14+
15+
it('Test getting by index with a negative offset', () => {
16+
const buffer = new DynamicBuffer('Hello world');
17+
18+
buffer[-1] = 33; // !
19+
20+
assert.equal(buffer.toString(), 'Hello world');
21+
});
22+
23+
it('Test getting by index with a offset larger than buffer length', () => {
24+
const buffer = new DynamicBuffer('Hello world');
25+
26+
buffer[buffer.length] = 33; // !
27+
28+
assert.equal(buffer.toString(), 'Hello world');
29+
});
30+
});
31+
732
describe('Append tests', () => {
833
it('Test appending string', () => {
934
const buffer = new DynamicBuffer();

0 commit comments

Comments
 (0)