react-native-encoding-api
v1.0.1
Published
A React Native package for handling text encoding and decoding
Downloads
23
Maintainers
Readme
react-native-encoding-api
A complete implementation of the Web API's TextEncoder and TextDecoder for React Native. This package provides both encoding and decoding functionality with the exact same API and behavior as the Web API.
Requirements
- React Native >= 0.60.0
- TypeScript >= 4.0.0 (for TypeScript projects)
Installation
npm install react-native-encoding-api
# or
yarn add react-native-encoding-apiUsage
import { TextEncoder, TextDecoder } from 'react-native-encoding-api';
// Encoding text to bytes
const encoder = new TextEncoder();
const bytes = encoder.encode('Hello World!');
// Decoding bytes back to text
const decoder = new TextDecoder();
const text = decoder.decode(bytes);
console.log(text); // Output: "Hello World!"API
TextEncoder
new TextEncoder()encode(text: string): Uint8Array: Encodes a string into UTF-8 bytes
TextDecoder
new TextDecoder(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean })encoding(optional): Currently only 'utf-8' is supportedoptions(optional):fatal: If true, throws an error for invalid UTF-8 sequencesignoreBOM: If true, ignores the UTF-8 BOM (Byte Order Mark)
decode(input: Uint8Array): string: Decodes a Uint8Array into a string
License
MIT
