Skip to content

Commit 3883b54

Browse files
committed
feat: add at method.
1 parent 72a3931 commit 3883b54

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/dynamicBuffer.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ export class DynamicBuffer {
271271
return count;
272272
}
273273

274+
/**
275+
* Takes an integer value and returns the item at that index, allowing for positive and negative
276+
* integers. Negative integers count back from the last item in the array. Takes an integer value
277+
* and returns the item at the index.
278+
*
279+
* ```js
280+
* buf.append('Hello world');
281+
* console.log(buf.at(0));
282+
* // 72
283+
*
284+
* @param index Zero-based index of the byte in the buffer to be returned.
285+
* @returns The byte at the position in the buffer.
286+
*/
287+
at(index: number): number | undefined {
288+
if (!this.buffer || index >= this.used) {
289+
return undefined;
290+
}
291+
292+
if (index >= 0) {
293+
return this.buffer.at(index);
294+
}
295+
return this.buffer.at(index - (this.size - this.length));
296+
}
297+
274298
/**
275299
* Reads and returns a byte at the position `offset` in this buffer.
276300
*
@@ -281,7 +305,7 @@ export class DynamicBuffer {
281305
* ```
282306
*
283307
* @param offset Number of bytes to skip before starting to read, and the offset must satisfy
284-
* between 0 and `this.length - `, default `0`.
308+
* between 0 and `this.length`, default `0`.
285309
* @returns The byte at the position in the buffer.
286310
*/
287311
read(offset: number = 0): number {

test/read.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,47 @@ describe('Read tests', () => {
4444
});
4545
});
4646
});
47+
48+
describe('At tests', () => {
49+
it('Test at with positive index within buffer\'s length', () => {
50+
const str = 'Hello world';
51+
const buffer = new DynamicBuffer(str);
52+
53+
for (let i = 0; i < str.length; i += 1) {
54+
assert.equal(buffer.at(i), str.charCodeAt(i));
55+
}
56+
});
57+
58+
it('Test at with positive index greater than buffer\'s length', () => {
59+
const str = 'Hello world';
60+
const buffer = new DynamicBuffer(str);
61+
62+
assert.equal(buffer.at(str.length), undefined);
63+
});
64+
65+
it('Test at with a negative index', () => {
66+
const str = 'Hello world';
67+
const buffer = new DynamicBuffer(str);
68+
69+
for (let i = 0; i < str.length; i += 1) {
70+
assert.equal(buffer.at(0 - str.length + i), str.charCodeAt(i));
71+
}
72+
});
73+
74+
it('Test at with negative index more than buffer\'s length', () => {
75+
const str = 'Hello world';
76+
const buffer = new DynamicBuffer(str);
77+
78+
assert.equal(buffer.at(-str.length - 1), undefined);
79+
});
80+
81+
it('Test at with specified buffer size and negative index', () => {
82+
const str = 'Hello world';
83+
const buffer = new DynamicBuffer(str, { size: str.length });
84+
85+
for (let i = 0; i < str.length; i += 1) {
86+
assert.equal(buffer.at(0 - str.length + i), str.charCodeAt(i));
87+
}
88+
assert.equal(buffer.at(-str.length - 1), undefined);
89+
});
90+
});

0 commit comments

Comments
 (0)