forked from reactplay/react-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.js
51 lines (45 loc) · 1.47 KB
/
string.js
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
export const toKebabCase = (str) => {
return str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map((x) => x.toLowerCase())
.join("-");
};
export const toPascalcase = (str) => {
let res = `${str}`
.toLowerCase()
.replace(new RegExp(/[-_]+/, "g"), " ")
.replace(new RegExp(/[^\w\s]/, "g"), "")
.replace(
new RegExp(/\s+(.)(\w*)/, "g"),
($1, $2, $3) => `${$2.toUpperCase() + $3}`
)
.replace(new RegExp(/\w/), (s) => s.toUpperCase());
res = res.replace(/^./, str[0].toLowerCase());
return res;
};
export const toTitleCase = (str) => {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
export const replaceAll = (str, replaceWhat, replaceTo) => {
replaceWhat = replaceWhat.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
var re = new RegExp(replaceWhat, "g");
return str.replace(re, replaceTo);
};
export const toTitleCaseTrimmed = (str) => {
const titleCse = toTitleCase(str);
return titleCse.replace(/\s/g, "");
};
export const toSlug = (str) => {
return toSanitized(str).toLowerCase();
};
export const toSanitized = (str) => {
//replace all special characters | symbols with a space
str = str.replace(/[`~!@#$%^&*()_\-+=\[\]{};:'"\\|\/,.<>?\s]/g, " ");
// trim spaces at start and end of string
str = str.replace(/^\s+|\s+$/gm, "");
// replace space with dash/hyphen
str = str.replace(/\s+/g, "-");
return str;
};