Skip to content

Commit a407751

Browse files
committed
feat: add fill method.
- Rename fill field to fillVal. - Add fill method. - Add test cases for fill method.
1 parent efa2762 commit a407751

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/dynamicBuffer.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class DynamicBuffer {
7575
/**
7676
* A value to pre-fill the new buffer with.
7777
*/
78-
private fill?: string | Buffer | number;
78+
private fillVal?: string | Buffer | number;
7979

8080
/**
8181
* The current size of the buffer.
@@ -175,7 +175,7 @@ export class DynamicBuffer {
175175
}
176176

177177
this.used = 0;
178-
this.fill = options?.fill || 0;
178+
this.fillVal = options?.fill || 0;
179179
this.encoding = options?.encoding || 'utf8';
180180
this.factor = options?.factor || this.DefaultResizeFactor;
181181

@@ -184,7 +184,7 @@ export class DynamicBuffer {
184184
}
185185

186186
if (this.size > 0) {
187-
this.buffer = Buffer.alloc(this.size, this.fill, this.encoding);
187+
this.buffer = Buffer.alloc(this.size, this.fillVal, this.encoding);
188188
}
189189

190190
if (data) {
@@ -428,6 +428,41 @@ export class DynamicBuffer {
428428
return this.compare(otherBuffer, 0, otherBuffer.length, 0, this.length) === 0;
429429
}
430430

431+
/**
432+
* Fills this buffer with the specified value, and the entire buffer will be filled if the offset
433+
* and end are not given.
434+
*
435+
* ```js
436+
* const buf = new DynamicBuffer('hello');
437+
* buf.fill('x');
438+
* console.log(buf.toString());
439+
* // xxxxx
440+
* ```
441+
*
442+
* @param value The value with which to be fill this buffer.
443+
* @param offset Number of bytes to skip before starting to fill this buffer, default 0.
444+
* @param end Where to stop filling this buffer (not inclusive), default `buf.length`.
445+
* @param encoding The encoding for value if value is a string, default `'utf8'`.
446+
* @returns A reference to this buffer.
447+
*/
448+
fill(
449+
value: string | Buffer | Uint8Array | number,
450+
offset: number = 0,
451+
end: number = this.length,
452+
encoding: BufferEncoding = 'utf8',
453+
): DynamicBuffer {
454+
if (!this.buffer || this.length === 0 || end - offset <= 0) {
455+
return this;
456+
}
457+
458+
rangeCheck('offset', offset, 0, this.length - 1);
459+
rangeCheck('end', end, 0, this.length);
460+
461+
this.buffer.fill(value, offset, end, encoding);
462+
463+
return this;
464+
}
465+
431466
/**
432467
* Returns a boolean value to indicate whether this buffer includes a certain value among it.
433468
*
@@ -740,7 +775,7 @@ export class DynamicBuffer {
740775
return Buffer.alloc(0);
741776
}
742777

743-
const newBuffer = Buffer.alloc(endOffset - startOffset, this.fill, this.encoding);
778+
const newBuffer = Buffer.alloc(endOffset - startOffset, this.fillVal, this.encoding);
744779
this.buffer.copy(newBuffer, 0, startOffset, endOffset);
745780

746781
return newBuffer;
@@ -985,7 +1020,7 @@ export class DynamicBuffer {
9851020
* @param newSize The size of new buffer.
9861021
*/
9871022
private resize(newSize: number): void {
988-
const newBuffer = Buffer.alloc(newSize, this.fill, this.encoding);
1023+
const newBuffer = Buffer.alloc(newSize, this.fillVal, this.encoding);
9891024

9901025
if (this.buffer && this.used > 0) {
9911026
this.buffer.copy(newBuffer, 0, 0, this.used);

test/dynamicBuffer.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,45 @@ describe('Subarray test', () => {
130130
assert.equal(buf.toString(), '');
131131
});
132132
});
133+
134+
describe('Fill tests', () => {
135+
it('Test fill', () => {
136+
const buf = new DynamicBuffer('hello');
137+
138+
buf.fill('x');
139+
140+
assert.equal(buf.toString(), 'xxxxx');
141+
});
142+
143+
it('Test fill to empty buffer', () => {
144+
const buf = new DynamicBuffer();
145+
146+
buf.fill('x');
147+
148+
assert.equal(buf.toString(), '');
149+
});
150+
151+
it('Test fill with empty string', () => {
152+
const buf = new DynamicBuffer('hello');
153+
154+
buf.fill('');
155+
156+
assert.equal(buf.toString(), '\x00\x00\x00\x00\x00');
157+
});
158+
159+
it('Test fill with specified range', () => {
160+
const buf = new DynamicBuffer('hello');
161+
162+
buf.fill('x', 2, 4);
163+
164+
assert.equal(buf.toString(), 'hexxo');
165+
});
166+
167+
it('Test fill with invalid range', () => {
168+
const buf = new DynamicBuffer('hello');
169+
170+
assert.throws(() => {
171+
buf.fill('x', 2, 10);
172+
});
173+
});
174+
});

0 commit comments

Comments
 (0)