-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
77 lines (73 loc) · 2.36 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
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
69
70
71
72
73
74
75
76
77
/** @format */
/**
* Retrieves the last tag from an array of tags.
*
* @template T - The type of the tags array elements.
* @param {T[]} tags - The array of tags.
* @returns {T} - Returns the last tag in the array.
*/
export function getLastTag<T>(tags: T[]): T {
return tags.slice(-1)[0];
}
/**
* Adds a tag to an array of tags.
*
* @template T - The type of the tags array elements.
* @param {T[]} tags - The array of tags.
* @param {T} tag - The tag to add.
* @returns {T[]} - Returns a new array of tags with the specified tag added.
*/
export function addTag<T>(tags: T[], tag: T): T[] {
return [...tags, tag];
}
/**
* Removes a tag from an array of tags.
*
* @template T - The type of the tags array elements.
* @param {T[]} tags - The array of tags.
* @param {T} tag - The tag to remove.
* @returns {T[]} - Returns a new array of tags with the specified tag removed.
*/
export function removeTag<T extends { id: number | string }>(
tags: T[],
tag: T
): T[] {
return tags.filter(i => i.id !== tag.id);
}
/**
* Generates a random alphanumeric ID.
*
* @returns {string} - Returns a random alphanumeric ID.
*/
export function generateId(): string {
return Math.random()
.toString(36)
.substring(2, 5);
}
/**
* Checks if the last character of the given text matches any of the tags provided.
* Also checks if the entire text consists only of the provided tags.
*
* @param {string} text - The text to check.
* @param {string[]} tagOnString - An optional array of tags to match against.
* @returns {boolean} - Returns true if the last character of the text matches any of the tags and the entire text consists only of the provided tags, otherwise returns false.
*/
export function isMatchTags(text: string, tagOnString: string[] = []): boolean {
return (
tagOnString.includes(text.slice(-1)) &&
!text.match(new RegExp(`^[${tagOnString.join('')}]+$`, 'g'))
);
}
/**
* Checks if a tag can be added to the given text.
*
* @param {string} text - The text to check.
* @param {string[]} createTagOnString - An optional array of tags to match against.
* @returns {boolean} - Returns true if the text length is greater than 1 and the text matches the provided tags, otherwise returns false.
*/
export function canAddTag(
text: string,
createTagOnString: string[] = []
): boolean {
return text.length > 1 && isMatchTags(text, createTagOnString);
}