source: frontend/node_modules/whatwg-encoding/lib/whatwg-encoding.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.3 KB
Line 
1"use strict";
2const iconvLite = require("iconv-lite");
3const supportedNames = require("./supported-names.json");
4const labelsToNames = require("./labels-to-names.json");
5
6const supportedNamesSet = new Set(supportedNames);
7
8// https://encoding.spec.whatwg.org/#concept-encoding-get
9exports.labelToName = label => {
10 label = String(label).trim().toLowerCase();
11
12 return labelsToNames[label] || null;
13};
14
15// https://encoding.spec.whatwg.org/#decode
16exports.decode = (buffer, fallbackEncodingName) => {
17 let encoding = fallbackEncodingName;
18 if (!exports.isSupported(encoding)) {
19 throw new RangeError(`"${encoding}" is not a supported encoding name`);
20 }
21
22 const bomEncoding = exports.getBOMEncoding(buffer);
23 if (bomEncoding !== null) {
24 encoding = bomEncoding;
25 }
26
27 // iconv-lite will strip BOMs for us, so no need to do the stuff the spec does
28
29 return iconvLite.decode(buffer, encoding);
30};
31
32// https://github.com/whatwg/html/issues/1910#issuecomment-254017369
33exports.getBOMEncoding = buffer => {
34 if (buffer[0] === 0xFE && buffer[1] === 0xFF) {
35 return "UTF-16BE";
36 } else if (buffer[0] === 0xFF && buffer[1] === 0xFE) {
37 return "UTF-16LE";
38 } else if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
39 return "UTF-8";
40 }
41
42 return null;
43};
44
45exports.isSupported = name => {
46 return supportedNamesSet.has(String(name));
47};
Note: See TracBrowser for help on using the repository browser.