1.0.0 β€’ Published 2 years ago

@dwcore/utf8 v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

UTF8

Install

npm i --save @dwcore/utf8

Usage

// CJS
const UTF8 = require('@dwcore/utf8');
// ESM
import UTF8, {
  UTF8Encode,
  UTF8EncodeInto,
  UTF8Decode
} from '@dwcore/utf8';

const string = 'Ave, Darkwolf!🐺🐺🐺$Β’β‚¬πˆ';
const encoded = UTF8.encode(string); // => Uint8Array
const decoded = UTF8.decode(encoded); // => 'Ave, Darkwolf!🐺🐺🐺$Β’β‚¬πˆ'
decoded === string; // => true

const buffer = new Uint8Array(string.length * 2 + 5);
const result = UTF8.encodeInto(string, buffer);
result.read; // => 25
result.written; // => 36
const decodedString = UTF8.decode(buffer); // => 'Ave, Darkwolf!🐺🐺🐺$Β’β‚¬πˆ'
decodedString === string; // => false
const newBuffer = buffer.slice(0, result.written); // => Uint8Array
const newDecodedString = UTF8.decode(newBuffer); // => 'Ave, Darkwolf!🐺🐺🐺$Β’β‚¬πˆ'
newDecodedString === string; // => true

API Documentation