-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (67 loc) · 2.07 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
71
'use strict'
module.exports = function (string, options) {
if (!string) return ''
options = options || {}
var maintainCase = options.maintainCase
var replacement = options.replacement || '-'
var maxLen = options.maxLen || 80
var prevdash = false
var sb = [ ]
var c
for (var i = 0; i < string.length; ++i) {
c = string[i]
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
sb.push(c)
prevdash = false
} else if (c >= 'A' && c <= 'Z') {
if (maintainCase) {
sb.push(c)
} else {
sb.push(c.toLowerCase())
}
prevdash = false
} else if (c === ' ' || c === ',' || c === '.' || c === '/' ||
c === '\\' || c === '-' || c === '_' || c === '=') {
if (!prevdash && sb.length > 0) {
sb.push(replacement)
prevdash = true
}
} else if (c.charCodeAt(0) >= 128) {
var remapped = remapInternationalCharToAscii(c)
if (remapped) {
sb.push(remapped)
prevdash = false
}
}
if (sb.length === maxLen) break
}
if (prevdash) {
return sb.join('').substring(0, sb.length - 1)
} else {
return sb.join('')
}
}
function remapInternationalCharToAscii (c) {
if ('àåáâãåa'.indexOf(c) !== -1) return 'a'
else if ('èéêe'.indexOf(c) !== -1) return 'e'
else if ('ìíîïi'.indexOf(c) !== -1) return 'i'
else if ('òóôõøoð'.indexOf(c) !== -1) return 'o'
else if ('ùúûuu'.indexOf(c) !== -1) return 'u'
else if ('çccc'.indexOf(c) !== -1) return 'c'
else if ('zzž'.indexOf(c) !== -1) return 'z'
else if ('ssšs'.indexOf(c) !== -1) return 's'
else if ('ñn'.indexOf(c) !== -1) return 'n'
else if ('ýÿ'.indexOf(c) !== -1) return 'y'
else if ('gg'.indexOf(c) !== -1) return 'g'
else if (c === 'r') return 'r'
else if (c === 'l') return 'l'
else if (c === 'd') return 'd'
else if (c === 'ä') return 'ae'
else if (c === 'ö') return 'oe'
else if (c === 'ü') return 'ue'
else if (c === 'ß') return 'ss'
else if (c === 'Þ') return 'th'
else if (c === 'h') return 'h'
else if (c === 'j') return 'j'
else return ''
}