Skip to content

Commit dee0b21

Browse files
committed
feat: add write method.
- Add write method to writing data to the specified position. - Add writeToBuffer private method to handle append and write. - Add test cases for write method.
1 parent 897f04a commit dee0b21

File tree

2 files changed

+180
-14
lines changed

2 files changed

+180
-14
lines changed

src/dynamicBuffer.ts

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ export class DynamicBuffer {
9797
/**
9898
* Appends string to this buffer according to the character encoding.
9999
*
100+
* ```js
101+
* buf.append('Hello ');
102+
* buf.append('world!');
103+
* console.log(buf.toString());
104+
* // Hello world!
105+
* ```
106+
*
100107
* @param data String to write to buffer.
101108
* @param length Maximum number of bytes to write, default the length of string.
102109
* @param encoding The character encoding to use, default from buffer encoding.
@@ -120,10 +127,6 @@ export class DynamicBuffer {
120127
lengthParam?: number | BufferEncoding,
121128
encodingParam?: BufferEncoding | number,
122129
) {
123-
if (typeof data !== 'string') {
124-
throw new TypeError('argument must be a string');
125-
}
126-
127130
let length: number | undefined;
128131
let encoding: BufferEncoding | undefined;
129132

@@ -137,19 +140,38 @@ export class DynamicBuffer {
137140
length = encodingParam;
138141
}
139142

140-
let lengthToWrite = data.length || 0;
141-
if (length !== undefined && length >= 0 && length <= data.length) {
142-
lengthToWrite = length;
143-
}
143+
const count = this.writeData(data, this.used, length, encoding);
144+
this.used += count;
144145

145-
if (lengthToWrite === 0) {
146-
return 0;
147-
}
146+
return count;
147+
}
148148

149-
this.ensureSize(lengthToWrite + this.used);
149+
/**
150+
* Writes data to this buffer at offset according to the specific character encoding.
151+
*
152+
* ```js
153+
* buf.write('Hello!');
154+
* console.log(buf.toString());
155+
* // Hello!
156+
* buf.write(' world!', 5);
157+
* console.log(buf.toString());
158+
* // Hello world!
159+
* ```
160+
*
161+
* @param data String to write to buffer.
162+
* @param offset Number of bytes to skip before starting to write data, default 0.
163+
* @param length Maximum number of bytes to write, default the length of string.
164+
* @param encoding The character encoding to use, default from buffer encoding.
165+
* @returns Number of bytes written.
166+
*/
167+
write(data: string, offset?: number, length?: number, encoding?: BufferEncoding) {
168+
const start = offset || 0;
169+
if (start < 0) {
170+
throw new RangeError('The value of "offset" is out of range.');
171+
}
150172

151-
const count = this.buffer?.write(data, this.used, lengthToWrite, encoding || this.encoding);
152-
this.used += count || 0;
173+
const count = this.writeData(data, start, length, encoding);
174+
this.used = start + count;
153175

154176
return count;
155177
}
@@ -345,6 +367,36 @@ export class DynamicBuffer {
345367
}(this);
346368
}
347369

