summary refs log tree commit diff
path: root/src/extensions/String.ts
blob: fb34600f4c4fb3914fb8c99a2dbcc174006cca45 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Spacebar and Spacebar Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { Request } from "express";
import { SPECIAL_CHAR } from "@spacebar/util/util/Regex";
import { Random, ntob } from "@spacebar/extensions";
import { FieldErrors } from "@spacebar/util/util/FieldError";

export function trimSpecial(str?: string): string {
    if (!str) return "";
    return str.replace(SPECIAL_CHAR, "").trim();
}

/**
 * Capitalizes the first letter of a string.
 * @param str The string to capitalize.
 * @returns The capitalized string.
 */
export function capitalize(str: string): string {
    if (!str) return "";
    return str.charAt(0).toUpperCase() + str.slice(1);
}

export function centerString(str: string, len: number): string {
    const pad = len - str.length;
    const padLeft = Math.floor(pad / 2) + str.length;
    return str.padStart(padLeft).padEnd(len);
}

export function stringGlobToRegexp(str: string, flags?: string): RegExp {
    // Convert simple wildcard patterns to regex
    const escaped = str.replace(".", "\\.").replace("?", ".").replace("*", ".*");
    return new RegExp(escaped, flags);
}

// TODO: use exception type
export function stringCheckLength(str: string, min: number, max: number, key: string, req: Request) {
    if (str.length < min || str.length > max) {
        throw FieldErrors({
            [key]: {
                code: "BASE_TYPE_BAD_LENGTH",
                // TODO: remove dependency on request (add generic field?)
                message: req.t("common:field.BASE_TYPE_BAD_LENGTH", {
                    length: `${min} - ${max}`,
                }),
            },
        });
    }
}

export function generateCode() {
    return ntob(Date.now() + Random.nextInt(0, 10000));
}