All files / src/web analytics-filters.js

100% Statements 23/23
100% Branches 14/14
100% Functions 6/6
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      109x 109x 109x 109x   109x 372x 372x         202x 202x               207x 170x 170x 207x     109x       24x                   169x 169x 397x 230x   2x    
import { Schemas, splitImage } from "../media-resolver.js";
import { Sources } from "./media-catalogue.js";
 
const MaxErrorLabelLength = 100;
const QuotedText = /"[^"]*"|'[^']*'|`[^`]*`|“[^”]*”|‘[^’]*’/g;
const Addresses = /\b[a-z][a-z0-9+.-]*:\/\/\S+|\b(?:data|blob|file):\S+/gi;
const PeerBucketTops = [0, 1, 2, 4];
 
const schemaOf = (ref) => {
    const { schema } = splitImage(ref);
    return { schema, known: Schemas[schema] ?? Schemas[""] };
};
 
/** The `Sources` key a reference comes from, or its schema for one with no source of its own (`https`, `data`). */
export function sourceKey(ref) {
    const { schema, known } = schemaOf(ref);
    return known.source ?? schema;
}
 
/**
 * A media reference whole when anyone could name it (a catalogue entry, a built-in disc, a public URL), and only by
 * its source when it is the user's own.
 */
export function mediaLabel(ref) {
    if (!ref) return ref;
    const { known } = schemaOf(ref);
    const personal = known.source ? Sources[known.source]?.personal : known.personal;
    return personal ? sourceKey(ref) : ref;
}
 
export const redacted = () => "redacted";
 
/** An error message without what it quotes or the addresses it names, which may be the user's own. */
export function errorLabel(message) {
    return String(message)
        .replace(QuotedText, '""')
        .replace(Addresses, "<address>")
        .replace(/\s+/g, " ")
        .trim()
        .slice(0, MaxErrorLabelLength);
}
 
/** A count of people, in buckets coarse enough that no session stands out by it. */
export function countBucket(count) {
    let bottom = 0;
    for (const top of PeerBucketTops) {
        if (count <= top) return bottom === top ? `${top}` : `${bottom}-${top}`;
        bottom = top + 1;
    }
    return `${bottom}+`;
}