You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,8 @@ yarn add dynamic-buffer
69
69
70
70
-[Iteration](#iteration)
71
71
72
+
-[Comparison](#comparison)
73
+
72
74
-[Export Data](#export-data)
73
75
74
76
-[APIs](#apis)
@@ -100,6 +102,15 @@ console.log(buf.toString());
100
102
// JavaScript!
101
103
```
102
104
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
+
103
114
### Iteration
104
115
105
116
`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());
134
145
135
146
-`keys()` returns an iterator of buffer keys (indices).
136
147
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
+
137
172
### Export Data
138
173
139
174
You can export buffer content (without unused parts) to string, `Buffer` object, or JSON representation object.
0 commit comments