aboutsummaryrefslogtreecommitdiffstats
path: root/config/quickshell/modules/common/functions/string_utils.js
blob: c31edf496fbf56c81d165144a21a5775d08ff939 (plain) (blame)
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
/**
 * Formats a string according to the args that are passed in
 * @param { string } str 
 * @param  {...any} args 
 * @returns 
 */
function format(str, ...args) {
    return str.replace(/{(\d+)}/g, (match, index) =>
        typeof args[index] !== 'undefined' ? args[index] : match
    );
}

/**
 * Returns the domain of the passed in url or null
 * @param { string } url 
 * @returns { string| null } 
 */
function getDomain(url) {
    const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/);
    return match ? match[1] : null;
}

/**
 * Returns the base url of the passed in url or null
 * @param { string } url 
 * @returns { string | null } 
 */
function getBaseUrl(url) {
    const match = url.match(/^(https?:\/\/[^\/]+)(\/.*)?$/);
    return match ? match[1] : null;
}

/**
 * Escapes single quotes in shell commands
 * @param { string } str 
 * @returns { string }
 */
function shellSingleQuoteEscape(str) {
    //  escape single quotes
    return String(str)
        // .replace(/\\/g, '\\\\')
        .replace(/'/g, "'\\''");
}

function escapeHtml(str) {
    if (typeof str !== 'string') return str;
    return str
        .replace(/&/g, '&')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;');
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage