-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
70 lines (63 loc) · 1.69 KB
/
index.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
function capitalize (string, opts) {
opts = normalizeOptions(opts)
if (!opts.preserve) {
string = string.toLowerCase();
}
return string.charAt(0).toUpperCase() + string.substring(1);
}
// a QUOTE character immediately followed by a word character
var QUOTE = /['"`’]/
var WORD = /[0-9a-zA-Z\u00C0-\u017F\u0400-\u04FF]/
capitalize.words = function (string, opts) {
opts = normalizeOptions(opts)
if (!opts.preserve) {
string = string.toLowerCase();
}
var startOfWord = 0
var nonWord = /[^0-9a-zA-Z\u00C0-\u017F\u0400-\u04FF]+|$/g
var match
var out = ""
var count = 0
while (match = nonWord.exec(string)) {
var sep = match[0]
var sepStart = nonWord.lastIndex - sep.length
if (QUOTE.test(string[sepStart]) && WORD.test(string[sepStart + 1])) {
// don't capitalize after an embedded quote
continue
}
var word = string.substring(startOfWord, nonWord.lastIndex - sep.length)
if (QUOTE.test(word[0])) {
// strip leading quote
out += word[0]
word = word.substring(1)
}
if (typeof opts.skipWord === 'function' && opts.skipWord(word, count)) {
out += word
} else {
out += capitalize(word, opts)
}
out += sep
startOfWord = nonWord.lastIndex
count++
if (startOfWord == string.length) {
break
}
}
return out
}
function normalizeOptions(opts) {
if (!opts) {
return { preserve: false }
}
if (typeof opts === 'boolean') {
return { preserve: opts }
}
if (opts.skipWord instanceof RegExp) {
const rgx = opts.skipWord
opts.skipWord = function (word, position) {
return position > 0 && rgx.test(word)
}
}
return opts || {}
}
module.exports = capitalize