Skip to content

Commit 1a62712

Browse files
author
madflow
committed
Added preSlug and postSlug as optional formatter functions
1 parent 0d2dc22 commit 1a62712

File tree

7 files changed

+73
-19
lines changed

7 files changed

+73
-19
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
[![Build Status](https://travis-ci.org/madflow/jquery-slugify.png?branch=master)](https://travis-ci.org/madflow/jquery-slugify)
22

3-
# Slugify
3+
# jQuery Slugify
44

55
Just another another (another) url slug creation plugin for jQuery.
66

77
## Getting Started
88

9-
Download the [production version][min] or the [development version][max].
9+
You can install the plugin using Bower
10+
11+
bower install jquery-slugify
12+
13+
You can download the [production version][min] or the [development version][max].
1014

1115
[min]: https://raw.github.com/madflow/jquery-slugify/master/dist/slugify.min.js
1216
[max]: https://raw.github.com/madflow/jquery-slugify/master/dist/slugify.js

dist/slugify.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/*! jQuery Slugify - v1.0.2 - 2013-11-15
1+
/*! jQuery Slugify - v1.0.3 - 2013-11-25
22
* https://github.com/madflow/jquery-slugify
33
* Copyright (c) 2013 madflow; Licensed MIT */
44
;(function($) {
55

66
$.fn.slugify = function(source, options) {
77
return this.each(function() {
88
var $target = $(this),
9-
$source = $(source);
9+
$source = $(source);
1010

1111
$target.on('keyup change', function() {
1212
if ($target.val() !== '' && $target.val() !== undefined) {
@@ -33,20 +33,36 @@
3333
$.slugify = function(sourceString, options) {
3434
// Override default options with passed-in options.
3535
options = $.extend({}, $.slugify.options, options);
36+
37+
// Apply preSlug function - if exists
38+
if (typeof options.preSlug === 'function') {
39+
sourceString = options.preSlug(sourceString);
40+
}
41+
3642
sourceString = $.trim(sourceString); // Trim
37-
sourceString = sourceString.toLowerCase(); // Lower Case
43+
sourceString = sourceString.toLowerCase(); // Lower Case
3844
$.each(options.replaceMap, function(key, value) { // Special char map
3945
sourceString = sourceString.replace(new RegExp(key, 'g'), value || options.invalid);
4046
});
41-
return sourceString
42-
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
43-
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters
47+
48+
sourceString = sourceString
49+
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
50+
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters
51+
52+
// Apply postSlug function - if exists
53+
if (typeof options.postSlug === 'function') {
54+
sourceString = options.postSlug(sourceString);
55+
}
56+
57+
return sourceString;
4458
};
4559

4660
// Default options
4761
$.slugify.options = {
4862
whitespace: '-',
4963
invalid: '',
64+
preSlug: null,
65+
postSlug: null,
5066
replaceMap: {
5167
'á': 'a',
5268
'à': 'a',
@@ -610,4 +626,4 @@
610626
}
611627
};
612628

613-
}(jQuery));
629+
}(jQuery));

dist/slugify.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-slugify",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"engines": {
55
"node": ">= 0.8.0"
66
},

slugify.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "slugify",
33
"title": "jQuery Slugify",
44
"description": "Just another another (another) url slug creation plugin for jQuery.",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"homepage": "https://github.com/madflow/jquery-slugify",
77
"author": {
88
"name": "madflow"

src/slugify.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$.fn.slugify = function(source, options) {
1111
return this.each(function() {
1212
var $target = $(this),
13-
$source = $(source);
13+
$source = $(source);
1414

1515
$target.on('keyup change', function() {
1616
if ($target.val() !== '' && $target.val() !== undefined) {
@@ -37,20 +37,36 @@
3737
$.slugify = function(sourceString, options) {
3838
// Override default options with passed-in options.
3939
options = $.extend({}, $.slugify.options, options);
40+
41+
// Apply preSlug function - if exists
42+
if (typeof options.preSlug === 'function') {
43+
sourceString = options.preSlug(sourceString);
44+
}
45+
4046
sourceString = $.trim(sourceString); // Trim
41-
sourceString = sourceString.toLowerCase(); // Lower Case
47+
sourceString = sourceString.toLowerCase(); // Lower Case
4248
$.each(options.replaceMap, function(key, value) { // Special char map
4349
sourceString = sourceString.replace(new RegExp(key, 'g'), value || options.invalid);
4450
});
45-
return sourceString
46-
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
47-
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters
51+
52+
sourceString = sourceString
53+
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
54+
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters
55+
56+
// Apply postSlug function - if exists
57+
if (typeof options.postSlug === 'function') {
58+
sourceString = options.postSlug(sourceString);
59+
}
60+
61+
return sourceString;
4862
};
4963

5064
// Default options
5165
$.slugify.options = {
5266
whitespace: '-',
5367
invalid: '',
68+
preSlug: null,
69+
postSlug: null,
5470
replaceMap: {
5571
'á': 'a',
5672
'à': 'a',
@@ -614,4 +630,4 @@
614630
}
615631
};
616632

617-
}(jQuery));
633+
}(jQuery));

test/slugify_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@
9292
equal($('#slug-target-span').text(), 'hello-spanner', "Slug added to span correctly again");
9393
});
9494

95+
test('test the preSlug postSlug callbacks', function() {
96+
97+
expect(2);
98+
99+
strictEqual($.slugify('a', {
100+
postSlug: function(sourceString) {
101+
return sourceString.toUpperCase();
102+
}
103+
}), 'A', 'Uppercase postSlug');
104+
105+
strictEqual($.slugify('a', {
106+
postSlug: function(sourceString) {
107+
return sourceString + 'rsch';
108+
}
109+
}), 'arsch', 'Naughty word appendend preSlug');
110+
});
111+
112+
95113
QUnit.testDone(function() {
96114
$('#slug-target').val('');
97115
$('#slug-source').val('');

0 commit comments

Comments
 (0)