Skip to content

Commit

Permalink
docs(readme): change example code from XMLHttpRequest to use fetch API
Browse files Browse the repository at this point in the history
  • Loading branch information
polygonplanet committed Feb 29, 2024
1 parent a5972b0 commit fd88f12
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Convert and detect character encoding in JavaScript.
* [Code array to string conversion (codeToString/stringToCode)](#code-array-to-string-conversion-codetostringstringtocode)
* [Japanese Zenkaku/Hankaku conversion](#japanese-zenkakuhankaku-conversion)
- [Other examples](#other-examples)
* [Example using the XMLHttpRequest and Typed arrays (Uint8Array)](#example-using-the-xmlhttprequest-and-typed-arrays-uint8array)
* [Example using the `fetch API` and Typed Arrays (Uint8Array)](#example-using-the-fetch-api-and-typed-arrays-uint8array)
* [Convert encoding for file using the File APIs](#convert-encoding-for-file-using-the-file-apis)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -461,36 +461,64 @@ console.log(decoded); // [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]

## Other examples

### Example using the XMLHttpRequest and Typed arrays (Uint8Array)
### Example using the `fetch API` and Typed Arrays (Uint8Array)

This sample reads the text file written in Shift_JIS as binary data,
and displays a string that is converted to Unicode by Encoding.convert.
This example reads a text file encoded in Shift_JIS as binary data,
and display it as a string after converting it to Unicode using [Encoding.convert](#convert-character-encoding-convert).

```javascript
var req = new XMLHttpRequest();
req.open('GET', '/my-shift_jis.txt', true);
(async () => {
try {
const response = await fetch('shift_jis.txt');
const buffer = await response.arrayBuffer();

// Code array with Shift_JIS file contents
const sjisArray = new Uint8Array(buffer);

// Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS
const unicodeArray = Encoding.convert(sjisArray, {
to: 'UNICODE',
from: 'SJIS'
});

// Convert to string from code array for display
const unicodeString = Encoding.codeToString(unicodeArray);
console.log(unicodeString);
} catch (error) {
console.error('Error loading the file:', error);
}
})();
```

<details>
<summary>XMLHttpRequest version of this example</summary>

```javascript
const req = new XMLHttpRequest();
req.open('GET', 'shift_jis.txt', true);
req.responseType = 'arraybuffer';

req.onload = function (event) {
var buffer = req.response;
req.onload = (event) => {
const buffer = req.response;
if (buffer) {
// Shift_JIS Array
var sjisArray = new Uint8Array(buffer);
// Code array with Shift_JIS file contents
const sjisArray = new Uint8Array(buffer);

// Convert encoding to UNICODE (JavaScript Unicode Array).
var unicodeArray = Encoding.convert(sjisArray, {
// Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS
const unicodeArray = Encoding.convert(sjisArray, {
to: 'UNICODE',
from: 'SJIS'
});

// Join to string.
var unicodeString = Encoding.codeToString(unicodeArray);
// JConvert to string from code array for display
const unicodeString = Encoding.codeToString(unicodeArray);
console.log(unicodeString);
}
};

req.send(null);
```
</details>

### Convert encoding for file using the File APIs

Expand Down

0 comments on commit fd88f12

Please sign in to comment.