slugz
Turn a string into a url-safe slug.
Basic Usage
var slugz = ; ; // => 'developing-hybrid-iphone-apps'; // => 'its-johnny'
Cleaners
You can customize the way handles are generated by using cleaners. Cleaners are added using the chainable use
method.
use(function)
Any function passed to the use
method will be called with a cleaned handle that has underscores seperating words, and is expected to return a modified handle.
For example, the following could be used to replace "yipee" with "yahoo":
var slugz = ; ; // => 'things-that-make-me-say-yahoo'
use(object)
If you need more granularity then you can pass an object to use
that can have any combination of beforeClean
, clean
, and afterClean
methods.
beforeClean
is called before the original text has been modified in any wayclean
is called after basic cleaning has occurred and the text will be underscore delimitedafterClean
is called just before the final handle is returned
For example, if you want to expand some contractions:
var slugz = ; ; // => 'that-is-all-folks'; // => 'peyton-manning-what-is-the-big-deal'
If you only need clean
then consider just passing that function to use
rather than using an object.
use(string, [params...])
There are several predefined cleaners in the "cleaners" folder that can be be added by name and configured by adding addtional arguments to the use
call.
Parenthesis
Strip parenthetical text from handles.
var slugz = ; ; // => 'save-money-on-over-the-counter-medications'
How-To
Remove the words "how to" from handles.
var slugz = ; ; // => 'build-a-hover-car'; // => 'dance-like-you-mean-it'; // => 'healthcare-headlines-deal-with-high-blood-pressure'
Max Chars
Limit the character length of the handle, truncating on word boundaries.
var slugz = ; ; // => 'your-dogs-pain'
Max Words
Limit the number of words in the handle.
var slugz = ; ; // => 'natural-ways-to-control-high'
Common Words
Remove words that would not be considered "keywords". First and last words will not be touched as well as hyphenated words.
var slugz = ; ; // => 'worst-nfl-off-seasons-2013'; // => 'the-best-both-worlds'; // => 'your-dogs-pain-medications-muscle-relaxants-dangerous-humans'
You can optionally pass your own "common words" list:
var slugz = ; ; // => 'treating-your-dog-with-medications'; // => 'staying-cool-when-outside'; // => 'how-your-dog-percieves-in-groups'
How we use it:
This is basically the logic that we want to use to generate article handles:
var Handleify = async = ; var MAX_CHARS = 100; // Handle generators, in our prefered ordervar handleifiers = ; // Handle generators to fall back to if the corresponding one above is too longvar shortenedHandleifiers = ; { var results = shortenedResults = ; // Get 7 possible handles to test for uniqueness forvar handle i = 0; i++; i < handleifierslength handle = handleifiersiheadline; ifheadlinelength <= MAX_CHARS results; else shortenedResults; // Build the list and remove duplicates results = results; //results = results.unique(); // Find the first handle that isn't already taken async;}