-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
31 lines (25 loc) · 1.08 KB
/
utils.ts
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
export function isValidBinName(name: string) {
const VALID_BIN_NAME_REGEX = /^[a-zA-Z0-9-_]+$/;
if (name.length < 10 || name.length > 100) return false;
if (!VALID_BIN_NAME_REGEX.test(name)) return false;
return true;
}
// https://stackoverflow.com/questions/11100821/javascript-regex-for-validating-filenames
export function isValidFilename(name: string) {
if (typeof name !== "string") return false;
if (name.length > 255) return false;
const rg1 = /^[^\\/:\*\?"<>\|]+$/; // forbidden characters \ / : * ? " < > |
const rg2 = /^\./; // cannot start with dot (.)
// const rg3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names - we ain't windows
return rg1.test(name) && !rg2.test(name); // && !rg3.test(name);
}
export function makeId(length: number) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
counter++;
}
return result;
}