Skip to content

Commit

Permalink
Added preSlug and postSlug as optional formatter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
madflow committed Nov 25, 2013
1 parent 0d2dc22 commit 1a62712
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 19 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[![Build Status](https://travis-ci.org/madflow/jquery-slugify.png?branch=master)](https://travis-ci.org/madflow/jquery-slugify)

# Slugify
# jQuery Slugify

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

## Getting Started

Download the [production version][min] or the [development version][max].
You can install the plugin using Bower

bower install jquery-slugify

You can download the [production version][min] or the [development version][max].

[min]: https://raw.github.com/madflow/jquery-slugify/master/dist/slugify.min.js
[max]: https://raw.github.com/madflow/jquery-slugify/master/dist/slugify.js
Expand Down
30 changes: 23 additions & 7 deletions dist/slugify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*! jQuery Slugify - v1.0.2 - 2013-11-15
/*! jQuery Slugify - v1.0.3 - 2013-11-25
* https://github.com/madflow/jquery-slugify
* Copyright (c) 2013 madflow; Licensed MIT */
;(function($) {

$.fn.slugify = function(source, options) {
return this.each(function() {
var $target = $(this),
$source = $(source);
$source = $(source);

$target.on('keyup change', function() {
if ($target.val() !== '' && $target.val() !== undefined) {
Expand All @@ -33,20 +33,36 @@
$.slugify = function(sourceString, options) {
// Override default options with passed-in options.
options = $.extend({}, $.slugify.options, options);

// Apply preSlug function - if exists
if (typeof options.preSlug === 'function') {
sourceString = options.preSlug(sourceString);
}

sourceString = $.trim(sourceString); // Trim
sourceString = sourceString.toLowerCase(); // Lower Case
sourceString = sourceString.toLowerCase(); // Lower Case
$.each(options.replaceMap, function(key, value) { // Special char map
sourceString = sourceString.replace(new RegExp(key, 'g'), value || options.invalid);
});
return sourceString
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters

sourceString = sourceString
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters

// Apply postSlug function - if exists
if (typeof options.postSlug === 'function') {
sourceString = options.postSlug(sourceString);
}

return sourceString;
};

// Default options
$.slugify.options = {
whitespace: '-',
invalid: '',
preSlug: null,
postSlug: null,
replaceMap: {
'á': 'a',
'à': 'a',
Expand Down Expand Up @@ -610,4 +626,4 @@
}
};

}(jQuery));
}(jQuery));
4 changes: 2 additions & 2 deletions dist/slugify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-slugify",
"version": "1.0.2",
"version": "1.0.3",
"engines": {
"node": ">= 0.8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion slugify.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "slugify",
"title": "jQuery Slugify",
"description": "Just another another (another) url slug creation plugin for jQuery.",
"version": "1.0.2",
"version": "1.0.3",
"homepage": "https://github.com/madflow/jquery-slugify",
"author": {
"name": "madflow"
Expand Down
28 changes: 22 additions & 6 deletions src/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$.fn.slugify = function(source, options) {
return this.each(function() {
var $target = $(this),
$source = $(source);
$source = $(source);

$target.on('keyup change', function() {
if ($target.val() !== '' && $target.val() !== undefined) {
Expand All @@ -37,20 +37,36 @@
$.slugify = function(sourceString, options) {
// Override default options with passed-in options.
options = $.extend({}, $.slugify.options, options);

// Apply preSlug function - if exists
if (typeof options.preSlug === 'function') {
sourceString = options.preSlug(sourceString);
}

sourceString = $.trim(sourceString); // Trim
sourceString = sourceString.toLowerCase(); // Lower Case
sourceString = sourceString.toLowerCase(); // Lower Case
$.each(options.replaceMap, function(key, value) { // Special char map
sourceString = sourceString.replace(new RegExp(key, 'g'), value || options.invalid);
});
return sourceString
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters

sourceString = sourceString
.replace(/\s+/g, options.whitespace) // Replace whitespace characters
.replace(new RegExp('[^a-z0-9 ' + options.whitespace + ']', 'g'), options.invalid); // Replace invalid characters

// Apply postSlug function - if exists
if (typeof options.postSlug === 'function') {
sourceString = options.postSlug(sourceString);
}

return sourceString;
};

// Default options
$.slugify.options = {
whitespace: '-',
invalid: '',
preSlug: null,
postSlug: null,
replaceMap: {
'á': 'a',
'à': 'a',
Expand Down Expand Up @@ -614,4 +630,4 @@
}
};

}(jQuery));
}(jQuery));
18 changes: 18 additions & 0 deletions test/slugify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@
equal($('#slug-target-span').text(), 'hello-spanner', "Slug added to span correctly again");
});

test('test the preSlug postSlug callbacks', function() {

expect(2);

strictEqual($.slugify('a', {
postSlug: function(sourceString) {
return sourceString.toUpperCase();
}
}), 'A', 'Uppercase postSlug');

strictEqual($.slugify('a', {
postSlug: function(sourceString) {
return sourceString + 'rsch';
}
}), 'arsch', 'Naughty word appendend preSlug');
});


QUnit.testDone(function() {
$('#slug-target').val('');
$('#slug-source').val('');
Expand Down

0 comments on commit 1a62712

Please sign in to comment.