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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/**
* Text and sorting utilities.
* @module app/utils/text_utils
*/
/**
* Remove HTML-unfriendly characters from a string
* @param {string} text string to sanitize
* @return {string} the sanitized string
*/
export const sanitize = (text) =>
String(text || "").replace(new RegExp("[<>&]", "g"), "");
/**
* Remove non-alphanumerics from a string
* @param {string} text string to sanitize
* @return {string} the sanitized string
*/
export const sanitizeName = (text) =>
String(text || "").replace(new RegExp("[^-_a-zA-Z0-9]", "g"), "");
/**
* Remove HTML from a string
* @param {string} text string to sanitize
* @return {string} the sanitized string
*/
export const stripHTML = (text) => String(text || "").replace(/<[^>]+>/g, "");
/**
* Replace HTML special characters with entitites
* @param {string} text string to sanitize
* @return {string} the sanitized string
*/
export const sanitizeHTML = (text) =>
String(text || "")
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
/**
* Capitalize a single word
* @param {string} text string to capitalize
* @return {string} the capitalized word
*/
export const capitalizeWord = (text) =>
text.charAt(0).toUpperCase() + text.slice(1);
/**
* Capitalize all words in a string
* @param {string} text string to capitalize
* @return {string} the capitalized string
*/
export const capitalize = (text) =>
String(text || "")
.split(" ")
.map(capitalizeWord)
.join(" ");
/**
* Convert a phrase to a slug
* @param {string} text string to slugify
* @return {string} the slugified string
*/
export const slugify = (text) =>
String(text || "")
.toLowerCase()
.replace(/\s/g, "-")
.replace(/[^-_a-zA-Z0-9]/g, "-")
.replace(/-+/g, "-");
/**
* Sort function, sort strings ascending (a -> z)
*/
// eslint-disable-next-line id-length
export const sortString = (a, b) => a[0].localeCompare(b[0]);
/**
* Sort function, sort strings descending (z -> a)
*/
// eslint-disable-next-line id-length
export const sortStringDescending = (a, b) => b[0].localeCompare(a[0]);
/**
* Sort function, sort numbers ascending (0 -> 1)
*/
// eslint-disable-next-line id-length
export const sortNumeric = (a, b) => a[1] - b[1];
/**
* Sort function, sort numbers descending (1 -> 0)
*/
// eslint-disable-next-line id-length
export const sortNumericDescending = (a, b) => b[1] - a[1];
|