-
Notifications
You must be signed in to change notification settings - Fork 26
/
jquery.slug.js
84 lines (60 loc) · 1.63 KB
/
jquery.slug.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
/*!
slug plug-in for jQuery, License - MIT, Copyright: 2010 Traversal
*/
/*
Title: slug plug-in
Description:
Creates "slug" strings out of another string value, which are safe to
use as URL slugs, or variable names etc.
Author Info:
Created By - Traversal <http:https://traversal.com.au>
Licence - MIT Style <http:https://en.wikipedia.org/wiki/MIT_License>
Requires:
jQuery 1.3 - http:https://jquery.com
Companion plug-ins:
Metadata - http:https://plugins.jquery.com/project/metadata (optional)
*/
(function($) {
var defaults = {
sep : '-',
strip: /[!@#$%^\*=\(\)\{\}\|\~`;:"'<>\,\.\/\?]/gi,
replace: /[\-\_\s\&\+\[\]]/gi,
noDuplicates: true,
toCase: 'lower'
};
var pn = 'slug';
$[pn] = function() {
var p, o, cmd, val, options = {}, params = {}, a = arguments;
val = a[0];
if (a.length >= 2) {
if (typeof(a[1]) == "string") {
cmd = a[1];
} else {
options = a[1];
}
}
if (a.length >= 3) {
params = a[2];
}
p = params;
o = $.extend(true, {}, defaults, options);
var ret = val;
// remove unwanted characters
ret = ret.replace(o.strip, '');
// replace others with the separator
ret = ret.replace(o.replace, o.sep);
// remove duplicate separators
if (o.noDuplicates) {
ret = ret.replace(new RegExp("[" + o.sep + "]{2,}"), o.sep);
}
if (o.toCase) {
if (o.toCase == 'upper') {
ret = ret.toUpperCase()
} else {
ret = ret.toLowerCase()
}
}
return ret;
};
$[pn].defaults = defaults;
})(jQuery);