Skip to content

Commit

Permalink
slugit
Browse files Browse the repository at this point in the history
  • Loading branch information
chilts committed Aug 5, 2016
0 parents commit 6e0b723
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
22 changes: 22 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SlugIt #

Another slug package. Seriously.

## Synopsis ##

```js
var slugit = require('slugit')

console.log(slugit('Hello, World!'))
// -> 'hello-world'
```

## Author ##

Andrew Chilton.

## License ##

ISC.

(Ends)
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// --------------------------------------------------------------------------------------------------------------------

'use strict'

// --------------------------------------------------------------------------------------------------------------------

function slugit(title) {
title = title || ''
title = title.toLowerCase().trim()
title = title.replace(/[\'\"]/g, '')
title = title.replace(/[^0-9A-Za-z]+/g, '-')
title = title.replace(/-+/g, '-').replace(/^-/, '').replace(/-$/, '')
return title
}

// --------------------------------------------------------------------------------------------------------------------

module.exports = slugit

// --------------------------------------------------------------------------------------------------------------------
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "slugit",
"description": "Another slug package.",
"version": "1.0.0",
"scripts": {
"test": "for FILE in test/*.js; do echo \"# --- $FILE ---\"; echo; tape $FILE; done"
},
"devDependencies": {
"tape": "^4.6.0"
},
"repository": {
"type": "git",
"url": "https://github.com/chilts/slugit.git"
},
"bugs": {
"url": "https://github.com/chilts/slugit/issues",
"email": "[email protected]"
},
"author": {
"name": "Andrew Chilton",
"email": "[email protected]",
"url": "https://chilts.org/"
},
"license": "ISC"
}
34 changes: 34 additions & 0 deletions test/t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// --------------------------------------------------------------------------------------------------------------------

"use strict"

// npm
var test = require('tape')

// local
var slugit = require('../')

// --------------------------------------------------------------------------------------------------------------------

var simple = [
[ 'Simple', 'simple', ],
[ 'Hello, World!', 'hello-world', ],
[ ' a ', 'a', ],
[ '1234', '1234', ],
[ '', '', ],
[ 'The Mountains', 'the-mountains', ],
[ "Andy's Tea", 'andys-tea', ],
[ '!@#^&Expletive&@^#*@', 'expletive', ],
]

test('simple', function(t) {
t.plan(simple.length)

simple.forEach(function(row) {
t.equal(slugit(row[0]), row[1], 'Testing ' + row[0])
})

t.end()
})

// --------------------------------------------------------------------------------------------------------------------

0 comments on commit 6e0b723

Please sign in to comment.