Skip to content

Commit b5dd0de

Browse files
committed
docs: update readme.
1 parent 8184562 commit b5dd0de

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ yarn add dynamic-buffer
6969

7070
- [Iteration](#iteration)
7171

72+
- [Comparison](#comparison)
73+
7274
- [Export Data](#export-data)
7375

7476
- [APIs](#apis)
@@ -100,6 +102,15 @@ console.log(buf.toString());
100102
// JavaScript!
101103
```
102104

105+
You can also use `write` method to write data to the specified position in the buffer:
106+
107+
```ts
108+
buf.append('Hello world!');
109+
buf.write('Node.js', 6);
110+
console.log(buf.toString());
111+
// Hello Node.js
112+
```
113+
103114
### Iteration
104115

105116
`DynamicBuffer` provides three ways to iterate data from the specified buffer, you can use them with `for...of` statement.
@@ -134,6 +145,30 @@ console.log(buf.toString());
134145

135146
- `keys()` returns an iterator of buffer keys (indices).
136147

148+
### Comparison
149+
150+
You can compare `DynamicBuffer` object with another `DynamicBuffer` object, Node.js builtin `Buffer` object, or an `Uint8Array` by `compare` or `equals` methods.
151+
152+
For `compare` method, it returns a number to indicate whether the buffer comes before, after, or is the same as another buffer in sort order.
153+
154+
```ts
155+
buf.append('ABC');
156+
console.log(buf.compare(Buffer.from('ABC')));
157+
// 0
158+
console.log(buf.compare(Buffer.from('BCD')));
159+
// -1
160+
```
161+
162+
For `equals` method, it returns a boolean value to indicate whether the buffer is the same as the target buffer.
163+
164+
```ts
165+
buf.append('ABC');
166+
console.log(buf.equals(Buffer.from('ABC')));
167+
// true
168+
console.log(buf.equals(Buffer.from('BCD')));
169+
// false
170+
```
171+
137172
### Export Data
138173

139174
You can export buffer content (without unused parts) to string, `Buffer` object, or JSON representation object.
@@ -163,6 +198,14 @@ console.log(buf.toString('utf8', 6, 11));
163198

164199
Append string into buffer.
165200

201+
- `write(data: string, offset?: number, length?: number, encoding?: BufferEncoding): number`
202+
203+
Write string into buffer at the specified position.
204+
205+
- `read(offset?: number): number`
206+
207+
Read a byte at the specified position in the buffer.
208+
166209
- `entries(): IterableIterator<[number, number]>`
167210

168211
Get an iterator of index-byte pairs from the buffer.
@@ -187,6 +230,14 @@ console.log(buf.toString('utf8', 6, 11));
187230

188231
Return a JSON representation object.
189232

233+
- `compare(target: DynamicBuffer | Buffer | Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number)`
234+
235+
Compares two buffers and returns a number indicating whether this buffer comes before, after, or is the same as another buffer in sort order.
236+
237+
- `equals(otherBuffer: DynamicBuffer | Buffer | Uint8Array): boolean`
238+
239+
Compares and returns a boolean value. Returns `true` if two buffers have exactly the same bytes, `false` otherwise.
240+
190241
## Run Tests
191242

192243
All of test cases are written with `mocha`, `assert`, and `nyc`. They can be run with the following commands:

0 commit comments

Comments
 (0)