Skip to content

Commit

Permalink
use opts.mode to specify flavour
Browse files Browse the repository at this point in the history
  • Loading branch information
dodo committed Oct 9, 2014
1 parent bff002a commit 9ca9fd7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ slug('string', [{options} || 'replacement']);
```

```javascript
slug.defaults = {
slug.defaults.mode ='rfc3986';
slug.defaults.modes['rfc3986'] = {
replacement: '-', // replace spaces with replacement
symbols: true, // replace unicode symbols or not
remove: null, // (optional) regex to remove characters
charmap: slug.charmap, // replace special characters
multicharmap: slug.multicharmap // replace multi-characters
};
slug.defaults.modes['pretty'] = {
replacement: '-',
symbols. true,
remove: /[.]/g,
charmap: slug.charmap,
multicharmap: slug.multicharmap
};
```

[![Build Status](https://secure.travis-ci.org/dodo/node-slug.png)](http://travis-ci.org/dodo/node-slug)
Expand Down
7 changes: 0 additions & 7 deletions seo.js

This file was deleted.

42 changes: 32 additions & 10 deletions slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ function slug(string, opts) {
string = string.toString();
if ('string' === typeof opts)
opts = {replacement:opts};
opts.replacement = opts.replacement || slug.defaults.replacement;
opts.multicharmap = opts.multicharmap || slug.defaults.multicharmap;
opts.charmap = opts.charmap || slug.defaults.charmap;
opts.remove = opts.remove || slug.defaults.remove;
opts.mode = opts.mode || slug.defaults.mode;
var defaults = slug.defaults.modes[opts.mode];
['replacement','multicharmap','charmap','remove'].forEach(function (key) {
opts[key] = opts[key] || defaults[key];
});
if ('undefined' === typeof opts.symbols)
opts.symbols = slug.defaults.symbols;
opts.symbols = defaults.symbols;
var lengths = [];
Object.keys(opts.multicharmap).forEach(function (key) {
var len = key.length;
Expand Down Expand Up @@ -60,9 +61,7 @@ function slug(string, opts) {
};

slug.defaults = {
replacement: '-',
symbols: true,
remove: null,
mode: 'rfc3986',
};

slug.multicharmap = slug.defaults.multicharmap = {
Expand Down Expand Up @@ -141,16 +140,39 @@ slug.charmap = slug.defaults.charmap = {
'<': 'less', '>': 'greater',
};

slug.defaults.modes = {
rfc3986: {
replacement: '-',
symbols: true,
remove: null,
charmap: slug.defaults.charmap,
multicharmap: slug.defaults.multicharmap,
},
pretty: {
replacement: '-',
symbols: true,
remove: /[.]/g,
charmap: slug.defaults.charmap,
multicharmap: slug.defaults.multicharmap,
},
};

// Be compatible with different module systems

if (typeof define !== 'undefined' && define.amd) { // AMD
slug.defaults.symbols = false; // dont load symbols table in the browser
// dont load symbols table in the browser
Object.keys(slug.defaults.modes).forEach(function (key) {
slug.defaults.modes[key].symbols = false;
});
define([], function () {return slug});
} else if (typeof module !== 'undefined' && module.exports) { // CommonJS
symbols(); // preload symbols table
module.exports = slug;
} else { // Script tag
slug.defaults.symbols = false; // dont load symbols table in the browser
// dont load symbols table in the browser
Object.keys(slug.defaults.modes).forEach(function (key) {
slug.defaults.modes[key].symbols = false;
});
root.slug = slug;
}

Expand Down

0 comments on commit 9ca9fd7

Please sign in to comment.