@mavogel/awscdk-rootmail 0.0.39 → 0.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +9 -9
- package/lib/rootmail.js +1 -1
- package/lib/ses-receive.js +1 -1
- package/node_modules/@aws-sdk/client-cloudwatch-logs/package.json +2 -2
- package/node_modules/@aws-sdk/client-route-53/package.json +2 -2
- package/node_modules/@aws-sdk/client-s3/package.json +2 -2
- package/node_modules/@aws-sdk/client-ses/package.json +2 -2
- package/node_modules/@aws-sdk/client-ssm/README.md +8 -0
- package/node_modules/@aws-sdk/client-ssm/dist-cjs/index.js +213 -73
- package/node_modules/@aws-sdk/client-ssm/dist-es/SSM.js +2 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/commands/DescribeInstancePropertiesCommand.js +24 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/commands/DescribeMaintenanceWindowTargetsCommand.js +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-es/commands/index.js +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/models/models_0.js +30 -9
- package/node_modules/@aws-sdk/client-ssm/dist-es/models/models_1.js +9 -43
- package/node_modules/@aws-sdk/client-ssm/dist-es/models/models_2.js +43 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/pagination/DescribeInstancePropertiesPaginator.js +4 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/pagination/index.js +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-es/protocols/Aws_json1_1.js +78 -3
- package/node_modules/@aws-sdk/client-ssm/dist-types/SSM.d.ts +8 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/SSMClient.d.ts +3 -2
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/DescribeInstancePropertiesCommand.d.ts +151 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/DescribeMaintenanceWindowScheduleCommand.d.ts +2 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/DescribeMaintenanceWindowTargetsCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/DescribeMaintenanceWindowTasksCommand.d.ts +1 -2
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/DescribeMaintenanceWindowsForTargetCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/StartChangeRequestExecutionCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/StartSessionCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/commands/index.d.ts +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/models/models_0.d.ts +334 -243
- package/node_modules/@aws-sdk/client-ssm/dist-types/models/models_1.d.ts +219 -234
- package/node_modules/@aws-sdk/client-ssm/dist-types/models/models_2.d.ts +246 -8
- package/node_modules/@aws-sdk/client-ssm/dist-types/pagination/DescribeInstancePropertiesPaginator.d.ts +7 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/pagination/index.d.ts +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/protocols/Aws_json1_1.d.ts +9 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/SSM.d.ts +18 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/SSMClient.d.ts +6 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/DescribeInstancePropertiesCommand.d.ts +39 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/DescribeMaintenanceWindowScheduleCommand.d.ts +2 -4
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/DescribeMaintenanceWindowTargetsCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/DescribeMaintenanceWindowTasksCommand.d.ts +4 -2
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/DescribeMaintenanceWindowsForTargetCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/StartChangeRequestExecutionCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/StartSessionCommand.d.ts +1 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/commands/index.d.ts +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/models/models_0.d.ts +79 -49
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/models/models_1.d.ts +51 -60
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/models/models_2.d.ts +62 -1
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/pagination/DescribeInstancePropertiesPaginator.d.ts +11 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/pagination/index.d.ts +1 -0
- package/node_modules/@aws-sdk/client-ssm/dist-types/ts3.4/protocols/Aws_json1_1.d.ts +12 -0
- package/node_modules/@aws-sdk/client-ssm/package.json +2 -2
- package/node_modules/@aws-sdk/credential-provider-node/dist-cjs/index.js +2 -2
- package/node_modules/@aws-sdk/credential-provider-node/dist-es/defaultProvider.js +1 -1
- package/node_modules/@aws-sdk/credential-provider-node/package.json +1 -1
- package/node_modules/cdk-nag/.jsii +2 -2
- package/node_modules/cdk-nag/lib/ignore-suppression-conditions.js +5 -5
- package/node_modules/cdk-nag/lib/nag-logger.js +2 -2
- package/node_modules/cdk-nag/lib/nag-pack.js +1 -1
- package/node_modules/cdk-nag/lib/nag-rules.js +1 -1
- package/node_modules/cdk-nag/lib/nag-suppressions.js +1 -1
- package/node_modules/cdk-nag/lib/packs/aws-solutions.js +1 -1
- package/node_modules/cdk-nag/lib/packs/hipaa-security.js +1 -1
- package/node_modules/cdk-nag/lib/packs/nist-800-53-r4.js +1 -1
- package/node_modules/cdk-nag/lib/packs/nist-800-53-r5.js +1 -1
- package/node_modules/cdk-nag/lib/packs/pci-dss-321.js +1 -1
- package/node_modules/cdk-nag/package.json +1 -1
- package/node_modules/encoding-japanese/README.md +449 -212
- package/node_modules/encoding-japanese/encoding.js +2 -2
- package/node_modules/encoding-japanese/encoding.min.js +2 -3
- package/node_modules/encoding-japanese/package.json +7 -8
- package/node_modules/libmime/.ncurc.js +3 -1
- package/node_modules/libmime/CHANGELOG.md +7 -0
- package/node_modules/libmime/lib/get-charset-name.js +227 -0
- package/node_modules/libmime/package.json +3 -3
- package/node_modules/mailparser/.ncurc.js +8 -0
- package/node_modules/mailparser/CHANGELOG.md +7 -0
- package/node_modules/mailparser/lib/mail-parser.js +1 -1
- package/node_modules/mailparser/package.json +7 -7
- package/node_modules/mailsplit/node_modules/encoding-japanese/LICENSE +21 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/README.md +542 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/encoding.js +6077 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/encoding.min.js +8 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/package.json +70 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/banner.js +6 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/config.js +139 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/encoding-convert.js +1676 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/encoding-detect.js +502 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/encoding-table.js +4 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/index.js +593 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/jis-to-utf8-table.js +5 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/jisx0212-to-utf8-table.js +5 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/kana-case-table.js +41 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/utf8-to-jis-table.js +1493 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/utf8-to-jisx0212-table.js +1224 -0
- package/node_modules/mailsplit/node_modules/encoding-japanese/src/util.js +361 -0
- package/node_modules/nodemailer/CHANGELOG.md +14 -0
- package/node_modules/nodemailer/lib/mime-node/index.js +1 -1
- package/node_modules/nodemailer/lib/smtp-connection/index.js +13 -0
- package/node_modules/nodemailer/package.json +3 -3
- package/node_modules/{punycode → punycode.js}/package.json +1 -1
- package/node_modules/tlds/index.json +0 -2
- package/node_modules/tlds/package.json +1 -1
- package/package.json +9 -9
- /package/node_modules/{encoding-japanese → mailsplit/node_modules/encoding-japanese}/CHANGELOG.md +0 -0
- /package/node_modules/{encoding-japanese → mailsplit/node_modules/encoding-japanese}/encoding.min.js.map +0 -0
- /package/node_modules/{punycode → punycode.js}/LICENSE-MIT.txt +0 -0
- /package/node_modules/{punycode → punycode.js}/README.md +0 -0
- /package/node_modules/{punycode → punycode.js}/punycode.es6.js +0 -0
- /package/node_modules/{punycode → punycode.js}/punycode.js +0 -0
|
@@ -2,62 +2,69 @@ encoding.js
|
|
|
2
2
|
===========
|
|
3
3
|
|
|
4
4
|
[](https://www.npmjs.com/package/encoding-japanese)
|
|
5
|
-
[](https://github.com/polygonplanet/encoding.js/actions)
|
|
6
6
|
[](https://github.com/polygonplanet/encoding.js/blob/master/LICENSE)
|
|
7
7
|
|
|
8
|
-
Convert
|
|
8
|
+
Convert and detect character encoding in JavaScript.
|
|
9
9
|
|
|
10
|
-
[**README (
|
|
10
|
+
[**README (日本語)**](README_ja.md)
|
|
11
11
|
|
|
12
12
|
## Table of contents
|
|
13
13
|
|
|
14
14
|
- [Features](#features)
|
|
15
|
-
* [How to
|
|
15
|
+
* [How to Use Character Encoding in Strings?](#how-to-use-character-encoding-in-strings)
|
|
16
16
|
- [Installation](#installation)
|
|
17
17
|
* [npm](#npm)
|
|
18
18
|
+ [TypeScript](#typescript)
|
|
19
|
-
* [
|
|
19
|
+
* [Browser (standalone)](#browser-standalone)
|
|
20
20
|
* [CDN](#cdn)
|
|
21
21
|
- [Supported encodings](#supported-encodings)
|
|
22
22
|
* [About `UNICODE`](#about-unicode)
|
|
23
23
|
- [Example usage](#example-usage)
|
|
24
24
|
- [Demo](#demo)
|
|
25
25
|
- [API](#api)
|
|
26
|
-
* [
|
|
27
|
-
* [
|
|
28
|
-
+ [Specify conversion options to the argument `
|
|
26
|
+
* [detect : Detects character encoding](#encodingdetect-data-encodings)
|
|
27
|
+
* [convert : Converts character encoding](#encodingconvert-data-to-from)
|
|
28
|
+
+ [Specify conversion options to the argument `to` as an object](#specify-conversion-options-to-the-argument-to-as-an-object)
|
|
29
29
|
+ [Specify the return type by the `type` option](#specify-the-return-type-by-the-type-option)
|
|
30
|
-
+ [
|
|
30
|
+
+ [Replacing characters with HTML entities when they cannot be represented](#replacing-characters-with-html-entities-when-they-cannot-be-represented)
|
|
31
31
|
+ [Specify BOM in UTF-16](#specify-bom-in-utf-16)
|
|
32
|
-
* [
|
|
33
|
-
* [
|
|
34
|
-
* [
|
|
32
|
+
* [urlEncode : Encodes to percent-encoded string](#encodingurlencode-data)
|
|
33
|
+
* [urlDecode : Decodes from percent-encoded string](#encodingurldecode-string)
|
|
34
|
+
* [base64Encode : Encodes to Base64 formatted string](#encodingbase64encode-data)
|
|
35
|
+
* [base64Decode : Decodes from Base64 formatted string](#encodingbase64decode-string)
|
|
36
|
+
* [codeToString : Converts character code array to string](#encodingcodetostring-code)
|
|
37
|
+
* [stringToCode : Converts string to character code array](#encodingstringtocode-string)
|
|
35
38
|
* [Japanese Zenkaku/Hankaku conversion](#japanese-zenkakuhankaku-conversion)
|
|
36
39
|
- [Other examples](#other-examples)
|
|
37
|
-
* [Example using the
|
|
40
|
+
* [Example using the `fetch API` and Typed Arrays (Uint8Array)](#example-using-the-fetch-api-and-typed-arrays-uint8array)
|
|
38
41
|
* [Convert encoding for file using the File APIs](#convert-encoding-for-file-using-the-file-apis)
|
|
39
42
|
- [Contributing](#contributing)
|
|
40
43
|
- [License](#license)
|
|
41
44
|
|
|
42
45
|
## Features
|
|
43
46
|
|
|
44
|
-
encoding.js is a JavaScript library for converting and detecting character encodings
|
|
45
|
-
|
|
47
|
+
encoding.js is a JavaScript library for converting and detecting character encodings,
|
|
48
|
+
supporting both Japanese character encodings (`Shift_JIS`, `EUC-JP`, `ISO-2022-JP`) and Unicode formats (`UTF-8`, `UTF-16`).
|
|
46
49
|
|
|
47
|
-
Since JavaScript string values are internally encoded as UTF-16 code units
|
|
48
|
-
|
|
50
|
+
Since JavaScript string values are internally encoded as UTF-16 code units
|
|
51
|
+
([ref: ECMAScript® 2019 Language Specification - 6.1.4 The String Type](https://www.ecma-international.org/ecma-262/10.0/index.html#sec-ecmascript-language-types-string-type)),
|
|
52
|
+
they cannot directly handle other character encodings as strings. However, encoding.js overcomes this limitation by treating these encodings as arrays instead of strings,
|
|
53
|
+
enabling the conversion between different character sets.
|
|
49
54
|
|
|
50
|
-
Each character encoding is
|
|
55
|
+
Each character encoding is represented as an array of numbers corresponding to character code values, for example, `[130, 160]` represents "あ" in UTF-8.
|
|
51
56
|
|
|
52
|
-
The array of character codes
|
|
57
|
+
The array of character codes used in its methods can also be utilized with TypedArray objects, such as `Uint8Array`, or with `Buffer` in Node.js.
|
|
53
58
|
|
|
54
|
-
### How to
|
|
59
|
+
### How to Use Character Encoding in Strings?
|
|
55
60
|
|
|
56
|
-
Numeric arrays of character codes can be converted to strings
|
|
57
|
-
|
|
61
|
+
Numeric arrays of character codes can be converted to strings using methods such as [`Encoding.codeToString`](#encodingcodetostring-code).
|
|
62
|
+
However, due to the JavaScript specifications mentioned above, some character encodings may not be handled properly when converted directly to strings.
|
|
58
63
|
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
If you prefer to use strings instead of numeric arrays, you can convert them to percent-encoded strings,
|
|
65
|
+
such as `'%82%A0'`, using [`Encoding.urlEncode`](#encodingurlencode-data) and [`Encoding.urlDecode`](#encodingurldecode-string) for passing to other resources.
|
|
66
|
+
Similarly, [`Encoding.base64Encode`](#encodingbase64encode-data) and [`Encoding.base64Decode`](#encodingbase64decode-string) allow for encoding and decoding to and from base64,
|
|
67
|
+
which can then be passed as strings.
|
|
61
68
|
|
|
62
69
|
## Installation
|
|
63
70
|
|
|
@@ -66,16 +73,16 @@ Or, [`Encoding.base64Encode`](#base64-encodedecode) and [`Encoding.base64Decode`
|
|
|
66
73
|
encoding.js is published under the package name `encoding-japanese` on npm.
|
|
67
74
|
|
|
68
75
|
```bash
|
|
69
|
-
|
|
76
|
+
npm install --save encoding-japanese
|
|
70
77
|
```
|
|
71
78
|
|
|
72
|
-
####
|
|
79
|
+
#### Using ES6 `import`
|
|
73
80
|
|
|
74
81
|
```javascript
|
|
75
82
|
import Encoding from 'encoding-japanese';
|
|
76
83
|
```
|
|
77
84
|
|
|
78
|
-
####
|
|
85
|
+
#### Using CommonJS `require`
|
|
79
86
|
|
|
80
87
|
```javascript
|
|
81
88
|
const Encoding = require('encoding-japanese');
|
|
@@ -83,60 +90,68 @@ const Encoding = require('encoding-japanese');
|
|
|
83
90
|
|
|
84
91
|
#### TypeScript
|
|
85
92
|
|
|
86
|
-
TypeScript type definitions for encoding.js are available at [@types/encoding-japanese](https://www.npmjs.com/package/@types/encoding-japanese) (thanks [@rhysd](https://github.com/rhysd)).
|
|
93
|
+
TypeScript type definitions for encoding.js are available at [@types/encoding-japanese](https://www.npmjs.com/package/@types/encoding-japanese) (thanks to [@rhysd](https://github.com/rhysd)).
|
|
87
94
|
|
|
88
95
|
```bash
|
|
89
|
-
|
|
96
|
+
npm install --save-dev @types/encoding-japanese
|
|
90
97
|
```
|
|
91
98
|
|
|
92
|
-
###
|
|
99
|
+
### Browser (standalone)
|
|
93
100
|
|
|
94
|
-
|
|
95
|
-
|
|
101
|
+
To use encoding.js in a browser environment, you can either install it via npm or download it directly from the [release list](https://github.com/polygonplanet/encoding.js/tags).
|
|
102
|
+
The package includes both `encoding.js` and `encoding.min.js`.
|
|
103
|
+
|
|
104
|
+
Note: Cloning the repository via `git clone` might give you access to the *master* (or *main*) branch, which could still be in a development state.
|
|
96
105
|
|
|
97
106
|
```html
|
|
107
|
+
<!-- To include the full version -->
|
|
98
108
|
<script src="encoding.js"></script>
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Or use the minified `encoding.min.js`
|
|
102
109
|
|
|
103
|
-
|
|
110
|
+
<!-- Or, to include the minified version for production -->
|
|
104
111
|
<script src="encoding.min.js"></script>
|
|
105
112
|
```
|
|
106
113
|
|
|
107
|
-
When the script is loaded, the object `Encoding` is defined in the global scope (
|
|
114
|
+
When the script is loaded, the object `Encoding` is defined in the global scope (i.e., `window.Encoding`).
|
|
108
115
|
|
|
109
116
|
### CDN
|
|
110
117
|
|
|
111
|
-
You can use
|
|
118
|
+
You can use encoding.js (package name: `encoding-japanese`) directly from a CDN via a script tag:
|
|
119
|
+
|
|
120
|
+
```html
|
|
121
|
+
<script src="https://unpkg.com/encoding-japanese@2.1.0/encoding.min.js"></script>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
In this example we use [unpkg](https://unpkg.com/encoding-japanese/), but you can use any CDN that provides npm packages,
|
|
125
|
+
for example [cdnjs](https://cdnjs.com/libraries/encoding-japanese) or [jsDelivr](https://www.jsdelivr.com/package/npm/encoding-japanese).
|
|
112
126
|
|
|
113
127
|
## Supported encodings
|
|
114
128
|
|
|
115
|
-
|Value in encoding.js|[`detect()`](#
|
|
129
|
+
|Value in encoding.js|[`detect()`](#encodingdetect-data-encodings)|[`convert()`](#encodingconvert-data-to-from)|MIME Name (Note)|
|
|
116
130
|
|:------:|:----:|:-----:|:---|
|
|
117
|
-
|ASCII |✓
|
|
118
|
-
|BINARY |✓
|
|
119
|
-
|EUCJP |✓
|
|
120
|
-
|JIS |✓
|
|
121
|
-
|SJIS |✓
|
|
122
|
-
|UTF8 |✓
|
|
123
|
-
|UTF16 |✓
|
|
124
|
-
|UTF16BE |✓
|
|
125
|
-
|UTF16LE |✓
|
|
126
|
-
|UTF32 |✓
|
|
127
|
-
|UNICODE |✓
|
|
131
|
+
|ASCII |✓ | |US-ASCII (Code point range: `0-127`)|
|
|
132
|
+
|BINARY |✓ | |(Binary string. Code point range: `0-255`)|
|
|
133
|
+
|EUCJP |✓ |✓ |EUC-JP|
|
|
134
|
+
|JIS |✓ |✓ |ISO-2022-JP|
|
|
135
|
+
|SJIS |✓ |✓ |Shift_JIS|
|
|
136
|
+
|UTF8 |✓ |✓ |UTF-8|
|
|
137
|
+
|UTF16 |✓ |✓ |UTF-16|
|
|
138
|
+
|UTF16BE |✓ |✓ |UTF-16BE (big-endian)|
|
|
139
|
+
|UTF16LE |✓ |✓ |UTF-16LE (little-endian)|
|
|
140
|
+
|UTF32 |✓ | |UTF-32|
|
|
141
|
+
|UNICODE |✓ |✓ |(JavaScript string. *See [About `UNICODE`](#about-unicode) below) |
|
|
128
142
|
|
|
129
143
|
### About `UNICODE`
|
|
130
144
|
|
|
131
|
-
In encoding.js, the internal character encoding that
|
|
145
|
+
In encoding.js, `UNICODE` is defined as the internal character encoding that JavaScript strings (JavaScript string objects) can handle directly.
|
|
132
146
|
|
|
133
|
-
As mentioned
|
|
134
|
-
|
|
147
|
+
As mentioned in the [Features](#features) section, JavaScript strings are internally encoded using UTF-16 code units.
|
|
148
|
+
This means that other character encodings cannot be directly handled without conversion.
|
|
149
|
+
Therefore, when converting to a character encoding that is properly representable in JavaScript, you should specify `UNICODE`.
|
|
135
150
|
|
|
136
|
-
(
|
|
151
|
+
(Note: Even if the HTML file's encoding is UTF-8, you should specify `UNICODE` instead of `UTF8` when processing the encoding in JavaScript.)
|
|
137
152
|
|
|
138
|
-
|
|
139
|
-
|
|
153
|
+
When using [`Encoding.convert`](#encodingconvert-data-to-from), if you specify a character encoding other than `UNICODE` (such as `UTF8` or `SJIS`), the values in the returned character code array will range from `0-255`.
|
|
154
|
+
However, if you specify `UNICODE`, the values will range from `0-65535`, which corresponds to the range of values returned by `String.prototype.charCodeAt()` (Code Units).
|
|
140
155
|
|
|
141
156
|
## Example usage
|
|
142
157
|
|
|
@@ -155,27 +170,27 @@ console.log(sjisArray);
|
|
|
155
170
|
Convert character encoding from `SJIS` to `UNICODE`.
|
|
156
171
|
|
|
157
172
|
```javascript
|
|
158
|
-
|
|
173
|
+
const sjisArray = [
|
|
159
174
|
130, 177, 130, 241, 130, 201, 130, 191, 130, 205
|
|
160
175
|
]; // 'こんにちは' array in SJIS
|
|
161
176
|
|
|
162
|
-
|
|
177
|
+
const unicodeArray = Encoding.convert(sjisArray, {
|
|
163
178
|
to: 'UNICODE',
|
|
164
179
|
from: 'SJIS'
|
|
165
180
|
});
|
|
166
|
-
|
|
181
|
+
const str = Encoding.codeToString(unicodeArray); // Convert code array to string
|
|
167
182
|
console.log(str); // 'こんにちは'
|
|
168
183
|
```
|
|
169
184
|
|
|
170
185
|
Detect character encoding.
|
|
171
186
|
|
|
172
187
|
```javascript
|
|
173
|
-
|
|
188
|
+
const data = [
|
|
174
189
|
227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175
|
|
175
190
|
]; // 'こんにちは' array in UTF-8
|
|
176
191
|
|
|
177
|
-
|
|
178
|
-
console.log(
|
|
192
|
+
const detectedEncoding = Encoding.detect(data);
|
|
193
|
+
console.log(`Character encoding is ${detectedEncoding}`); // 'Character encoding is UTF8'
|
|
179
194
|
```
|
|
180
195
|
|
|
181
196
|
(Node.js) Example of reading a text file written in `SJIS`.
|
|
@@ -194,90 +209,132 @@ console.log(Encoding.codeToString(unicodeArray));
|
|
|
194
209
|
|
|
195
210
|
## Demo
|
|
196
211
|
|
|
197
|
-
* [Test for character encoding conversion (Demo)](
|
|
198
|
-
* [Detect and Convert encoding from file (Demo)](
|
|
212
|
+
* [Test for character encoding conversion (Demo)](https://polygonplanet.github.io/encoding.js/tests/encoding-test.html)
|
|
213
|
+
* [Detect and Convert encoding from file (Demo)](https://polygonplanet.github.io/encoding.js/tests/detect-file-encoding.html)
|
|
199
214
|
|
|
200
215
|
----
|
|
201
216
|
|
|
202
217
|
## API
|
|
203
218
|
|
|
204
|
-
* [detect](#
|
|
205
|
-
* [convert](#
|
|
206
|
-
* [urlEncode
|
|
207
|
-
* [
|
|
208
|
-
* [
|
|
209
|
-
* [
|
|
219
|
+
* [detect](#encodingdetect-data-encodings)
|
|
220
|
+
* [convert](#encodingconvert-data-to-from)
|
|
221
|
+
* [urlEncode](#encodingurlencode-data)
|
|
222
|
+
* [urlDecode](#encodingurldecode-string)
|
|
223
|
+
* [base64Encode](#encodingbase64encode-data)
|
|
224
|
+
* [base64Decode](#encodingbase64decode-string)
|
|
225
|
+
* [codeToString](#encodingcodetostring-code)
|
|
226
|
+
* [stringToCode](#encodingstringtocode-string)
|
|
227
|
+
* [Japanese Zenkaku/Hankaku conversion](#japanese-zenkakuhankaku-conversion)
|
|
210
228
|
|
|
211
|
-
|
|
229
|
+
----
|
|
212
230
|
|
|
213
|
-
|
|
214
|
-
Detect character encoding.
|
|
215
|
-
@param {_Array|TypedArray|string_} _data_ Target data
|
|
216
|
-
@param {_string|Array_} [_encodings_] (Optional) The encoding name that to specify the detection (value of [Supported encodings](#supported-encodings))
|
|
217
|
-
@return {_string|boolean_} Return the detected character encoding, or false.
|
|
231
|
+
### Encoding.detect (data, [encodings])
|
|
218
232
|
|
|
219
|
-
|
|
233
|
+
Detects the character encoding of the given data.
|
|
234
|
+
|
|
235
|
+
#### Parameters
|
|
236
|
+
|
|
237
|
+
* **data** *(Array\<number\>|TypedArray|Buffer|string)* : The code array or string to detect character encoding.
|
|
238
|
+
* **\[encodings\]** *(string|Array\<string\>|Object)* : (Optional) Specifies a specific character encoding,
|
|
239
|
+
or an array of encodings to limit the detection. Detects automatically if this argument is omitted or `AUTO` is specified.
|
|
240
|
+
Supported encoding values can be found in the "[Supported encodings](#supported-encodings)" section.
|
|
241
|
+
|
|
242
|
+
#### Return value
|
|
243
|
+
|
|
244
|
+
*(string|boolean)*: Returns a string representing the detected encoding (e.g., `SJIS`, `UTF8`) listed in the "[Supported encodings](#supported-encodings)" section, or `false` if the encoding cannot be detected.
|
|
245
|
+
If the `encodings` argument is provided, it returns the name of the detected encoding if the `data` matches any of the specified encodings, or `false` otherwise.
|
|
246
|
+
|
|
247
|
+
#### Examples
|
|
248
|
+
|
|
249
|
+
Example of detecting character encoding.
|
|
220
250
|
|
|
221
251
|
```javascript
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
console.log(
|
|
252
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
253
|
+
const detectedEncoding = Encoding.detect(sjisArray);
|
|
254
|
+
console.log(`Encoding is ${detectedEncoding}`); // 'Encoding is SJIS'
|
|
225
255
|
```
|
|
226
256
|
|
|
227
|
-
Example of
|
|
228
|
-
|
|
257
|
+
Example of using the `encodings` argument to specify the character encoding to be detected.
|
|
258
|
+
This returns a string detected encoding if the specified encoding matches, or `false` otherwise:
|
|
229
259
|
|
|
230
260
|
```javascript
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (
|
|
261
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
262
|
+
const detectedEncoding = Encoding.detect(sjisArray, 'SJIS');
|
|
263
|
+
if (detectedEncoding) {
|
|
234
264
|
console.log('Encoding is SJIS');
|
|
265
|
+
} else {
|
|
266
|
+
console.log('Encoding does not match SJIS');
|
|
235
267
|
}
|
|
236
268
|
```
|
|
237
269
|
|
|
238
|
-
|
|
270
|
+
Example of specifying multiple encodings:
|
|
239
271
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
272
|
+
```javascript
|
|
273
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
274
|
+
const detectedEncoding = Encoding.detect(sjisArray, ['UTF8', 'SJIS']);
|
|
275
|
+
if (detectedEncoding) {
|
|
276
|
+
console.log(`Encoding is ${detectedEncoding}`); // 'Encoding is SJIS'
|
|
277
|
+
} else {
|
|
278
|
+
console.log('Encoding does not match UTF8 and SJIS');
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
----
|
|
283
|
+
|
|
284
|
+
### Encoding.convert (data, to[, from])
|
|
285
|
+
|
|
286
|
+
Converts the character encoding of the given data.
|
|
287
|
+
|
|
288
|
+
#### Parameters
|
|
289
|
+
|
|
290
|
+
* **data** *(Array\<number\>|TypedArray|Buffer|string)* : The code array or string to convert character encoding.
|
|
291
|
+
* **to** *(string|Object)* : The character encoding name of the conversion destination as a string, or conversion options as an object.
|
|
292
|
+
* **\[from\]** *(string|Array\<string\>)* : (Optional) The character encoding name of the conversion source as a string,
|
|
293
|
+
or an array of encoding names. Detects automatically if this argument is omitted or `AUTO` is specified.
|
|
294
|
+
Supported encoding values can be found in the "[Supported encodings](#supported-encodings)" section.
|
|
295
|
+
|
|
296
|
+
#### Return value
|
|
246
297
|
|
|
247
|
-
|
|
298
|
+
*(Array\<number\>|TypedArray|string)* : Returns a numeric character code array of the converted character encoding if `data` is an array or a buffer,
|
|
299
|
+
or returns the converted string if `data` is a string.
|
|
300
|
+
|
|
301
|
+
#### Examples
|
|
302
|
+
|
|
303
|
+
Example of converting a character code array to Shift_JIS from UTF-8:
|
|
248
304
|
|
|
249
305
|
```javascript
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
console.log(sjisArray); // [130, 160] (
|
|
306
|
+
const utf8Array = [227, 129, 130]; // 'あ' in UTF-8
|
|
307
|
+
const sjisArray = Encoding.convert(utf8Array, 'SJIS', 'UTF8');
|
|
308
|
+
console.log(sjisArray); // [130, 160] ('あ' in SJIS)
|
|
253
309
|
```
|
|
254
310
|
|
|
255
|
-
TypedArray such as `Uint8Array`, and `Buffer` of Node.js can be converted in the same usage
|
|
311
|
+
TypedArray such as `Uint8Array`, and `Buffer` of Node.js can be converted in the same usage:
|
|
256
312
|
|
|
257
313
|
```javascript
|
|
258
|
-
|
|
259
|
-
Encoding.convert(utf8Array, 'SJIS', 'UTF8');
|
|
314
|
+
const utf8Array = new Uint8Array([227, 129, 130]);
|
|
315
|
+
const sjisArray = Encoding.convert(utf8Array, 'SJIS', 'UTF8');
|
|
260
316
|
```
|
|
261
317
|
|
|
262
|
-
Converts character encoding by auto-detecting the encoding name of the source
|
|
318
|
+
Converts character encoding by auto-detecting the encoding name of the source:
|
|
263
319
|
|
|
264
320
|
```javascript
|
|
265
|
-
// The character encoding is automatically detected when the
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
321
|
+
// The character encoding is automatically detected when the argument `from` is omitted
|
|
322
|
+
const utf8Array = [227, 129, 130];
|
|
323
|
+
let sjisArray = Encoding.convert(utf8Array, 'SJIS');
|
|
269
324
|
// Or explicitly specify 'AUTO' to auto-detecting
|
|
270
325
|
sjisArray = Encoding.convert(utf8Array, 'SJIS', 'AUTO');
|
|
271
326
|
```
|
|
272
327
|
|
|
273
|
-
#### Specify conversion options to the argument `
|
|
328
|
+
#### Specify conversion options to the argument `to` as an object
|
|
274
329
|
|
|
275
|
-
You can
|
|
330
|
+
You can pass the second argument `to` as an object for improving readability.
|
|
331
|
+
Also, the following options such as `type`, `fallback`, and `bom` must be specified with an object.
|
|
276
332
|
|
|
277
333
|
```javascript
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
334
|
+
const utf8Array = [227, 129, 130];
|
|
335
|
+
const sjisArray = Encoding.convert(utf8Array, {
|
|
336
|
+
to: 'SJIS',
|
|
337
|
+
from: 'UTF8'
|
|
281
338
|
});
|
|
282
339
|
```
|
|
283
340
|
|
|
@@ -287,8 +344,8 @@ var sjisArray = Encoding.convert(utf8Array, {
|
|
|
287
344
|
Also, if the argument `data` is passed as a string and the` type` option is not specified, then `type` ='string' is assumed (returns as a string).
|
|
288
345
|
|
|
289
346
|
```javascript
|
|
290
|
-
|
|
291
|
-
|
|
347
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
348
|
+
const unicodeString = Encoding.convert(sjisArray, {
|
|
292
349
|
to: 'UNICODE',
|
|
293
350
|
from: 'SJIS',
|
|
294
351
|
type: 'string' // Specify 'string' to return as string
|
|
@@ -296,27 +353,32 @@ var unicodeString = Encoding.convert(sjisArray, {
|
|
|
296
353
|
console.log(unicodeString); // 'おはよ'
|
|
297
354
|
```
|
|
298
355
|
|
|
299
|
-
The following `type` options are supported
|
|
356
|
+
The following `type` options are supported.
|
|
357
|
+
|
|
358
|
+
* **string** : Return as a string.
|
|
359
|
+
* **arraybuffer** : Return as an ArrayBuffer (Actually returns a `Uint16Array` due to historical reasons).
|
|
360
|
+
* **array** : Return as an Array. (*default*)
|
|
300
361
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
362
|
+
`type: 'string'` can be used as a shorthand for converting a code array to a string,
|
|
363
|
+
as performed by [`Encoding.codeToString`](#encodingcodetostring-code).
|
|
364
|
+
Note: Specifying `type: 'string'` may not handle conversions properly, except when converting to `UNICODE`.
|
|
304
365
|
|
|
305
|
-
####
|
|
366
|
+
#### Replacing characters with HTML entities when they cannot be represented
|
|
306
367
|
|
|
307
|
-
Characters that cannot be represented in the target character set are replaced with '?' (U+003F) by default
|
|
368
|
+
Characters that cannot be represented in the target character set are replaced with '?' (U+003F) by default,
|
|
369
|
+
but by specifying the `fallback` option, you can replace them with HTML entities (Numeric character references), such as `🍣`.
|
|
308
370
|
|
|
309
371
|
The `fallback` option supports the following values.
|
|
310
372
|
|
|
311
|
-
* **html-entity** : Replace to HTML entity (decimal HTML numeric character reference)
|
|
312
|
-
* **html-entity-hex** : Replace to HTML entity (hexadecimal HTML numeric character reference)
|
|
373
|
+
* **html-entity** : Replace to HTML entity (decimal HTML numeric character reference).
|
|
374
|
+
* **html-entity-hex** : Replace to HTML entity (hexadecimal HTML numeric character reference).
|
|
313
375
|
|
|
314
|
-
Example of specifying `{ fallback: 'html-entity' }` option
|
|
376
|
+
Example of specifying `{ fallback: 'html-entity' }` option:
|
|
315
377
|
|
|
316
378
|
```javascript
|
|
317
|
-
|
|
379
|
+
const unicodeArray = Encoding.stringToCode('寿司🍣ビール🍺');
|
|
318
380
|
// No fallback specified
|
|
319
|
-
|
|
381
|
+
let sjisArray = Encoding.convert(unicodeArray, {
|
|
320
382
|
to: 'SJIS',
|
|
321
383
|
from: 'UNICODE'
|
|
322
384
|
});
|
|
@@ -331,11 +393,11 @@ sjisArray = Encoding.convert(unicodeArray, {
|
|
|
331
393
|
console.log(sjisArray); // Converted to a code array of '寿司🍣ビール🍺'
|
|
332
394
|
```
|
|
333
395
|
|
|
334
|
-
Example of specifying `{ fallback: 'html-entity-hex' }` option
|
|
396
|
+
Example of specifying `{ fallback: 'html-entity-hex' }` option:
|
|
335
397
|
|
|
336
398
|
```javascript
|
|
337
|
-
|
|
338
|
-
|
|
399
|
+
const unicodeArray = Encoding.stringToCode('ホッケの漢字は𩸽');
|
|
400
|
+
const sjisArray = Encoding.convert(unicodeArray, {
|
|
339
401
|
to: 'SJIS',
|
|
340
402
|
from: 'UNICODE',
|
|
341
403
|
fallback: 'html-entity-hex'
|
|
@@ -349,7 +411,7 @@ You can add a BOM (byte order mark) by specifying the `bom` option when converti
|
|
|
349
411
|
The default is no BOM.
|
|
350
412
|
|
|
351
413
|
```javascript
|
|
352
|
-
|
|
414
|
+
const utf16Array = Encoding.convert(utf8Array, {
|
|
353
415
|
to: 'UTF16', // to_encoding
|
|
354
416
|
from: 'UTF8', // from_encoding
|
|
355
417
|
bom: true // Add BOM
|
|
@@ -360,7 +422,7 @@ var utf16Array = Encoding.convert(utf8Array, {
|
|
|
360
422
|
If you want to convert as little-endian, specify the `{ bom: 'LE' }` option.
|
|
361
423
|
|
|
362
424
|
```javascript
|
|
363
|
-
|
|
425
|
+
const utf16leArray = Encoding.convert(utf8Array, {
|
|
364
426
|
to: 'UTF16', // to_encoding
|
|
365
427
|
from: 'UTF8', // from_encoding
|
|
366
428
|
bom: 'LE' // With BOM (little-endian)
|
|
@@ -371,158 +433,335 @@ If you do not need BOM, use `UTF16BE` or `UTF16LE`.
|
|
|
371
433
|
`UTF16BE` is big-endian, and `UTF16LE` is little-endian, and both have no BOM.
|
|
372
434
|
|
|
373
435
|
```javascript
|
|
374
|
-
|
|
436
|
+
const utf16beArray = Encoding.convert(utf8Array, {
|
|
375
437
|
to: 'UTF16BE',
|
|
376
438
|
from: 'UTF8'
|
|
377
439
|
});
|
|
378
440
|
```
|
|
379
441
|
|
|
380
|
-
|
|
442
|
+
----
|
|
443
|
+
|
|
444
|
+
### Encoding.urlEncode (data)
|
|
445
|
+
|
|
446
|
+
Encodes a numeric character code array into a percent-encoded string formatted as a URI component in `%xx` format.
|
|
447
|
+
|
|
448
|
+
urlEncode escapes all characters except the following, just like [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent).
|
|
449
|
+
|
|
450
|
+
```
|
|
451
|
+
A-Z a-z 0-9 - _ . ! ~ * ' ( )
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
#### Parameters
|
|
455
|
+
|
|
456
|
+
* **data** *(Array\<number\>|TypedArray|Buffer|string)* : The numeric character code array or string that will be encoded into a percent-encoded URI component.
|
|
457
|
+
|
|
458
|
+
#### Return value
|
|
381
459
|
|
|
382
|
-
*
|
|
383
|
-
URL(percent) encode.
|
|
384
|
-
@param {_Array_|_TypedArray_} _data_ Target data.
|
|
385
|
-
@return {_string_} Return the encoded string.
|
|
460
|
+
*(string)* : Returns a percent-encoded string formatted as a URI component in `%xx` format.
|
|
386
461
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
@return {_Array_} Return the decoded array.
|
|
462
|
+
#### Examples
|
|
463
|
+
|
|
464
|
+
Example of URL encoding a Shift_JIS array:
|
|
391
465
|
|
|
392
466
|
```javascript
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
console.log(encoded); // '%82%
|
|
467
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
468
|
+
const encoded = Encoding.urlEncode(sjisArray);
|
|
469
|
+
console.log(encoded); // '%82%A8%82%CD%82%E6'
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
----
|
|
473
|
+
|
|
474
|
+
### Encoding.urlDecode (string)
|
|
475
|
+
|
|
476
|
+
Decodes a percent-encoded string formatted as a URI component in `%xx` format to a numeric character code array.
|
|
477
|
+
|
|
478
|
+
#### Parameters
|
|
479
|
+
|
|
480
|
+
* **string** *(string)* : The string to decode.
|
|
396
481
|
|
|
397
|
-
|
|
398
|
-
|
|
482
|
+
#### Return value
|
|
483
|
+
|
|
484
|
+
*(Array\<number\>)* : Returns a numeric character code array.
|
|
485
|
+
|
|
486
|
+
#### Examples
|
|
487
|
+
|
|
488
|
+
Example of decoding a percent-encoded Shift_JIS string:
|
|
489
|
+
|
|
490
|
+
```javascript
|
|
491
|
+
const encoded = '%82%A8%82%CD%82%E6'; // 'おはよ' encoded as percent-encoded SJIS string
|
|
492
|
+
const sjisArray = Encoding.urlDecode(encoded);
|
|
493
|
+
console.log(sjisArray); // [130, 168, 130, 205, 130, 230]
|
|
399
494
|
```
|
|
400
495
|
|
|
401
|
-
|
|
496
|
+
----
|
|
497
|
+
|
|
498
|
+
### Encoding.base64Encode (data)
|
|
499
|
+
|
|
500
|
+
Encodes a numeric character code array into a Base64 encoded string.
|
|
402
501
|
|
|
403
|
-
|
|
404
|
-
Base64 encode.
|
|
405
|
-
@param {_Array_|_TypedArray_} _data_ Target data.
|
|
406
|
-
@return {_string_} Return the Base64 encoded string.
|
|
502
|
+
#### Parameters
|
|
407
503
|
|
|
408
|
-
*
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
504
|
+
* **data** *(Array\<number\>|TypedArray|Buffer|string)* : The numeric character code array or string to encode.
|
|
505
|
+
|
|
506
|
+
#### Return value
|
|
507
|
+
|
|
508
|
+
*(string)* : Returns a Base64 encoded string.
|
|
509
|
+
|
|
510
|
+
#### Examples
|
|
511
|
+
|
|
512
|
+
Example of Base64 encoding a Shift_JIS array:
|
|
412
513
|
|
|
413
514
|
```javascript
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
console.log(
|
|
515
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
516
|
+
const encodedStr = Encoding.base64Encode(sjisArray);
|
|
517
|
+
console.log(encodedStr); // 'gqiCzYLm'
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
----
|
|
521
|
+
|
|
522
|
+
### Encoding.base64Decode (string)
|
|
417
523
|
|
|
418
|
-
|
|
419
|
-
|
|
524
|
+
Decodes a Base64 encoded string to a numeric character code array.
|
|
525
|
+
|
|
526
|
+
#### Parameters
|
|
527
|
+
|
|
528
|
+
* **string** *(string)* : The Base64 encoded string to decode.
|
|
529
|
+
|
|
530
|
+
#### Return value
|
|
531
|
+
|
|
532
|
+
*(Array\<number\>)* : Returns a Base64 decoded numeric character code array.
|
|
533
|
+
|
|
534
|
+
#### Examples
|
|
535
|
+
|
|
536
|
+
Example of `base64Encode` and `base64Decode`:
|
|
537
|
+
|
|
538
|
+
```javascript
|
|
539
|
+
const sjisArray = [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]; // 'こんにちは' array in SJIS
|
|
540
|
+
const encodedStr = Encoding.base64Encode(sjisArray);
|
|
541
|
+
console.log(encodedStr); // 'grGC8YLJgr+CzQ=='
|
|
542
|
+
|
|
543
|
+
const decodedArray = Encoding.base64Decode(encodedStr);
|
|
544
|
+
console.log(decodedArray); // [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]
|
|
420
545
|
```
|
|
421
546
|
|
|
422
|
-
|
|
547
|
+
----
|
|
548
|
+
|
|
549
|
+
### Encoding.codeToString (code)
|
|
550
|
+
|
|
551
|
+
Converts a numeric character code array to string.
|
|
552
|
+
|
|
553
|
+
#### Parameters
|
|
423
554
|
|
|
424
|
-
*
|
|
425
|
-
Joins a character code array to string.
|
|
555
|
+
* **code** *(Array\<number\>|TypedArray|Buffer)* : The numeric character code array to convert.
|
|
426
556
|
|
|
427
|
-
|
|
428
|
-
|
|
557
|
+
#### Return value
|
|
558
|
+
|
|
559
|
+
*(string)* : Returns a converted string.
|
|
560
|
+
|
|
561
|
+
#### Examples
|
|
562
|
+
|
|
563
|
+
Example of converting a character code array to a string:
|
|
564
|
+
|
|
565
|
+
```javascript
|
|
566
|
+
const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS
|
|
567
|
+
const unicodeArray = Encoding.convert(sjisArray, {
|
|
568
|
+
to: 'UNICODE',
|
|
569
|
+
from: 'SJIS'
|
|
570
|
+
});
|
|
571
|
+
const unicodeStr = Encoding.codeToString(unicodeArray);
|
|
572
|
+
console.log(unicodeStr); // 'おはよ'
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
----
|
|
576
|
+
|
|
577
|
+
### Encoding.stringToCode (string)
|
|
578
|
+
|
|
579
|
+
Converts a string to a numeric character code array.
|
|
580
|
+
|
|
581
|
+
#### Parameters
|
|
582
|
+
|
|
583
|
+
* **string** *(string)* : The string to convert.
|
|
584
|
+
|
|
585
|
+
#### Return value
|
|
586
|
+
|
|
587
|
+
*(Array\<number\>)* : Returns a numeric character code array converted from the string.
|
|
588
|
+
|
|
589
|
+
#### Examples
|
|
590
|
+
|
|
591
|
+
Example of converting a string to a character code array:
|
|
592
|
+
|
|
593
|
+
```javascript
|
|
594
|
+
const unicodeArray = Encoding.stringToCode('おはよ');
|
|
595
|
+
console.log(unicodeArray); // [12362, 12399, 12424]
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
----
|
|
429
599
|
|
|
430
600
|
### Japanese Zenkaku/Hankaku conversion
|
|
431
601
|
|
|
432
|
-
|
|
433
|
-
|
|
602
|
+
The following methods convert Japanese full-width (zenkaku) and half-width (hankaku) characters,
|
|
603
|
+
suitable for use with `UNICODE` strings or numeric character code arrays of `UNICODE`.
|
|
604
|
+
|
|
605
|
+
Returns a converted string if the argument `data` is a string.
|
|
606
|
+
Returns a numeric character code array if the argument `data` is a code array.
|
|
607
|
+
|
|
608
|
+
- **Encoding.toHankakuCase (data)** : Converts full-width (zenkaku) symbols and alphanumeric characters to their half-width (hankaku) equivalents.
|
|
609
|
+
- **Encoding.toZenkakuCase (data)** : Converts half-width (hankaku) symbols and alphanumeric characters to their full-width (zenkaku) equivalents.
|
|
610
|
+
- **Encoding.toHiraganaCase (data)** : Converts full-width katakana to full-width hiragana.
|
|
611
|
+
- **Encoding.toKatakanaCase (data)** : Converts full-width hiragana to full-width katakana.
|
|
612
|
+
- **Encoding.toHankanaCase (data)** : Converts full-width katakana to half-width katakana.
|
|
613
|
+
- **Encoding.toZenkanaCase (data)** : Converts half-width katakana to full-width katakana.
|
|
614
|
+
- **Encoding.toHankakuSpace (data)** : Converts the em space (U+3000) to the single space (U+0020).
|
|
615
|
+
- **Encoding.toZenkakuSpace (data)** : Converts the single space (U+0020) to the em space (U+3000).
|
|
616
|
+
|
|
617
|
+
#### Parameters
|
|
618
|
+
|
|
619
|
+
- **data** *(Array\<number\>|TypedArray|Buffer|string)* : The string or numeric character code array to convert.
|
|
434
620
|
|
|
435
|
-
|
|
436
|
-
Convert to the zenkaku symbols and alphanumeric characters from the ascii symbols and alphanumeric characters.
|
|
621
|
+
#### Return value
|
|
437
622
|
|
|
438
|
-
*
|
|
439
|
-
Convert to the zenkaku hiragana from the zenkaku katakana.
|
|
623
|
+
*(Array\<number\>|string)* : Returns a converted string or numeric character code array.
|
|
440
624
|
|
|
441
|
-
|
|
442
|
-
Convert to the zenkaku katakana from the zenkaku hiragana.
|
|
625
|
+
#### Examples
|
|
443
626
|
|
|
444
|
-
|
|
445
|
-
Convert to the hankaku katakana from the zenkaku katakana.
|
|
627
|
+
Example of converting zenkaku and hankaku strings:
|
|
446
628
|
|
|
447
|
-
|
|
448
|
-
|
|
629
|
+
```javascript
|
|
630
|
+
console.log(Encoding.toHankakuCase('abcDEF123@!#*=')); // 'abcDEF123@!#*='
|
|
631
|
+
console.log(Encoding.toZenkakuCase('abcDEF123@!#*=')); // 'abcDEF123@!#*='
|
|
632
|
+
console.log(Encoding.toHiraganaCase('アイウエオァィゥェォヴボポ')); // 'あいうえおぁぃぅぇぉゔぼぽ'
|
|
633
|
+
console.log(Encoding.toKatakanaCase('あいうえおぁぃぅぇぉゔぼぽ')); // 'アイウエオァィゥェォヴボポ'
|
|
634
|
+
console.log(Encoding.toHankanaCase('アイウエオァィゥェォヴボポ')); // 'アイウエオァィゥェォヴボポ'
|
|
635
|
+
console.log(Encoding.toZenkanaCase('アイウエオァィゥェォヴボポ')); // 'アイウエオァィゥェォヴボポ'
|
|
636
|
+
console.log(Encoding.toHankakuSpace('あいうえお abc 123')); // 'あいうえお abc 123'
|
|
637
|
+
console.log(Encoding.toZenkakuSpace('あいうえお abc 123')); // 'あいうえお abc 123'
|
|
638
|
+
```
|
|
449
639
|
|
|
450
|
-
|
|
451
|
-
|
|
640
|
+
Example of converting zenkaku and hankaku code arrays:
|
|
641
|
+
|
|
642
|
+
```javascript
|
|
643
|
+
const unicodeArray = Encoding.stringToCode('abc123!# あいうアイウ ABCアイウ');
|
|
644
|
+
console.log(Encoding.codeToString(Encoding.toHankakuCase(unicodeArray)));
|
|
645
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
646
|
+
console.log(Encoding.codeToString(Encoding.toZenkakuCase(unicodeArray)));
|
|
647
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
648
|
+
console.log(Encoding.codeToString(Encoding.toHiraganaCase(unicodeArray)));
|
|
649
|
+
// 'abc123!# あいうあいう ABCアイウ'
|
|
650
|
+
console.log(Encoding.codeToString(Encoding.toKatakanaCase(unicodeArray)));
|
|
651
|
+
// 'abc123!# アイウアイウ ABCアイウ'
|
|
652
|
+
console.log(Encoding.codeToString(Encoding.toHankanaCase(unicodeArray)));
|
|
653
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
654
|
+
console.log(Encoding.codeToString(Encoding.toZenkanaCase(unicodeArray)));
|
|
655
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
656
|
+
console.log(Encoding.codeToString(Encoding.toHankakuSpace(unicodeArray)));
|
|
657
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
658
|
+
console.log(Encoding.codeToString(Encoding.toZenkakuSpace(unicodeArray)));
|
|
659
|
+
// 'abc123!# あいうアイウ ABCアイウ'
|
|
660
|
+
```
|
|
452
661
|
|
|
453
|
-
|
|
454
|
-
Convert the single space(U+0020) to the em space(U+3000).
|
|
662
|
+
----
|
|
455
663
|
|
|
456
664
|
## Other examples
|
|
457
665
|
|
|
458
|
-
### Example using the
|
|
666
|
+
### Example using the `Fetch API` and Typed Arrays (Uint8Array)
|
|
667
|
+
|
|
668
|
+
This example reads a text file encoded in Shift_JIS as binary data,
|
|
669
|
+
and displays it as a string after converting it to Unicode using [Encoding.convert](#encodingconvert-data-to-from).
|
|
670
|
+
|
|
671
|
+
```javascript
|
|
672
|
+
(async () => {
|
|
673
|
+
try {
|
|
674
|
+
const response = await fetch('shift_jis.txt');
|
|
675
|
+
const buffer = await response.arrayBuffer();
|
|
676
|
+
|
|
677
|
+
// Code array with Shift_JIS file contents
|
|
678
|
+
const sjisArray = new Uint8Array(buffer);
|
|
679
|
+
|
|
680
|
+
// Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS
|
|
681
|
+
const unicodeArray = Encoding.convert(sjisArray, {
|
|
682
|
+
to: 'UNICODE',
|
|
683
|
+
from: 'SJIS'
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
// Convert to string from code array for display
|
|
687
|
+
const unicodeString = Encoding.codeToString(unicodeArray);
|
|
688
|
+
console.log(unicodeString);
|
|
689
|
+
} catch (error) {
|
|
690
|
+
console.error('Error loading the file:', error);
|
|
691
|
+
}
|
|
692
|
+
})();
|
|
693
|
+
```
|
|
459
694
|
|
|
460
|
-
|
|
461
|
-
|
|
695
|
+
<details>
|
|
696
|
+
<summary>XMLHttpRequest version of this example</summary>
|
|
462
697
|
|
|
463
698
|
```javascript
|
|
464
|
-
|
|
465
|
-
req.open('GET', '
|
|
699
|
+
const req = new XMLHttpRequest();
|
|
700
|
+
req.open('GET', 'shift_jis.txt', true);
|
|
466
701
|
req.responseType = 'arraybuffer';
|
|
467
702
|
|
|
468
|
-
req.onload =
|
|
469
|
-
|
|
703
|
+
req.onload = (event) => {
|
|
704
|
+
const buffer = req.response;
|
|
470
705
|
if (buffer) {
|
|
471
|
-
// Shift_JIS
|
|
472
|
-
|
|
706
|
+
// Code array with Shift_JIS file contents
|
|
707
|
+
const sjisArray = new Uint8Array(buffer);
|
|
473
708
|
|
|
474
|
-
// Convert encoding to UNICODE (JavaScript
|
|
475
|
-
|
|
709
|
+
// Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS
|
|
710
|
+
const unicodeArray = Encoding.convert(sjisArray, {
|
|
476
711
|
to: 'UNICODE',
|
|
477
712
|
from: 'SJIS'
|
|
478
713
|
});
|
|
479
714
|
|
|
480
|
-
//
|
|
481
|
-
|
|
715
|
+
// Convert to string from code array for display
|
|
716
|
+
const unicodeString = Encoding.codeToString(unicodeArray);
|
|
482
717
|
console.log(unicodeString);
|
|
483
718
|
}
|
|
484
719
|
};
|
|
485
720
|
|
|
486
721
|
req.send(null);
|
|
487
722
|
```
|
|
723
|
+
</details>
|
|
488
724
|
|
|
489
725
|
### Convert encoding for file using the File APIs
|
|
490
726
|
|
|
491
|
-
|
|
492
|
-
|
|
727
|
+
This example uses the File API to read the content of a selected file, detects its character encoding,
|
|
728
|
+
and converts the file content to UNICODE from any character encoding such as `Shift_JIS` or `EUC-JP`.
|
|
729
|
+
The converted content is then displayed in a textarea.
|
|
493
730
|
|
|
494
731
|
```html
|
|
495
732
|
<input type="file" id="file">
|
|
496
733
|
<div id="encoding"></div>
|
|
497
|
-
<textarea id="
|
|
734
|
+
<textarea id="content" rows="5" cols="80"></textarea>
|
|
498
735
|
|
|
499
736
|
<script>
|
|
500
737
|
function onFileSelect(event) {
|
|
501
|
-
|
|
738
|
+
const file = event.target.files[0];
|
|
502
739
|
|
|
503
|
-
|
|
740
|
+
const reader = new FileReader();
|
|
504
741
|
reader.onload = function(e) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
742
|
+
const codes = new Uint8Array(e.target.result);
|
|
743
|
+
|
|
744
|
+
const detectedEncoding = Encoding.detect(codes);
|
|
745
|
+
const encoding = document.getElementById('encoding');
|
|
746
|
+
encoding.textContent = `Detected encoding: ${detectedEncoding}`;
|
|
747
|
+
|
|
748
|
+
// Convert encoding to UNICODE
|
|
749
|
+
const unicodeString = Encoding.convert(codes, {
|
|
750
|
+
to: 'UNICODE',
|
|
751
|
+
from: detectedEncoding,
|
|
513
752
|
type: 'string'
|
|
514
753
|
});
|
|
515
|
-
document.getElementById('
|
|
754
|
+
document.getElementById('content').value = unicodeString;
|
|
516
755
|
};
|
|
517
756
|
|
|
518
757
|
reader.readAsArrayBuffer(file);
|
|
519
758
|
}
|
|
520
759
|
|
|
521
|
-
document.getElementById('file').addEventListener('change', onFileSelect
|
|
760
|
+
document.getElementById('file').addEventListener('change', onFileSelect);
|
|
522
761
|
</script>
|
|
523
762
|
```
|
|
524
763
|
|
|
525
|
-
[**Demo**](
|
|
764
|
+
[**Demo**](https://polygonplanet.github.io/encoding.js/tests/detect-file-encoding.html)
|
|
526
765
|
|
|
527
766
|
## Contributing
|
|
528
767
|
|
|
@@ -531,12 +770,10 @@ For bug reports and feature requests, please [create an issue on GitHub](https:/
|
|
|
531
770
|
|
|
532
771
|
### Pull requests
|
|
533
772
|
|
|
534
|
-
|
|
535
|
-
We only accept requests
|
|
773
|
+
Before submitting a pull request, please run `npm run test` to ensure there are no errors.
|
|
774
|
+
We only accept pull requests that pass all tests.
|
|
536
775
|
|
|
537
776
|
## License
|
|
538
777
|
|
|
539
|
-
MIT
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
778
|
+
This project is licensed under the terms of the MIT license.
|
|
779
|
+
See the [LICENSE](LICENSE) file for details.
|