Skip to content

Commit

Permalink
Add separator parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jledentu committed Oct 7, 2017
1 parent df6bd7a commit a9597b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Parameter | Type | Details
-------------- | ------------------- |:---------------------------------
lowercase | `boolean` | Whether or not the generated slugs are lowercased. Defaults to `true`.
blacklist | `Array<string>` | A list of reserved words to not use as slugs in your application. Defaults to `[]`.
separator | `string` | Separator to use in slugs. Defaults to `-`.

## License

Expand Down
12 changes: 8 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ function SlugsHook(sails) {

model.beforeCreate = ((previousBeforeCreate, from, blacklist) => function(values, cb) {

let slugName = slug(values[from], { toLowerCase: sails.config.slugs.lowercase });
const slugName = slug(values[from], {
toLowerCase: sails.config.slugs.lowercase,
separator: sails.config.slugs.separator
});

// Check that slug is not already used
slugCanBeUsed(slugName, modelName, name, blacklist)
.then((ok) => {
values[name] = ok ? slugName : slugName + '-' + uuid();
.then(ok => {
values[name] = ok ? slugName : slugName + sails.config.slugs.separator + uuid();

if (typeof previousBeforeCreate === 'function') {
previousBeforeCreate(values, cb);
Expand All @@ -75,7 +78,8 @@ function SlugsHook(sails) {
defaults: {
__configKey__: {
lowercase: true,
blacklist: []
blacklist: [],
separator: '-'
}
},

Expand Down
20 changes: 20 additions & 0 deletions test/unit/models/Post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ describe('PostModel', function() {
})
.catch(done);
});

it('should use separator defined in config', done => {
sails.config.slugs.separator = '_';

Post.create({
title: 'This is underscore',
content: 'Post content',
author: 'Jérémie Ledentu'
})
.then(post => {
post.should.have.property('slug');
post.slug.should.eql('this_is_underscore');
post.slugAuthor.should.eql('jeremie_ledentu');

sails.config.slugs.separator = '-';

done();
})
.catch(done);
});
});

describe('#findOneBySlug()', function() {
Expand Down
6 changes: 4 additions & 2 deletions test/unit/slugsHook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ describe('SlugsHook', function() {
it('should add default config', function(done) {
sails.config.should.have.property('slugs');
sails.config.slugs.should.have.property('lowercase');
sails.config.slugs.lowercase.should.be.eql(true);
sails.config.slugs.lowercase.should.eql(true);
sails.config.slugs.should.have.property('blacklist');
sails.config.slugs.blacklist.should.be.eql([]);
sails.config.slugs.blacklist.should.eql([]);
sails.config.slugs.should.have.property('separator');
sails.config.slugs.separator.should.eql('-');
done();
});
});

0 comments on commit a9597b0

Please sign in to comment.