Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 956c1b7

Browse files
codejockieKent C. Dodds
authored and
Kent C. Dodds
committed
feat(slugify): Add a slugify function (#121)
A function to slugify text input, trims whitespaces and multiple dashes Closes #120
1 parent 1aac27a commit 956c1b7

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import isFunction from './is-function'
4040
import isOdd from './is-odd'
4141
import isNumeric from './is-numeric'
4242
import max from './max'
43+
import slugify from './slugify'
4344

4445
export {
4546
isOdd,
@@ -84,4 +85,5 @@ export {
8485
subtraction,
8586
isNumeric,
8687
max,
88+
slugify,
8789
}

src/slugify.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default slugify
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/questions/1053902/how-to-convert-a-title-to-a-url-slug-in-jquery
5+
*
6+
* This function will slugify text given to it.
7+
*
8+
* @param {string} text - The text to slugify
9+
* @return {string} - The slugified string
10+
*/
11+
function slugify(text) {
12+
return text.toString()
13+
.toLowerCase()
14+
.trim() // Trim whitespaces at start and end of text
15+
.replace(/\s+/g, '-') // Replace whitespaces with dash (-)
16+
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
17+
.replace(/\-\-+/g, '-') // Replace multiple dashes/hyphens with single dash (-)
18+
.replace(/^-+/, '') // Remove dash (-) from start of text
19+
.replace(/-+$/, '') // Remove dash (-) from end of text
20+
}

test/slugify.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import {slugify} from '../src'
3+
4+
test('slugifies a text', t => {
5+
const text = 'The world is spherical in shape'
6+
const expected = 'the-world-is-spherical-in-shape'
7+
const actual = slugify(text)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('trims whitespaces and slugifies the text', t => {
12+
const text = ' The world is spherical in shape '
13+
const expected = 'the-world-is-spherical-in-shape'
14+
const actual = slugify(text)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('it converts extra dashes to single dash', t => {
19+
const text = 'The--world---is---- spherical-- in-- shape'
20+
const expected = 'the-world-is-spherical-in-shape'
21+
const actual = slugify(text)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('removes all none word characters', t => {
26+
const text = 'The & world & is & spherical & in & shape'
27+
const expected = 'the-world-is-spherical-in-shape'
28+
const actual = slugify(text)
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('removes dashes at the start and end of text', t => {
33+
const text = '---The world is spherical in shape---'
34+
const expected = 'the-world-is-spherical-in-shape'
35+
const actual = slugify(text)
36+
t.deepEqual(actual, expected)
37+
})

0 commit comments

Comments
 (0)