370+
/**
371+
* Write data into internal buffer with the specified offset.
372+
*
373+
* @param data String to write to buffer.
374+
* @param offset Number of bytes to skip before starting to write data.
375+
* @param length Maximum number of bytes to write, default the length of string.
376+
* @param encoding The character encoding to use, default from buffer encoding.
377+
* @returns Number of bytes written.
378+
*/
379+
private writeData(data: string, offset: number, length?: number, encoding?: BufferEncoding) {
380+
if (typeof data !== 'string') {
381+
throw new TypeError('argument must be a string');
382+
}
383+
384+
let lengthToWrite = data.length || 0;
385+
if (length !== undefined && length >= 0 && length <= data.length) {
386+
lengthToWrite = length;
387+
}
388+
389+
if (lengthToWrite === 0) {
390+
return 0;
391+
}
392+
393+
this.ensureSize(lengthToWrite + offset);
394+
395+
const count = this.buffer?.write(data, offset, lengthToWrite, encoding || this.encoding);
396+
397+
return count || 0;
398+
}
399+
348400
/**
349401
* Ensures the buffer size is at least equal to the expect size.
350402
*

test/dynamicBuffer.spec.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,120 @@ describe('Append tests', () => {
134134
});
135135
});
136136

137+
describe('Write tests', () => {
138+
it('Test writing string without offset', () => {
139+
const buffer = new DynamicBuffer();
140+
const str = 'Hello world';
141+
142+
const count = buffer.write(str);
143+
144+
assert.equal(count, str.length);
145+
assert.equal(buffer.toString(), str);
146+
});
147+
148+
it('Test writing string with offset', () => {
149+
const buffer = new DynamicBuffer({ fill: ' ' });
150+
const str = 'Hello world';
151+
152+
const count = buffer.write(str, 5);
153+
154+
assert.equal(count, str.length);
155+
assert.equal(buffer.toString(), ` ${str}`);
156+
});
157+
158+
it('Test writing string with negative offset', () => {
159+
const buffer = new DynamicBuffer();
160+
const str = 'Hello world';
161+
162+
assert.throws(() => {
163+
buffer.write(str, -1);
164+
});
165+
});
166+
167+
it('Test writing string with large offset', () => {
168+
const buffer = new DynamicBuffer({ fill: ' ' });
169+
const str = 'Hello world';
170+
171+
assert.throws(() => {
172+
buffer.write(str, constants.MAX_LENGTH * 2);
173+
});
174+
});
175+
176+
it('Test writing string without offset twice time', () => {
177+
const buffer = new DynamicBuffer();
178+
let str = 'Hello';
179+
let count = buffer.write(str);
180+
assert.equal(count, str.length);
181+
assert.equal(buffer.toString(), str);
182+
183+
str = 'world';
184+
count = buffer.write(str);
185+
assert.equal(count, str.length);
186+
assert.equal(buffer.toString(), str);
187+
assert.equal(buffer.length, str.length);
188+
});
189+
190+
it('Test writing string with offset twice time', () => {
191+
const buffer = new DynamicBuffer();
192+
let str = 'Hello!';
193+
let count = buffer.write(str);
194+
assert.equal(count, str.length);
195+
assert.equal(buffer.toString(), str);
196+
197+
str = ' world!';
198+
count = buffer.write(str, 5);
199+
assert.equal(count, str.length);
200+
assert.equal(buffer.toString(), 'Hello world!');
201+
assert.equal(buffer.length, 12);
202+
});
203+
204+
it('Test writing empty string without offset', () => {
205+
const buffer = new DynamicBuffer();
206+
207+
const count = buffer.write('');
208+
209+
assert.equal(count, 0);
210+
assert.equal(buffer.toString(), '');
211+
});
212+
213+
it('Test writing string with parameters', () => {
214+
const buffer = new DynamicBuffer();
215+
const str = 'Hello world!!!';
216+
217+
const count = buffer.write(str, 0, 12, 'utf-8');
218+
219+
assert.equal(count, 12);
220+
assert.equal(buffer.toString(), 'Hello world!');
221+
});
222+
223+
it('Test writing without parameter', () => {
224+
const buffer = new DynamicBuffer();
225+
226+
assert.throws(() => {
227+
// @ts-ignore
228+
buffer.write();
229+
});
230+
});
231+
232+
it('Test writing null', () => {
233+
const buffer = new DynamicBuffer();
234+
235+
assert.throws(() => {
236+
// @ts-ignore
237+
buffer.write(null);
238+
});
239+
});
240+
241+
it('Test writing a number', () => {
242+
const buffer = new DynamicBuffer();
243+
244+
assert.throws(() => {
245+
// @ts-ignore
246+
buffer.write(65);
247+
});
248+
});
249+
});
250+
137251
describe('Export buffer tests', () => {
138252
it('Test toBuffer() before appending', () => {
139253
const buffer = new DynamicBuffer();

0 commit comments

Comments
 (0)