Skip to content

Instantly share code, notes, and snippets.

@joelpalmer
Created June 29, 2015 19:59
Show Gist options
  • Save joelpalmer/7adf0c9f667c21633388 to your computer and use it in GitHub Desktop.
Save joelpalmer/7adf0c9f667c21633388 to your computer and use it in GitHub Desktop.
Slugging
function slug (text) {
var separator = /[^a-z0-9]+/ig;
var drop = /^-|-$/g;
return text
.replace(separator, '-')
.replace(drop, '')
.toLowerCase();
}
function stamp(date){
return slug(date.toDateString());
}
var post = {
title: 'Dodgers are awesome',
date: new Date()
};
var _slug = slug(post.title);
var time = stamp(post.date);
var url = '/' + time + '/' + _slug;
console.log(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment