-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtitleize.js
139 lines (127 loc) · 3.47 KB
/
titleize.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Titleize JS © https://github.com/GoudekettingRM/titleize && Robin Goudeketting */
const symbols = /[!¡⁄÷…æ«≤πø¬^˚¨∆¥˙†®´∑œåß∂ƒ∂©˙~µ∫√ç≈§±"#$%&()*+,./:;<=>¿?@[\\\]^_`{|}~‹™›€£¢∞]/;
const lowerCasedWords = [
'a',
'an',
'the',
'and',
'but',
'or',
'nor',
'via',
'to',
'on',
'onto',
'per',
'for',
'in',
'into',
'of',
'by',
'at',
'as',
'yet',
'so',
];
const capitalize = (value) => {
const splitValue = value.toString().split('');
splitValue[0] = splitValue[0].toUpperCase();
return splitValue.join('');
};
const cleanseSymbolList = (symbolsToIgnore, regex) => {
symbolsToIgnore.split('').forEach((symbol) => {
regex = regex.replace(new RegExp('[\\' + symbol + ']', 'g'), '');
});
return regex;
};
const getWordForTitle = (word, index, array, exceptions = {}) => {
if (word === word.toUpperCase() && exceptions.keepUpperCaseWords) {
return word;
} else if (exceptions.keepUpperCaseLetters) {
return capitalize(word);
} else if (index === 0 || index == array.length - 1) {
return capitalize(word.toLowerCase());
} else if (lowerCasedWords.includes(word.toLowerCase())) {
return word.toLowerCase();
} else {
return capitalize(word.toLowerCase());
}
};
const titleize = (value, exceptions = {}) => {
if (typeof value !== 'string') {
try {
throw new TypeError('No string was provided');
} catch (err) {
console.error(err.name + ':', err.message);
throw err;
}
} else if (!value) {
try {
throw new Error('Empty string provided');
} catch (err) {
console.error(err.name + ':', err.message);
throw err;
}
}
const symbolsForRegex = exceptions.ignoreSymbols
? cleanseSymbolList(exceptions.ignoreSymbols, symbols.toString().slice(2, -2))
: symbols.toString().slice(2, -2);
const symbolRegExp = new RegExp('[' + symbolsForRegex + ']', 'gi');
if (exceptions.isSlug) {
value = typeof exceptions.isSlug === 'string' ? value.replaceAll(exceptions.isSlug, ' ') : value.replace(/-/g, ' ');
}
return value
.replace(symbolRegExp, '')
.split(' ')
.filter((word) => {
if (word) return word;
})
.map((word, index, array) => {
const regex = /([\w+])-([\w+])/gi;
const matches = [];
while (true) {
const match = regex.exec(word);
if (!match) break;
matches.push(match[match[1] ? 1 : 2]);
}
if (exceptions.isSlug) {
return getWordForTitle(word, index, array, exceptions);
} else if (
word.indexOf('-') !== -1 &&
word.length >= 3 &&
// word.match(/(?<=[\w+])-(?=[\w+])/gi)
matches.length
) {
return word
.split('-')
.map((w) => {
return getWordForTitle(w, index, array, exceptions);
})
.join('-');
} else if (
(word.indexOf('-') !== -1 && word.length < 3) ||
(word.indexOf('-') !== -1 &&
word.length >= 3 &&
// !word.match(/(?<=[\w+])-(?=[\w+])/gi)
!matches.length)
) {
const v = word.replace(/-/g, '');
if (v.length > 0) {
return getWordForTitle(word.replace(/-/g, ''), index, array, exceptions);
}
} else {
return getWordForTitle(word, index, array, exceptions);
}
})
.join(' ')
.trim();
};
module.exports = titleize;
/*
/\ /\
\ \__/ /
(ಠ _ ಠ)
_____(_____)____
| Robin |
|_Goudeketting_|
*/