-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (84 loc) · 3.5 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** @module slugify*/
var letters = {
'a': 'áàâǎăãảȧạäåḁāąⱥȁấầẫẩậắằẵẳặǻǡǟȁȃa',
'A': 'ÁÀÂǍĂÃẢȦẠÄÅḀĀĄȺȀẤẦẪẨẬẮẰẴẲẶǺǠǞȀȂA',
'b': 'ḃḅḇƀᵬᶀb',
'B': 'ḂḄḆɃƁʙB',
'c': 'ćĉčċçḉȼɕcƇ',
'C': 'ĆĈČĊÇḈȻCƈ',
'd': 'ďḋḑḍḓḏđɗƌdᵭᶁᶑȡ',
'D': 'ĎḊḐḌḒḎĐƉƊƋDᴅᶑȡ',
'e': 'éèêḙěĕẽḛẻėëēȩęɇȅếềễểḝḗḕȇẹệeᶒⱸ',
'E': 'ÉÈÊḘĚĔẼḚẺĖËĒȨĘɆȄẾỀỄỂḜḖḔȆẸỆEᴇ',
'f': 'ḟƒfᵮᶂ',
'F': 'ḞƑF',
'g': 'ǵğĝǧġģḡǥɠgᶃ',
'G': 'ǴĞĜǦĠĢḠǤƓGɢ',
'h': 'ĥȟḧḣḩḥḫ̱ẖħⱨh',
'H': 'ĤȞḦḢḨḤḪĦⱧHʜ',
'i': 'íìĭîǐïḯĩįīỉȉȋịḭɨiıi',
'I': 'ÍÌĬÎǏÏḮĨĮĪỈȈȊỊḬƗİII',
'j': 'ĵɉjʝɟʄǰ',
'J': 'ĴɈJᴊ',
'k': 'ḱǩķḳḵƙⱪꝁkᶄ',
'K': 'ḰǨĶḲḴƘⱩꝀKᴋ',
'l': 'ĺľļḷḹḽḻłŀƚⱡɫlɬᶅɭȴ',
'L': 'ĹĽĻḶḸḼḺŁĿȽⱠⱢLʟ',
'm': 'ḿṁṃɱmᵯᶆ',
'M': 'ḾṀṂⱮMᴍ',
'n': 'ńǹňñṅņṇṋṉɲƞnŋᵰᶇɳȵ',
'N': 'ŃǸŇÑṄŅṆṊṈṉƝȠNŊɴ',
'o': 'óòŏôốồỗổǒöȫőõṍṏȭȯȱøǿǫǭōṓṑỏȍȏơớờỡởợọộɵoⱺᴏ',
'O': 'ÓÒŎÔỐỒỖỔǑÖȪŐÕṌṎȬȮȰØǾǪǬŌṒṐỎȌȎƠỚỜỠỞỢỌỘƟO',
'p': 'ṕṗᵽƥpᵱᶈᴘ',
'P': 'ṔṖⱣƤP',
'q': 'ɋʠq',
'Q': 'ɊQ',
'r': 'ŕřṙŗȑȓṛṝṟɍɽrᵲᶉɼɾᵳ',
'R': 'ŔŘṘŖȐȒṚṜṞɌⱤRʀ',
's': 'śṥŝšṧṡşṣṩșsßẛᵴᶊʂȿſ',
'S': 'ŚṤŜŠṦṠŞṢṨȘSẞ',
't': 'ťṫţṭțṱṯŧⱦƭʈtẗᵵƫȶ',
'T': 'ŤṪŢṬȚṰṮŦȾƬƮTᴛ',
'u': 'úùŭûǔůüǘǜǚǖűũṹųūṻủȕȗưứừữửựụṳṷṵʉᶙ',
'U': 'ÚÙŬÛǓŮÜǗǛǙǕŰŨṸŲŪṺỦȔȖƯỨỪỮỬỰỤṲṶṴɄ',
'v': 'ṽṿʋvⱱⱴᴠᶌ',
'V': 'ṼṾƲV',
'w': 'ẃẁŵẅẇẉⱳwẘ',
'W': 'ẂẀŴẄẆẈⱲW',
'x': 'ẍẋxᶍ',
'X': 'ẌẊX',
'y': 'ýỳŷÿỹẏȳỷỵɏƴyẙ',
'Y': 'ÝỲŶŸỸẎȲỶỴɎƳYʏ',
'z': 'źẑžżẓẕƶȥⱬzᵶᶎʐʑɀᴢ',
'Z': 'ŹẐŽŻẒẔƵȤⱫZ',
};
function lookup (m) { return translate[m] || m; }
//a string of every character we want to replace
var matchCharacters = '';
//create an object to hold all translations
var translate = {};
Object.keys(letters).forEach(l => {
//add all the letters variations to the string
matchCharacters += letters[l];
//loop through each char in the set
for (var i=0; i<letters[l].length; i++) {
translate[ letters[l][i] ] = l;
}
});
var accentRegex = new RegExp('[' + matchCharacters + ']', 'g');
/**Generates a slug with only lowercase letters and dashes, from any text string.
* @param {string} text - the unformatted text
*/
function slugify (text) {
return String(text)
.toLowerCase() //make all lowercase
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/\//g, '-') // Replace slashes with -
.replace(accentRegex, lookup) //replace accented letters with normal
.replace(/[^a-z0-9-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
module.exports = slugify;