8
common/resources/client/utils/string/is-email.ts
Executable file
8
common/resources/client/utils/string/is-email.ts
Executable file
@@ -0,0 +1,8 @@
|
||||
const matcher =
|
||||
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
||||
|
||||
export function isEmail(string?: string): boolean {
|
||||
if (!string) return false;
|
||||
if (string.length > 320) return false;
|
||||
return matcher.test(string);
|
||||
}
|
||||
4
common/resources/client/utils/string/lower-first.ts
Executable file
4
common/resources/client/utils/string/lower-first.ts
Executable file
@@ -0,0 +1,4 @@
|
||||
export function lowerFirst(string: string): string {
|
||||
if (!string) return '';
|
||||
return string.charAt(0).toLowerCase() + string.slice(1);
|
||||
}
|
||||
11
common/resources/client/utils/string/random-number.ts
Executable file
11
common/resources/client/utils/string/random-number.ts
Executable file
@@ -0,0 +1,11 @@
|
||||
export function randomNumber(min: number = 1, max: number = 10000) {
|
||||
const randomBuffer = new Uint32Array(1);
|
||||
|
||||
window.crypto.getRandomValues(randomBuffer);
|
||||
|
||||
const number = randomBuffer[0] / (0xffffffff + 1);
|
||||
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
return Math.floor(number * (max - min + 1)) + min;
|
||||
}
|
||||
11
common/resources/client/utils/string/random-string.ts
Executable file
11
common/resources/client/utils/string/random-string.ts
Executable file
@@ -0,0 +1,11 @@
|
||||
export function randomString(length: number = 36) {
|
||||
let random = '';
|
||||
const possible =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
random += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
}
|
||||
|
||||
return random;
|
||||
}
|
||||
21
common/resources/client/utils/string/slugify-string.ts
Executable file
21
common/resources/client/utils/string/slugify-string.ts
Executable file
@@ -0,0 +1,21 @@
|
||||
import slugify from 'slugify';
|
||||
|
||||
export function slugifyString(
|
||||
text: string,
|
||||
replacement = '-',
|
||||
strict = false,
|
||||
): string {
|
||||
if (!text) return text;
|
||||
let slugified = slugify(text, {
|
||||
lower: true,
|
||||
replacement,
|
||||
strict,
|
||||
remove: /[*+~.()'"!:@?\|/\\#]/g,
|
||||
});
|
||||
// some chinese text might not get slugified properly,
|
||||
// just replace whitespace with dash in that case
|
||||
if (!slugified) {
|
||||
slugified = text.replace(/\s+/g, '-').toLowerCase();
|
||||
}
|
||||
return slugified;
|
||||
}
|
||||
3
common/resources/client/utils/string/strip-tags.ts
Executable file
3
common/resources/client/utils/string/strip-tags.ts
Executable file
@@ -0,0 +1,3 @@
|
||||
export function stripTags(str: string) {
|
||||
return str.replace(/<\/?[^>]+(>|$)/g, '');
|
||||
}
|
||||
6
common/resources/client/utils/string/truncate-string.ts
Executable file
6
common/resources/client/utils/string/truncate-string.ts
Executable file
@@ -0,0 +1,6 @@
|
||||
export function truncateString(str: string, length: number, end = '...') {
|
||||
if (length == null || length >= str.length) {
|
||||
return str;
|
||||
}
|
||||
return str.slice(0, Math.max(0, length - end.length)) + end;
|
||||
}
|
||||
4
common/resources/client/utils/string/uc-first.ts
Executable file
4
common/resources/client/utils/string/uc-first.ts
Executable file
@@ -0,0 +1,4 @@
|
||||
export function ucFirst<T extends string>(string: T): T {
|
||||
if (!string) return string;
|
||||
return (string.charAt(0).toUpperCase() + string.slice(1)) as T;
|
||||
}
|
||||
Reference in New Issue
Block a user