summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/metrics/cookies.js
blob: ba46ef11f8c39507ce9226a98e763a4a3ee0c4e9 (plain)
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cookieValueForKey = exports.cookiesOf = void 0;
const optional_1 = require("../types/optional");
/**
 * Iterate the cookies contained in a string.
 *
 * @param cookie - A string containing zero or more cookies.
 */
function* cookiesOf(cookie) {
    if ((0, optional_1.isNothing)(cookie)) {
        return;
    }
    const rawEntries = cookie.split(";");
    for (const rawEntry of rawEntries) {
        const keyEndIndex = rawEntry.indexOf("=");
        if (keyEndIndex === -1) {
            // If there's no splitter, treat the whole raw
            // entry as the key and provide an empty value.
            const key = decodeURIComponent(rawEntry).trim();
            yield { key, value: "" };
        }
        else {
            const key = decodeURIComponent(rawEntry.substring(0, keyEndIndex)).trim();
            const value = decodeURIComponent(rawEntry.substring(keyEndIndex + 1)).trim();
            yield { key, value };
        }
    }
}
exports.cookiesOf = cookiesOf;
/**
 * Returns value of the cookie with the given key or `null` if there's no such cookie.
 *
 * @param cookies - Cookies.
 * @param key - The key to return cookie value for.
 */
function cookieValueForKey(cookies, key) {
    for (const cookie of cookies) {
        if (cookie.key === key) {
            return cookie.value;
        }
    }
    return null;
}
exports.cookieValueForKey = cookieValueForKey;
//# sourceMappingURL=cookies.js.map