2 /* --------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 * ------------------------------------------------------------------------------------------ */
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.Encodings = void 0;
9 (function (Encodings) {
10 function getEncodingHeaderValue(encodings) {
11 if (encodings.length === 1) {
12 return encodings[0].name;
14 const distribute = encodings.length - 1;
15 if (distribute > 1000) {
16 throw new Error(`Quality value can only have three decimal digits but trying to distribute ${encodings.length} elements.`);
18 const digits = Math.ceil(Math.log10(distribute));
19 const factor = Math.pow(10, digits);
20 const diff = Math.floor((1 / distribute) * factor) / factor;
23 for (const encoding of encodings) {
24 result.push(`${encoding.name};q=${q === 1 || q === 0 ? q.toFixed(0) : q.toFixed(digits)}`);
27 return result.join(', ');
29 Encodings.getEncodingHeaderValue = getEncodingHeaderValue;
30 function parseEncodingHeaderValue(value) {
31 const map = new Map();
32 const encodings = value.split(/\s*,\s*/);
33 for (const value of encodings) {
34 const [encoding, q] = parseEncoding(value);
35 if (encoding === '*') {
38 let values = map.get(q);
39 if (values === undefined) {
43 values.push(encoding);
45 const keys = Array.from(map.keys());
46 keys.sort((a, b) => b - a);
48 for (const key of keys) {
49 result.push(...map.get(key));
53 Encodings.parseEncodingHeaderValue = parseEncodingHeaderValue;
54 function parseEncoding(value) {
57 const index = value.indexOf(';q=');
59 const parsed = parseFloat(value.substr(index));
63 encoding = value.substr(0, index);
70 })(Encodings = exports.Encodings || (exports.Encodings = {}));
71 //# sourceMappingURL=encoding.js.map