https://github.com/loopmode/utf8-byte-chunks
Utility to split a string into chunks of a given byte size
https://github.com/loopmode/utf8-byte-chunks
Last synced: about 1 year ago
JSON representation
Utility to split a string into chunks of a given byte size
- Host: GitHub
- URL: https://github.com/loopmode/utf8-byte-chunks
- Owner: loopmode
- Created: 2018-06-26T12:56:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-26T14:22:10.000Z (almost 8 years ago)
- Last Synced: 2024-05-28T16:05:58.421Z (almost 2 years ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# utf8-byte-chunks
A utility that splits a given string into chunks of a maximum size in bytes.
## use case
The primary use case was feeding user input to a chat API that accepts input of max 255 bytes per message.
Limiting the number of input characters was not an option. Users should be able to submit larger text, e.g. copy-pasted from some documents, potentially containing multibyte characters.
The most viable option seemed to be sending multiple requests, each carrying up to 255 bytes of text, until the entire message was sent.
## installation
Via npm:
```javascript
npm install --save utf8-byte-chunks
```
Via yarn:
```javascript
yarn add utf8-byte-chunks
```
## Example usage
```javascript
import getBytesizedChunks from 'utf8-byte-chunks';
async handleSubmit(event) {
event.preventDefault();
// split the current text into chunks, then submit each of them individually
const chunks = getBytesizedChunks(this.state.text, 255);
for (let chunk of chunks) {
if (this._isMounted) {
await ChatStore.submitMessage(chunk);
}
}
if (this._isMounted) {
this.setState({ text: '' }, this.focusInput);
}
}
```
## Options
### Split on spaces
Pass `true` as the third argument (`splitOnSpace`) to avoid the initial value to be split in the middle of a word and respect spaces instead.