forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitleify.js
More file actions
71 lines (65 loc) · 1.64 KB
/
Copy pathtitleify.js
File metadata and controls
71 lines (65 loc) · 1.64 KB
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
const { preFormatted, stopWords } = require('./formatting');
const prototypeRE = /prototype/i;
const prototypesRE = /prototypes/i;
const removeProto = x => x !== 'prototype';
function titleCase(word) {
return word[0].toUpperCase() + word.slice(1);
}
function prototyper(str) {
const formatted = str
.replace(/javascript/i, '')
.split('-')
.map(str => {
if (prototypeRE.test(str)) {
if (str.length > 9) {
return prototyper(
str
.trim()
.split('prototype')
.join('-prototype-')
);
}
return str;
}
return titleify(str);
})
.join(' ')
.split(' ');
const noProto = formatted.filter(removeProto).filter(x => !!x);
if (noProto.length === 2) {
const [first, second] = noProto;
const secondLC = second.toLowerCase();
const finalSecond = preFormatted[secondLC]
? preFormatted[secondLC]
: secondLC;
return `${titleify(first)}.prototype.${finalSecond}`;
}
if (noProto.length === 1) {
return prototyper(
noProto[0]
.toLowerCase()
.split('.')
.join('-')
);
}
return titleify(str, true);
}
function titleify(str, triedPrototyper) {
if (str.match(prototypeRE) && !triedPrototyper && !prototypesRE.test(str)) {
return prototyper(str);
}
return str
.toLowerCase()
.split('-')
.map((word, i) => {
if (!word) {
return '';
}
if (stopWords.some(x => x === word) && i !== 0) {
return word;
}
return preFormatted[word] ? preFormatted[word] : titleCase(word);
})
.join(' ');
}
module.exports = titleify;