Skip to content

Commit 1d6535a

Browse files
committed
feat: add compare and equals methods.
- Add compare and equals methods. - Add test cases for compare and equals methods. - Update eslint rule.
1 parent faa52f0 commit 1d6535a

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"import/prefer-default-export": "off",
2525
"max-classes-per-file": "off",
2626
"no-dupe-class-members": "off",
27+
"no-restricted-syntax": "off",
2728
"no-unused-vars": "off"
2829
},
2930
"settings": {

src/dynamicBuffer.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,83 @@ export class DynamicBuffer {
388388
}(this);
389389
}
390390

391+
/**
392+
* Compares this buffer with target and returns a number to indicate whether comes before, after
393+
* or they are the same in sort order.
394+
*
395+
* ```js
396+
* buf1.append('ABC');
397+
* buf2.append('BCD');
398+
* console.log(buf1.compare(buf2));
399+
* // -1
400+
* ```
401+
*
402+
* @param target A buffer (`DynamicBuffer` or builtin `Buffer`) or `Uint8Array` with which to
403+
* compare this buffer.
404+
* @param targetStart The offset within `target` at which to begin comparison, default `0`.
405+
* @param targetEnd the offset within `target` at which to end comparison (not inclusive),
406+
* default `target.length`.
407+
* @param sourceStart The offset within this buffer at which to begin comparison, default `0`.
408+
* @param sourceEnd the offset within this buffer at which to end comparison (not inclusive),
409+
* default `target.length`.
410+
* @returns `0` if `target` is the same as this buffer; `1` if `target` should come before this
411+
* buffer when sorted; `-1` if `target` should come after this buffer when sorted.
412+
*/
413+
compare(
414+
target: DynamicBuffer | Buffer | Uint8Array,
415+
targetStart: number = 0,
416+
targetEnd: number = target.length,
417+
sourceStart: number = 0,
418+
sourceEnd: number = this.length,
419+
) {
420+
const otherBuf = target instanceof DynamicBuffer ? (target.buffer || ([] as number[])) : target;
421+
const thisBuf = this.buffer || ([] as number[]);
422+
423+
let i = targetStart;
424+
let j = sourceStart;
425+
426+
while (i < targetEnd && j < sourceEnd && i < target.length && j < this.length) {
427+
if (thisBuf[j] !== otherBuf[i]) {
428+
return thisBuf[j] > otherBuf[i] ? 1 : -1;
429+
}
430+
431+
i += 1;
432+
j += 1;
433+
}
434+
435+
if (i < targetEnd || (i < target.length && target.length < targetEnd)) {
436+
return -1;
437+
}
438+
if (j < sourceEnd || (j < this.length && this.length < sourceEnd)) {
439+
return 1;
440+
}
441+
442+
return 0;
443+
}
444+
445+
/**
446+
* Compare this buffer with another buffer (`DynamicBuffer` or `Buffer`) or an `Uint8Array`, and
447+
* returns true if they are equal, and false if not.
448+
*
449+
* This method is equivalent to `buf.compare(otherBuffer) === 0`.
450+
*
451+
* ```js
452+
* const buf1 = new DynamicBuffer();
453+
* buf1.append('Hello world');
454+
* const buf2 = Buffer.from('Hello world');
455+
*
456+
* console.log(buf1.equals(buf2));
457+
* // true
458+
* ```
459+
*
460+
* @param otherBuffer A buffer (`DynamicBuffer` or builtin `Buffer`) or `Uint8Array` with which
461+
* to compare this buffer.
462+
* @returns `true` if two buffers have exactly the same bytes, `false` otherwise.
463+
*/
464+
equals(otherBuffer: DynamicBuffer | Buffer | Uint8Array) {
465+
return this.compare(otherBuffer, 0, otherBuffer.length, 0, this.length) === 0;
466+
}
467+
391468
/**
392469
* Write data into internal buffer with the specified offset.
393470
*

test/comparison.spec.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import assert from 'assert';
2+
import { describe, it } from 'mocha';
3+
4+
import { DynamicBuffer } from '../src';
5+
6+
describe('Comparison tests', () => {
7+
it('Compare with other DynamicBuffers', () => {
8+
const buf1 = new DynamicBuffer();
9+
const buf2 = new DynamicBuffer();
10+
const buf3 = new DynamicBuffer();
11+
12+
buf1.append('ABC');
13+
buf2.append('BCD');
14+
buf3.append('ABCD');
15+
16+
assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC');
17+
assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD');
18+
assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD');
19+
assert.equal(buf2.compare(buf1), 1, 'expect BCD > ABC');
20+
assert.equal(buf2.compare(buf3), 1, 'expect BCD > ABCD');
21+
assert.equal(buf3.compare(buf1), 1, 'expect ABCD > ABC');
22+
assert.equal(buf3.compare(buf2), -1, 'expect ABCD < BCD');
23+
});
24+
25+
it('Compare with other Buffer (1)', () => {
26+
const buf1 = new DynamicBuffer();
27+
buf1.append('ABC');
28+
29+
const buf2 = Buffer.from('BCD');
30+
const buf3 = Buffer.from('ABCD');
31+
32+
assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC');
33+
assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD');
34+
assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD');
35+
});
36+
37+
it('Compare with other Buffer (2)', () => {
38+
const buf1 = new DynamicBuffer();
39+
buf1.append('BCD');
40+
41+
const buf2 = Buffer.from('ABC');
42+
const buf3 = Buffer.from('ABCD');
43+
44+
assert.equal(buf1.compare(buf2), 1, 'expect BCD > ABC');
45+
assert.equal(buf1.compare(buf3), 1, 'expect BCD > ABCD');
46+
});
47+
48+
it('Compare with other Buffer (3)', () => {
49+
const buf1 = new DynamicBuffer();
50+
buf1.append('ABCD');
51+
52+
const buf2 = Buffer.from('ABC');
53+
const buf3 = Buffer.from('BCD');
54+
55+
assert.equal(buf1.compare(buf2), 1, 'expect ABCD > ABC');
56+
assert.equal(buf1.compare(buf3), -1, 'expect ABCD < BCD');
57+
});
58+
59+
it('Compare with other Uint8Array (1)', () => {
60+
const buf1 = new DynamicBuffer();
61+
buf1.append('ABC');
62+
63+
const buf2 = new Uint8Array([0x42, 0x43, 0x44]);
64+
const buf3 = new Uint8Array([0x41, 0x42, 0x43, 0x44]);
65+
66+
assert.equal(buf1.compare(buf1), 0, 'expected ABC == ABC');
67+
assert.equal(buf1.compare(buf2), -1, 'expect ABC < BCD');
68+
assert.equal(buf1.compare(buf3), -1, 'expect ABC < ABCD');
69+
});
70+
71+
it('Compare with other Uint8Array (2)', () => {
72+
const buf1 = new DynamicBuffer();
73+
buf1.append('BCD');
74+
75+
const buf2 = new Uint8Array([0x41, 0x42, 0x43]);
76+
const buf3 = new Uint8Array([0x41, 0x42, 0x43, 0x44]);
77+
78+
assert.equal(buf1.compare(buf2), 1, 'expect BCD > ABC');
79+
assert.equal(buf1.compare(buf3), 1, 'expect BCD > ABCD');
80+
});
81+
82+
it('Compare with other Uint8Array (3)', () => {
83+
const buf1 = new DynamicBuffer();
84+
buf1.append('ABCD');
85+
86+
const buf2 = new Uint8Array([0x41, 0x42, 0x43]);
87+
const buf3 = new Uint8Array([0x42, 0x43, 0x44]);
88+
89+
assert.equal(buf1.compare(buf2), 1, 'expect ABCD > ABC');
90+
assert.equal(buf1.compare(buf3), -1, 'expect ABCD < BCD');
91+
});
92+
});
93+
94+
describe('Equality tests', () => {
95+
it('Compare equals with other DynamicBuffers', () => {
96+
const buf1 = new DynamicBuffer();
97+
const buf2 = new DynamicBuffer();
98+
const buf3 = new DynamicBuffer();
99+
const buf4 = new DynamicBuffer();
100+
101+
buf1.append('ABC');
102+
buf2.append('ABC');
103+
buf3.append('BCD');
104+
buf4.append('ABCD');
105+
106+
assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC');
107+
assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC');
108+
assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC');
109+
assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD');
110+
});
111+
112+
it('Compare equals with other Buffer', () => {
113+
const buf1 = new DynamicBuffer();
114+
buf1.append('ABC');
115+
116+
const buf2 = Buffer.from('ABC');
117+
const buf3 = Buffer.from('BCD');
118+
const buf4 = Buffer.from('ABCD');
119+
120+
assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC');
121+
assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC');
122+
assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC');
123+
assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD');
124+
});
125+
126+
it('Compare equals with other Uint8Array', () => {
127+
const buf1 = new DynamicBuffer();
128+
buf1.append('ABC');
129+
130+
const buf2 = new Uint8Array([0x41, 0x42, 0x43]);
131+
const buf3 = new Uint8Array([0x42, 0x43, 0x44]);
132+
const buf4 = new Uint8Array([0x41, 0x42, 0x43, 0x44]);
133+
134+
assert.equal(buf1.equals(buf1), true, 'expected ABC == ABC');
135+
assert.equal(buf1.equals(buf2), true, 'expected ABC == ABC');
136+
assert.equal(buf1.equals(buf3), false, 'expected ABC != ABC');
137+
assert.equal(buf1.equals(buf4), false, 'expected ABC != ABCD');
138+
});
139+
});

0 commit comments

Comments
 (0)