Convert ArrayBuffer to text, automatically detect encoding and decode.
| Argument |
Description |
Type |
TransformText | undefined |
Conversion result object or undefined |
TransformText | undefined |
| Property |
Description |
Type |
encoding |
Detected encoding |
string |
content |
Decoded text |
string |
| Parameter |
Description |
Type |
Default |
content |
Content to convert |
string | ArrayBuffer |
Required |
import { transformText } from 'ranuts';
const arrayBuffer = new TextEncoder().encode('Hello World').buffer;
const result = transformText(arrayBuffer);
if (result) {
console.log('Encoding:', result.encoding);
console.log('Content:', result.content); // 'Hello World'
}
import { transformText } from 'ranuts';
async function readTextFile(file) {
const arrayBuffer = await file.arrayBuffer();
const result = transformText(arrayBuffer);
if (result) {
return result.content;
}
return null;
}
import { transformText } from 'ranuts';
// Automatically detect encoding and decode
const result = transformText(arrayBuffer);
if (result) {
console.log(`Successfully decoded using ${result.encoding} encoding`);
console.log(result.content);
}
- Auto detection: Uses
jschardet to automatically detect encoding type.
- ArrayBuffer only: Currently only supports
ArrayBuffer type, string type will output warning.
- Return condition: Only returns result when encoding is detected and decoding succeeds, otherwise returns
undefined.
- Use case: Commonly used for file reading, text decoding, encoding conversion, etc.