Created
May 14, 2017 16:02
-
-
Save singhmohancs/87382ce718d7b0b0d71011177ca0251a to your computer and use it in GitHub Desktop.
AngularJS - Slugify Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular | |
.module('app') | |
.filter('slugify', Filter); | |
function Filter() { | |
return function (input) { | |
if (!input) | |
return; | |
// make lower case and trim | |
var slug = input.toLowerCase().trim(); | |
// replace invalid chars with spaces | |
slug = slug.replace(/[^a-z0-9\s-]/g, ' '); | |
// replace multiple spaces or hyphens with a single hyphen | |
slug = slug.replace(/[\s-]+/g, '-'); | |
return slug; | |
}; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="{{post._id}}/{{post.title | slugify}}">{{post.title}}</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment