├── .npmignore ├── .gitignore ├── README.md ├── package.json ├── license.txt ├── slugs.js └── test └── basic-tests.js /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | *.tgz 10 | 11 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slugs for Node.JS 2 | 3 | Pure JavaScript slug generator for Node.JS applications that need to use slugs. 4 | 5 | ## Usage 6 | 7 | 8 | var slugs = require("slugs") 9 | 10 | console.log(slugs('Hi there! How are you!')); 11 | //Writes hi-there-how-are-you to console 12 | 13 | 14 | ## Notes 15 | Currently deletes unicode characters altogether, rather than convert them to URL-friendly chars. 16 | 17 | ## Slugs on NPM 18 | You can install node-slugs via NPM, like so: 19 | 20 | npm install slugs 21 | 22 | ## License 23 | Licensed under Apache 2.0, see license.txt for details. 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "slugs" 3 | , "description": "pure JavaScript url slug generator for Node.JS with minimal dependencies" 4 | , "version": "0.1.3" 5 | , "author": "Aaron Stannard " 6 | , "contributors": [ "Timothy Strimple " ] 7 | , "devDependencies": { 8 | "nodeunit":"0.6.4" 9 | } 10 | ,"repository": { 11 | "type": "git", 12 | "url": "git@github.com:Aaronontheweb/node-slugs.git" 13 | } 14 | , "homepage": "https://github.com/Aaronontheweb/node-slugs" 15 | , "main": "slugs.js" 16 | , "keywords": ["slug", "slugs", "url"] 17 | } -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright 2011-2012 Aaron Stannard 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /slugs.js: -------------------------------------------------------------------------------- 1 | /* 2 | * SLUGS MODULE 3 | * By Aaron Stannard (aaron@stannardlabs.com) 4 | */ 5 | 6 | var slug = module.exports = function slug (incString, separator, preserved) { 7 | var p = ['.', '=', '-']; 8 | var s = '-'; 9 | 10 | if(typeof preserved != 'undefined') { 11 | p = preserved; 12 | } 13 | 14 | if(typeof separator != 'undefined') { 15 | s = separator; 16 | } 17 | 18 | return incString.toLowerCase(). 19 | replace(/ü/g, 'ue'). 20 | replace(/ä/g, 'ae'). 21 | replace(/ö/g, 'oe'). 22 | replace(/ß/g, 'ss'). 23 | replace(new RegExp('[' + p.join('') + ']', 'g'), ' '). // replace preserved characters with spaces 24 | replace(/-{2,}/g, ' '). // remove duplicate spaces 25 | replace(/^\s\s*/, '').replace(/\s\s*$/, ''). // trim both sides of string 26 | replace(/[^\w\ ]/gi, ''). // replaces all non-alphanumeric with empty string 27 | replace(/[\ ]/gi, s); // Convert spaces to dashes 28 | } 29 | -------------------------------------------------------------------------------- /test/basic-tests.js: -------------------------------------------------------------------------------- 1 | /* 2 | * BASIC UNIT TESTS 3 | */ 4 | 5 | var slugs = require('../slugs'); 6 | 7 | exports['should-not-modify-string'] = function (test) { 8 | test.equal(slugs('thisisasafestring'), 'thisisasafestring'); 9 | test.done(); 10 | }; 11 | 12 | exports['should-replace-spaces-but-nothing-else'] = function(test){ 13 | test.equal(slugs('this is a string with spaces'), 'this-is-a-string-with-spaces'); 14 | test.done(); 15 | }; 16 | 17 | exports['should-replace-periods-with-hashes'] = function(test){ 18 | //Shouldn't allow trailing hash 19 | test.equal(slugs('thisisastring.with.aperiod.'), 'thisisastring-with-aperiod'); 20 | test.done(); 21 | } 22 | 23 | exports['should-not-have-forward-or-trailing-hash'] = function(test){ 24 | test.equal(slugs(' space in front and in back '),'space-in-front-and-in-back'); 25 | test.done(); 26 | } 27 | 28 | exports['should-convert-everything-to-lower-case'] = function(test){ 29 | test.equal(slugs('This should all be in LowEr Case'),'this-should-all-be-in-lower-case'); 30 | test.done(); 31 | } 32 | 33 | exports['should-preserve-numbers-and-characters'] = function(test){ 34 | test.equal(slugs('Please keep m4h numb3r5'),'please-keep-m4h-numb3r5'); 35 | test.done(); 36 | } 37 | 38 | exports['should-not-contain-duplicate-hashes-for-multiple-spaces'] = function(test){ 39 | test.equal(slugs('SHOULD-LOOK-NORMAL------'), 'should-look-normal'); 40 | test.done(); 41 | } 42 | 43 | exports['should-not-include-unsafe-characters'] = function(test){ 44 | test.equal(slugs('%^T%^~!@##$$#%^$^/?????.'), 't'); 45 | test.done(); 46 | } 47 | 48 | exports['should-override-separator-character'] = function(test){ 49 | test.equal(slugs('this is a string with spaces', '+'), 'this+is+a+string+with+spaces'); 50 | test.done(); 51 | } 52 | 53 | exports['should-override-preserved-characters'] = function(test){ 54 | test.equal(slugs('this.is=a-string.without=spaces', '-', []), 'thisisastringwithoutspaces'); 55 | test.done(); 56 | } --------------------------------------------------------------------------------