This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextDecoder.js
80 lines (73 loc) · 1.9 KB
/
TextDecoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { between } from './math.js';
/**
*
* Array of supported encodings
* @Todo: At least add utf-16
* @type {Array}
*/
const supportedEncodings = ['utf-8', 'utf8'];
export const BOM = new Uint8Array([0xEF, 0xBB, 0xBF]);
export class TextDecoder {
constructor(encoding = 'utf-8', { fatal = false, ignoreBOM = false } = {}) {
if (typeof encoding !== 'string') {
encoding = encoding.toString().toLowerCase();
} else {
encoding = encoding.toLowerCase();
}
if (! supportedEncodings.includes(encoding)) {
throw new RangeError(`TextDecoder constructor: The given encoding '${encoding}' is not supported.`);
}
Object.defineProperties(this, {
encoding: {
configurable: false,
enumerable: true,
writable: false,
value: encoding,
},
fatal: {
configurable: false,
enumerable: true,
writable: false,
value: fatal,
},
ignoreBOM: {
configurable: false,
enumerable: true,
writable: false,
value: ignoreBOM,
},
});
}
decode(data) {
switch(this.encoding) {
case 'utf-8':
case 'utf8': {
let prev = NaN;
/**
* For (0,127) we just take the charCode
* For charCode 128+this will be in the form [n=194+, m=(128-191)]
* where n increments after m reaches 191, increments, and rolls back to 128
*/
return data.reduce((str, i, n) => {
if (Number.isNaN(prev) && i < 128) {
return str + String.fromCharCode(i);
} else if (Number.isNaN(prev) && i > 193) {
prev = i;
return str;
} else if (prev > 193 && between(128, i, 194)) {
const offset = (prev - 194) * 64;
prev = NaN;
return str + String.fromCharCode(offset + i);
} else if (this.fatal) {
throw new RangeError(`Unhandled character at postion ${n}`);
} else {
prev = NaN;
return str;
}
}, '');
}
default:
throw new TypeError(`Unsupported encoding '${this.encoding}'`);
}
}
}