|
1 | 1 | # Encoding |
2 | 2 |
|
3 | | -:::warning |
4 | | -**Note:** Work is in progress 🚧 |
5 | | -::: |
| 3 | +This allow to **encode** and **decode** strings with different character sets. This is especially useful when working with **files** that require specific encodings or when handling international text. |
| 4 | + |
| 5 | +## Importing Encodings |
| 6 | + |
| 7 | +To use the Encoding utilities, you need to import them using the `acode.require` method: |
| 8 | + |
| 9 | +```javascript |
| 10 | +const encodings = acode.require('encodings'); |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Encoding a String |
| 16 | + |
| 17 | +The `encode` method converts a string into an ArrayBuffer using the specified character set. |
| 18 | + |
| 19 | +```javascript |
| 20 | +const text = 'Hello World!'; |
| 21 | +const charset = 'utf-8'; |
| 22 | + |
| 23 | +const encoded = await encodings.encode(text, charset); |
| 24 | +console.log(encoded); // Output: ArrayBuffer |
| 25 | +``` |
| 26 | + |
| 27 | +### Decoding an ArrayBuffer |
| 28 | + |
| 29 | +The `decode` method converts an ArrayBuffer back into a string using the specified character set. |
| 30 | + |
| 31 | +```javascript |
| 32 | +const buffer = await encodings.encode(text, charset); |
| 33 | +const decoded = await encodings.decode(buffer, charset); |
| 34 | +console.log(decoded); // Output: 'Hello World!' |
| 35 | +``` |
| 36 | + |
| 37 | +## Methods |
| 38 | + |
| 39 | +### `encode(text: string, charset: string): Promise<ArrayBuffer>` |
| 40 | + |
| 41 | +Encodes a string with the specified character set. |
| 42 | + |
| 43 | +- **text**: The text to encode. |
| 44 | +- **charset**: The character set name (e.g., UTF-8, GBK). |
6 | 45 |
|
7 | | -We are currently working on this section to provide you with detailed and comprehensive information about how Acode plugins work. Please check back soon for updates! |
| 46 | +### `decode(buffer: ArrayBuffer, charset: string): Promise<string>` |
8 | 47 |
|
9 | | -## Contribute to the Documentation |
| 48 | +Decodes an ArrayBuffer into a string using the specified character set. |
10 | 49 |
|
11 | | -We welcome contributions from the community! If you would like to help improve this documentation, please visit our [GitHub repository](https://github.com/bajrangCoder/acode-plugin-docs) and follow the contribution guidelines. |
| 50 | +- **buffer**: The ArrayBuffer to decode. |
| 51 | +- **charset**: The character set name (e.g., UTF-8, GBK). |
12 | 52 |
|
13 | | -:::tip |
14 | | -You can suggest changes, add new content, or improve existing sections. Every bit of help is appreciated! 🤗 |
| 53 | +## Properties |
| 54 | + |
| 55 | +### `encodings: Array<Encoding>` |
| 56 | + |
| 57 | +An array of all the supported encodings. Each `Encoding` object contains: |
| 58 | + |
| 59 | +- **name**: The name of the encoding. |
| 60 | +- **labels**: The labels of the encoding. |
| 61 | +- **aliases**: The aliases of the encoding. |
| 62 | + |
| 63 | +## Example |
| 64 | + |
| 65 | +Here is a comprehensive example demonstrating how to encode and decode text using different character sets. |
| 66 | + |
| 67 | +```javascript |
| 68 | +const encodings = acode.require('encodings'); |
| 69 | + |
| 70 | +async function example() { |
| 71 | + const text = 'Hello World!'; |
| 72 | + const charset = 'utf-8'; |
| 73 | + |
| 74 | + try { |
| 75 | + // Encoding text |
| 76 | + const encoded = await encodings.encode(text, charset); |
| 77 | + console.log('Encoded:', encoded); |
| 78 | + |
| 79 | + // Decoding text |
| 80 | + const decoded = await encodings.decode(encoded, charset); |
| 81 | + console.log('Decoded:', decoded); |
| 82 | + |
| 83 | + } catch (error) { |
| 84 | + console.error('Encoding/Decoding error:', error); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +example(); |
| 89 | +``` |
| 90 | + |
| 91 | +:::info |
| 92 | +Always handle encoding and decoding within a try-catch block to manage potential errors, such as unsupported character sets. |
15 | 93 | ::: |
16 | 94 |
|
17 | | -For more information, see official [Guide](https://acode.app/plugin-docs). |
| 95 | +### Checking Available Encodings |
| 96 | + |
| 97 | +You can check all the available encodings supported by Acode: |
| 98 | + |
| 99 | +```javascript |
| 100 | +console.log(encodings.encodings); |
| 101 | +``` |
| 102 | + |
| 103 | +This will print an array of `Encoding` objects, each containing details about the encoding. |
0 commit comments