Skip to content

Commit f7edb23

Browse files
committed
doc: update readme.
1 parent 0a802ce commit f7edb23

File tree

1 file changed

+134
-16
lines changed

1 file changed

+134
-16
lines changed

README.md

Lines changed: 134 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Dynamic Buffer for Node.js
22

33
[![Latest version](https://img.shields.io/github/v/release/ghosind/node-dynamic-buffer?include_prereleases)](https://github.com/ghosind/node-dynamic-buffer)
4+
[![NPM Package](https://img.shields.io/npm/v/dynamic-buffer)](https://www.npmjs.com/package/dynamic-buffer)
45
[![Github Actions build](https://img.shields.io/github/workflow/status/ghosind/node-dynamic-buffer/Test)](https://github.com/ghosind/node-dynamic-buffer)
56
[![codecov](https://codecov.io/gh/ghosind/node-dynamic-buffer/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/node-dynamic-buffer)
67
[![License](https://img.shields.io/github/license/ghosind/node-dynamic-buffer)](https://github.com/ghosind/node-dynamic-buffer)
@@ -55,29 +56,146 @@ yarn add dynamic-buffer
5556
// Hello world!
5657
```
5758

59+
## Table of Contents
60+
61+
- [Installation](#installation)
62+
63+
- [Getting Started](#getting-started)
64+
65+
- [Usages](#usages)
66+
67+
- [Write Data](#write-data)
68+
69+
- [Iteration](#iteration)
70+
71+
- [Export Data](#export-data)
72+
73+
- [APIs](#apis)
74+
75+
- [Run Tests](#run-tests)
76+
77+
- [License](#license)
78+
79+
## Usages
80+
81+
### Write Data
82+
83+
You can write a string into a buffer by `append` method, it'll add the string to the end of the buffer:
84+
85+
```ts
86+
buf.append('Hello');
87+
buf.append(' ');
88+
buf.append('world');
89+
console.log(buf.toString());
90+
// Hello world
91+
```
92+
93+
And you can also set the string length to write. Like the second line of the following example, it'll write `'Script!'` into buffer and without the last two `'!'` symbols:
94+
95+
```ts
96+
buf.append('Java');
97+
buf.append('Script!!!', 7);
98+
console.log(buf.toString());
99+
// JavaScript!
100+
```
101+
102+
### Iteration
103+
104+
`DynamicBuffer` provides three ways to iterate data from the specified buffer, you can use them with `for...of` statement.
105+
106+
- `entries()` returns an iterator of key-value pair (index and byte) from the buffer.
107+
108+
```ts
109+
buf.append('Hello');
110+
for (const pair of buf.entries()) {
111+
console.log(pair);
112+
}
113+
// [ 0, 72 ]
114+
// [ 1, 101 ]
115+
// [ 2, 108 ]
116+
// [ 3, 108 ]
117+
// [ 4, 111 ]
118+
```
119+
120+
- `values()` returns an iterator of data(byte) from the buffer.
121+
122+
```ts
123+
buf.append('Hello');
124+
for (const value of buf.values()) {
125+
console.log(value);
126+
}
127+
// 72
128+
// 101
129+
// 108
130+
// 108
131+
// 111
132+
```
133+
134+
- `keys()` returns an iterator of buffer keys (indices).
135+
136+
### Export Data
137+
138+
You can export buffer content (without unused parts) to string, `Buffer` object, or JSON representation object.
139+
140+
```ts
141+
buf.append('Hello');
142+
console.log(buf.toString());
143+
// Hello
144+
const dataBuffer = buf.toBuffer();
145+
console.log(buf.length, dataBuffer.toString());
146+
// 5 Hello
147+
console.log(JSON.stringify(buf)); // JSON.stringify implicitly calls toJSON method.
148+
// {"type":"Buffer","data":[72,101,108,108,111]}
149+
```
150+
151+
For `toString` and `toBuffer` methods, you can also set the start and end offsets to export the subset of written data.
152+
153+
```ts
154+
buf.append('Hello world!!!');
155+
console.log(buf.toString('utf8', 6, 11));
156+
// world
157+
```
158+
58159
## APIs
59160

60-
- `DynamicBuffer(options?: DynamicBufferOptions)`: Constructor of `DynamicBuffer` class.
161+
- `append(data: string, length?: number, encoding?: BufferEncoding): number`
61162

62-
- `options.size`: Initial buffer size, default 16.
63-
- `options.fill`: Pre-fill value to the buffer, default 0.
64-
- `options.encoding`: Character encoding for pre-fill value if it's a string, default `'utf8'`.
163+
Append string into buffer.
65164

66-
- `append(data: string, length?: number, encoding?: BufferEncoding)`: Appends data into buffer.
165+
- `entries(): IterableIterator<[number, number]>`
67166

68-
- `data`: String to write into this buffer.
69-
- `length`: The number of bytes to write.
70-
- `encoding`: Character encoding of this string.
167+
Get an iterator of index-byte pairs from the buffer.
71168

72-
- `toBuffer(start?: number, end?: number)`: Exports data to a new builtin buffer object.
169+
- `keys(): IterableIterator<number>`
73170

74-
- `start`: The start byte offset.
75-
- `end`: The end byte offset.
171+
Get an iterator of indices in the buffer.
76172

77-
- `toString(encoding?: BufferEncoding, start?: number, end?: number)`: Exports data to a string.
173+
- `values(): IterableIterator<number>`
174+
175+
Get an iterator of data in the buffer.
176+
177+
- `toString(encoding?: BufferEncoding, start?: number, end?: number): string`
178+
179+
Decode buffer to a string.
180+
181+
- `toBuffer(start?: number, end?: number): Buffer`
182+
183+
Return a new builtin Buffer object.
184+
185+
- `toJSON(): { type: string, data: number[] }`
186+
187+
Return a JSON representation object.
188+
189+
## Run Tests
190+
191+
All of test cases are written with `mocha`, `assert`, and `nyc`. They can be run with the following commands:
192+
193+
```sh
194+
npm test
195+
# or
196+
yarn test
197+
```
78198

79-
- `encoding`: The character encoding for output string.
80-
- `start`: The start byte offset.
81-
- `end`: The end byte offset.
199+
## License
82200

83-
- `toJSON()`: Exports data to a JSON representation.
201+
This project was published under MIT license, you can see more detail in [LICENSE file](./LICENSE).

0 commit comments

Comments
 (0)