Skip to content

Commit 1cac50d

Browse files
committed
feat: add includes method.
1 parent b8c3a4e commit 1cac50d

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/dynamicBuffer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,31 @@ export class DynamicBuffer {
214214
return this.buffer[offset];
215215
}
216216

217+
/**
218+
* Returns a boolean value to indicate whether this buffer includes a certain value among it.
219+
*
220+
* ```js
221+
* buf.append('Hello world');
222+
* console.log(buf.includes('world'));
223+
* // true
224+
* console.log(buf.includes('not in buffer'));
225+
* // false
226+
* ```
227+
*
228+
* @param value The value what to search for.
229+
* @param byteOffset Where to begin searching in the buffer, and it'll be calculated from the
230+
* end of buffer if it's negative. Default `0`.
231+
* @param encoding The character encoding if the value is a string, default 'utf8'.
232+
* @returns `true` if `value` was found in this buffer, `false` otherwise.
233+
*/
234+
includes(
235+
value: string | Buffer | Uint8Array | number | DynamicBuffer,
236+
byteOffset: number = 0,
237+
encoding: BufferEncoding = 'utf8',
238+
): boolean {
239+
return this.indexOf(value, byteOffset, encoding) !== -1;
240+
}
241+
217242
/**
218243
* Gets the first index at which the given value can be found in the buffer, or `-1` if it is
219244
* not present.

test/search.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,93 @@ describe('IndexOf tests', () => {
9292
assert.equal(buf.indexOf('', 32), 0);
9393
});
9494
});
95+
96+
describe('Includes tests', () => {
97+
it('Test includes with empty buffer and empty value.', () => {
98+
const buf = new DynamicBuffer();
99+
100+
assert.equal(buf.includes('Hello'), false);
101+
assert.equal(buf.includes(''), true);
102+
assert.equal(buf.includes(0), false);
103+
assert.equal(buf.includes(Buffer.alloc(0)), true);
104+
assert.equal(buf.includes(new Uint8Array()), true);
105+
assert.equal(buf.includes(new DynamicBuffer()), true);
106+
});
107+
108+
it('Test includes with empty value.', () => {
109+
const buf = new DynamicBuffer();
110+
buf.append('Hello world');
111+
112+
assert.equal(buf.includes(''), true);
113+
assert.equal(buf.includes(0), false);
114+
assert.equal(buf.includes(Buffer.alloc(0)), true);
115+
assert.equal(buf.includes(new Uint8Array()), true);
116+
assert.equal(buf.includes(new DynamicBuffer()), true);
117+
});
118+
119+
it('Test includes with string.', () => {
120+
const buf = new DynamicBuffer();
121+
buf.append('Hello world');
122+
123+
assert.equal(buf.includes('Hello'), true);
124+
assert.equal(buf.includes('world'), true);
125+
assert.equal(buf.includes('no'), false);
126+
});
127+
128+
it('Test includes with number.', () => {
129+
const buf = new DynamicBuffer();
130+
buf.append('Hello world');
131+
132+
assert.equal(buf.includes(72), true); // H
133+
assert.equal(buf.includes(119), true); // w
134+
assert.equal(buf.includes(97), false); // a
135+
});
136+
137+
it('Test includes with Buffer.', () => {
138+
const buf = new DynamicBuffer();
139+
buf.append('Hello world');
140+
141+
assert.equal(buf.includes(Buffer.from('Hello')), true);
142+
assert.equal(buf.includes(Buffer.from('world')), true);
143+
assert.equal(buf.includes(Buffer.from('no')), false);
144+
});
145+
146+
it('Test includes with Uint8Array.', () => {
147+
const buf = new DynamicBuffer();
148+
buf.append('Hello world');
149+
150+
assert.equal(buf.includes(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f])), true);
151+
assert.equal(buf.includes(new Uint8Array([0x77, 0x6f, 0x72, 0x6c, 0x64])), true);
152+
assert.equal(buf.includes(new Uint8Array([0x6e, 0x6c])), false);
153+
});
154+
155+
it('Test includes with DynamicBuffer.', () => {
156+
const buf = new DynamicBuffer();
157+
buf.append('Hello world');
158+
159+
const buf1 = new DynamicBuffer();
160+
buf1.append('Hello');
161+
assert.equal(buf.includes(buf1), true);
162+
163+
const buf2 = new DynamicBuffer();
164+
buf2.append('world');
165+
assert.equal(buf.includes(buf2), true);
166+
167+
const buf3 = new DynamicBuffer();
168+
buf3.append('no');
169+
assert.equal(buf.includes(buf3), false);
170+
});
171+
172+
it('Test includes with byteOffset parameter.', () => {
173+
const buf = new DynamicBuffer();
174+
buf.append('Hello world');
175+
176+
assert.equal(buf.includes('Hello'), true);
177+
assert.equal(buf.includes('Hello', 0), true);
178+
assert.equal(buf.includes('Hello', 1), false);
179+
assert.equal(buf.includes('Hello', -1), false);
180+
assert.equal(buf.includes('Hello', -11), true); // -11 equals 0
181+
assert.equal(buf.includes('Hello', 32), false);
182+
assert.equal(buf.includes('', 32), true);
183+
});
184+
});

0 commit comments

Comments
 (0)