@exodus/bytes 1.0.0 → 1.1.0
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/README.md +7 -14
- package/fallback/encoding.js +3 -3
- package/fallback/encoding.labels.js +10 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -205,16 +205,10 @@ as an ASCII-lowercased string.
|
|
|
205
205
|
If an encoding with that label does not exist, returns `null`.
|
|
206
206
|
|
|
207
207
|
This is the same as [`decoder.encoding` getter](https://encoding.spec.whatwg.org/#dom-textdecoder-encoding),
|
|
208
|
-
except that it
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (!label) return null // does not default to 'utf-8'
|
|
213
|
-
return new TextDecoder(label).encoding
|
|
214
|
-
} catch {
|
|
215
|
-
return null
|
|
216
|
-
}
|
|
217
|
-
```
|
|
208
|
+
except that it:
|
|
209
|
+
1. Supports [`replacement` encoding](https://encoding.spec.whatwg.org/#replacement) and its
|
|
210
|
+
[labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)
|
|
211
|
+
2. Does not throw for invalid labels and instead returns `null`
|
|
218
212
|
|
|
219
213
|
All encoding names are also valid labels for corresponding encodings.
|
|
220
214
|
|
|
@@ -233,15 +227,14 @@ Given a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:
|
|
|
233
227
|
Implements [decode](https://encoding.spec.whatwg.org/#decode) legacy hook.
|
|
234
228
|
|
|
235
229
|
Given a `TypedArray` or an `ArrayBuffer` instance `input` and an optional `fallbackEncoding`
|
|
236
|
-
|
|
230
|
+
encoding [label](https://encoding.spec.whatwg.org/#names-and-labels),
|
|
231
|
+
sniffs encoding from BOM with `fallbackEncoding` fallback and then
|
|
237
232
|
decodes the `input` using that encoding, skipping BOM if it was present.
|
|
238
233
|
|
|
239
234
|
Notes:
|
|
240
235
|
|
|
241
236
|
* BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.
|
|
242
237
|
Use with care.
|
|
243
|
-
* `fallbackEncoding` must be ASCII-lowercased encoding name,
|
|
244
|
-
e.g. a result of `normalizeEncoding(label)` call.
|
|
245
238
|
* Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),
|
|
246
239
|
aka replacement. It can convert different byte sequences to equal strings.
|
|
247
240
|
|
|
@@ -249,7 +242,7 @@ This method is similar to the following code, except that it doesn't support enc
|
|
|
249
242
|
only expects lowercased encoding name:
|
|
250
243
|
|
|
251
244
|
```js
|
|
252
|
-
new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding
|
|
245
|
+
new TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)
|
|
253
246
|
```
|
|
254
247
|
|
|
255
248
|
### `@exodus/bytes/encoding-lite.js`
|
package/fallback/encoding.js
CHANGED
|
@@ -256,13 +256,13 @@ export function getBOMEncoding(input) {
|
|
|
256
256
|
// https://encoding.spec.whatwg.org/#decode
|
|
257
257
|
// Warning: encoding sniffed from BOM takes preference over the supplied one
|
|
258
258
|
// Warning: lossy, performs replacement, no option of throwing
|
|
259
|
-
//
|
|
259
|
+
// Completely ignores encoding and even skips validation when BOM is found
|
|
260
260
|
// Unlike TextDecoder public API, additionally supports 'replacement' encoding
|
|
261
|
-
export function legacyHookDecode(input, fallbackEncoding) {
|
|
261
|
+
export function legacyHookDecode(input, fallbackEncoding = 'utf-8') {
|
|
262
262
|
let u8 = fromSource(input)
|
|
263
263
|
const bomEncoding = getBOMEncoding(u8)
|
|
264
264
|
if (bomEncoding) u8 = u8.subarray(bomEncoding === 'utf-8' ? 3 : 2)
|
|
265
|
-
const enc = bomEncoding ?? fallbackEncoding
|
|
265
|
+
const enc = bomEncoding ?? normalizeEncoding(fallbackEncoding) // "the byte order mark is more authoritative than anything else"
|
|
266
266
|
|
|
267
267
|
if (enc === 'utf-8') return utf8toStringLoose(u8)
|
|
268
268
|
if (enc === 'utf-16le' || enc === 'utf-16be') {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
/* eslint-disable @exodus/export-default/named */
|
|
4
4
|
// prettier-ignore
|
|
5
|
-
|
|
5
|
+
const labels = {
|
|
6
6
|
'utf-8': ['unicode-1-1-utf-8', 'unicode11utf8', 'unicode20utf8', 'utf8', 'x-unicode20utf8'],
|
|
7
7
|
ibm866: ['866', 'cp866', 'csibm866'],
|
|
8
8
|
'iso-8859-2': ['csisolatin2', 'iso-ir-101', 'iso8859-2', 'iso88592', 'iso_8859-2', 'iso_8859-2:1987', 'l2', 'latin2'],
|
|
@@ -22,15 +22,6 @@ export default {
|
|
|
22
22
|
'koi8-u': ['koi8-ru'],
|
|
23
23
|
macintosh: ['csmacintosh', 'mac', 'x-mac-roman'],
|
|
24
24
|
'windows-874': ['dos-874', 'iso-8859-11', 'iso8859-11', 'iso885911', 'tis-620'],
|
|
25
|
-
'windows-1250': ['cp1250', 'x-cp1250'],
|
|
26
|
-
'windows-1251': ['cp1251', 'x-cp1251'],
|
|
27
|
-
'windows-1252': ['ansi_x3.4-1968', 'ascii', 'cp1252', 'cp819', 'csisolatin1', 'ibm819', 'iso-8859-1', 'iso-ir-100', 'iso8859-1', 'iso88591', 'iso_8859-1', 'iso_8859-1:1987', 'l1', 'latin1', 'us-ascii', 'x-cp1252'],
|
|
28
|
-
'windows-1253': ['cp1253', 'x-cp1253'],
|
|
29
|
-
'windows-1254': ['cp1254', 'csisolatin5', 'iso-8859-9', 'iso-ir-148', 'iso8859-9', 'iso88599', 'iso_8859-9', 'iso_8859-9:1989', 'l5', 'latin5', 'x-cp1254'],
|
|
30
|
-
'windows-1255': ['cp1255', 'x-cp1255'],
|
|
31
|
-
'windows-1256': ['cp1256', 'x-cp1256'],
|
|
32
|
-
'windows-1257': ['cp1257', 'x-cp1257'],
|
|
33
|
-
'windows-1258': ['cp1258', 'x-cp1258'],
|
|
34
25
|
'x-mac-cyrillic': ['x-mac-ukrainian'],
|
|
35
26
|
gbk: ['chinese', 'csgb2312', 'csiso58gb231280', 'gb2312', 'gb_2312', 'gb_2312-80', 'iso-ir-58', 'x-gbk'],
|
|
36
27
|
gb18030: [],
|
|
@@ -44,3 +35,12 @@ export default {
|
|
|
44
35
|
'utf-16le': ['csunicode', 'iso-10646-ucs-2', 'ucs-2', 'unicode', 'unicodefeff', 'utf-16'],
|
|
45
36
|
'x-user-defined': [],
|
|
46
37
|
}
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < 9; i++) labels[`windows-125${i}`] = [`cp125${i}`, `x-cp125${i}`]
|
|
40
|
+
|
|
41
|
+
// prettier-ignore
|
|
42
|
+
labels['windows-1252'].push('ansi_x3.4-1968', 'ascii', 'cp819', 'csisolatin1', 'ibm819', 'iso-8859-1', 'iso-ir-100', 'iso8859-1', 'iso88591', 'iso_8859-1', 'iso_8859-1:1987', 'l1', 'latin1', 'us-ascii')
|
|
43
|
+
// prettier-ignore
|
|
44
|
+
labels['windows-1254'].push('csisolatin5', 'iso-8859-9', 'iso-ir-148', 'iso8859-9', 'iso88599', 'iso_8859-9', 'iso_8859-9:1989', 'l5', 'latin5')
|
|
45
|
+
|
|
46
|
+
export default labels
|