|
1 | 1 | # Dynamic Buffer for Node.js |
2 | 2 |
|
3 | 3 | [](https://github.com/ghosind/node-dynamic-buffer) |
| 4 | +[](https://www.npmjs.com/package/dynamic-buffer) |
4 | 5 | [](https://github.com/ghosind/node-dynamic-buffer) |
5 | 6 | [](https://codecov.io/gh/ghosind/node-dynamic-buffer) |
6 | 7 | [](https://github.com/ghosind/node-dynamic-buffer) |
@@ -55,29 +56,146 @@ yarn add dynamic-buffer |
55 | 56 | // Hello world! |
56 | 57 | ``` |
57 | 58 |
|
| 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 | + |
58 | 159 | ## APIs |
59 | 160 |
|
60 | | -- `DynamicBuffer(options?: DynamicBufferOptions)`: Constructor of `DynamicBuffer` class. |
| 161 | +- `append(data: string, length?: number, encoding?: BufferEncoding): number` |
61 | 162 |
|
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. |
65 | 164 |
|
66 | | -- `append(data: string, length?: number, encoding?: BufferEncoding)`: Appends data into buffer. |
| 165 | +- `entries(): IterableIterator<[number, number]>` |
67 | 166 |
|
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. |
71 | 168 |
|
72 | | -- `toBuffer(start?: number, end?: number)`: Exports data to a new builtin buffer object. |
| 169 | +- `keys(): IterableIterator<number>` |
73 | 170 |
|
74 | | - - `start`: The start byte offset. |
75 | | - - `end`: The end byte offset. |
| 171 | + Get an iterator of indices in the buffer. |
76 | 172 |
|
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 | +``` |
78 | 198 |
|
79 | | - - `encoding`: The character encoding for output string. |
80 | | - - `start`: The start byte offset. |
81 | | - - `end`: The end byte offset. |
| 199 | +## License |
82 | 200 |
|
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