-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (40 loc) · 1019 Bytes
/
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
const slugify = require('slugify')
const STOP_WORDS = new Set(['and', 'or', 'an'])
slugify.extend({
$: '',
'&': '',
'|': '',
'☠': 'skull-bones',
'☣': 'biohazard',
'☭': 'hammer-sickle',
'☯': 'yin-yang',
'☮': 'peace',
'☏': 'telephone',
'☎': 'telephone',
'★': 'star',
'☂': 'umbrella',
'☃': 'snowman',
'✈': 'airplane',
'✉': 'envelope',
'✊': 'raised-fist'
})
function slug (input) {
return slugify(String(input), { replacement: '-', lower: true, remove: /[^\w\s_~-]+/g })
.split('-')
.filter(word => word && !STOP_WORDS.has(word))
.join('-')
}
function makeSlug (id, string) {
if (!string) { return `${id}` }
return `${slug(string)}-${id}`
}
function makeOldSlug (id, string) {
if (!string) { return `${id}` }
return `${id}-${slug(string)}`
}
module.exports = {
slug,
slugUser: user => makeSlug(user.id, user.name),
slugPost: post => makeSlug(post.id, post.title),
slugArticle: article => makeOldSlug(article.id, article.title)
}