Skip to content

Commit

Permalink
Merge branch 'release/3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jledentu committed Oct 7, 2017
2 parents fa0df78 + d0e8121 commit deab05c
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 49 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"node": 4
}
}]
]
],
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

config/local.js
dist

.nyc_output



Expand Down
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ addons:
deploy:
provider: npm
email: jledentu@gmail.com
api_key:
secure: HaUooYs1t9OSYUD7sRkFm3uR307r+Ze0qx59E3tlBCp3Cm86yt1k3dRR7FqexDt6Gspn5wR1m59JfYtJ3a3137CryTCDjEGmBfpFBWb7+e7wg/NiSNVbyH+R8vk3kxEhv7Llzv8dKk/ZOiMyEMGyynd6GmUtAanmh2N/I/bEpq+KfVySdxFKBN+6iL0ppjB0Kq7TkYZ3IrGTkHH2BsAXZPinMFapeG1WVUkM3yrNmRZiaBRb5Kvxn74dz2BfoRybY9Am9oIgOZCjqmhio/rdTLSQ03/Hgs4zEkUKHbbDkNi9amqF1LR9TmA7a3Z9cnPtMX7kTo0N8iOml0G5qBdmdMJrqaeHU3n4DHV6PjUq18qJaJngdbAmPnr5YaUzZzzp3ESK/NlWv76O50WkLN2t5SzaS39a4kRzJfPKbJRjfrmak/AGUpYxKilhnbeGKWIEWhyQjUlA2ekUqChCDpsn8SHBZnZTvYf4tioYhN1kVNYY9EroygUAGgw3HigIgGK982hY55TVdTQO+3pl2LPwarqbQMqNHdAVnbiODKtr7wukieyuRo/lBMFj+aANqZdbw9VDdve1iTChvhDEHZyBl7IUJTWOp6ajzrKFmYSpNyFw5rSi9ur3k3knqAr3NqQlNHp6iBR+JwlGQPs/cqtUpyz5ZDFb5Asm1DEzms/uTlU=
on:
branch: master
tags: true
repo: jledentu/sails-hook-slugs
repo: jledentu/sails-hook-slugs
api-key:
secure: XBmb8MNoFm31nOWd8N0GMEnSZT6kas9r81FpnF4v1jGvoIO9yku2WoKo7pop2IIIXD4GLVzCdpOpo+K5+a+UV/O4tVqrlHHD3s0+4+lgRvFlAVpWbwO186Patsof8TSmz2l9jYcN7f9V7s/+msuA/BDAZD2G1MV1khM8603j21oCVjWojpmfmhM7o31UVWMPJP4zt6Li1K/Qdrvk+UToD22ydy2jq/ZM2CCF2FFyKIhIlY5SyXunqpBiXEVFDW/cep5xnJfD6y8gC/kqnM895oUJrFhH3VS/OQ5l0QetPPhxFqC5a+DETKlNucSIemI4c5xFon5wKngEjy47bmxNebJb+Bi5oOflDQDv0sLdmyNVwLXS+XLZmKQ/4c0Aq4Q4uNHbg3bkzfmaRxp6NBtJYxkMR3RrDQy51TtVwbM6EraADKD9mFvo7KkLqTmFqdTBRuIQxMgK9ZQg1yvEtPWO0z0SBG/tIqvXY5cBP8hkeDKD7Nu88DX2uQ56SOEsHnAzC8Yr3ZUuLTjHjhNTeS9cymiYoW3ClLzMBh5u0A2LndCs1AtW7S6mq0c49wLjaKkunmF3KmH3klFvhtqQ+KnlTd8ZZEjKcWLKmZYSoTMCJrXu76+AGaMEx0P5cD6m+Z47cxXygUJpL2S1aDB0UQZQJ5pbLbOYCyUOGmU6kZkX4BQ=
after_success: npm run coverage
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [3.0.0] - 2017-10-08
### Added
Add property `separator` in `sails.config.slugs` to specify separator to use in slugs.
### Changed
- Replace slug by slugg
- Indicate code coverage

## [2.1.0] - 2017-03-29
### Added
Add property `blacklist` in `sails.config.slugs` and model attributes to exclude names from slugs.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# sails-hook-slugs

[![Build Status](https://travis-ci.org/jledentu/sails-hook-slugs.svg?branch=master)](https://travis-ci.org/jledentu/sails-hook-slugs) [![dependencies Status](https://david-dm.org/jledentu/sails-hook-slugs/status.svg)](https://david-dm.org/jledentu/sails-hook-slugs) [![npm](https://img.shields.io/npm/v/sails-hook-slugs.svg)](https://www.npmjs.com/package/sails-hook-slugs)
[![Build Status](https://travis-ci.org/jledentu/sails-hook-slugs.svg)](https://travis-ci.org/jledentu/sails-hook-slugs) [![Coverage Status](https://coveralls.io/repos/github/jledentu/sails-hook-slugs/badge.svg)](https://coveralls.io/github/jledentu/sails-hook-slugs) [![dependencies Status](https://david-dm.org/jledentu/sails-hook-slugs/status.svg)](https://david-dm.org/jledentu/sails-hook-slugs) [![npm](https://img.shields.io/npm/v/sails-hook-slugs.svg)](https://www.npmjs.com/package/sails-hook-slugs)

This [Sails.js](https://github.com/balderdashy/sails) hook generates URL-friendly slugs in your models.

Expand Down 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
21 changes: 10 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import slug from 'slug';
import slug from 'slugg';
import uuid from 'uuid/v4';

function SlugsHook(sails) {
Expand All @@ -21,7 +21,7 @@ function SlugsHook(sails) {
return resolve(false);
}

let criteria = {};
const criteria = {};
criteria[attributeName] = slugName;

sails.models[modelName].count(criteria)
Expand All @@ -46,17 +46,15 @@ function SlugsHook(sails) {

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

let slugName = slug(values[from], {lower: 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) => {
if (ok) {
values[name] = slugName;
}
else {
values[name] = slugName + '-' + uuid();
}
.then(ok => {
values[name] = ok ? slugName : slugName + sails.config.slugs.separator + uuid();

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

Expand Down
26 changes: 17 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "sails-hook-slugs",
"version": "2.1.0",
"version": "3.0.0",
"description": "A hook for Sails to handle slugs in your models.",
"main": "dist/lib/index.js",
"scripts": {
"babel": "babel lib --out-dir dist/lib",
"pretest": "npm run babel",
"test": "mocha test/bootstrap.test.js test/unit/*.test.js test/unit/**/*.test.js --compilers js:babel-core/register",
"test": "cross-env NODE_ENV=test nyc --reporter=text mocha test/bootstrap.test.js test/unit/*.test.js test/unit/**/*.test.js --compilers js:babel-core/register",
"prepublish": "npm run babel",
"lint": "eslint lib"
"lint": "eslint lib",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"keywords": [
"sails",
Expand All @@ -20,18 +20,26 @@
"sails": {
"isHook": true
},
"nyc": {
"sourceMap": false,
"instrument": false
},
"devDependencies": {
"babel-cli": "^6.2.0",
"babel-core": "^6.2.1",
"babel-plugin-istanbul": "^4.1.5",
"babel-preset-env": "^1.2.2",
"eslint": "^3.18.0",
"mocha": "^2.3.4",
"sails": "^0.11.3",
"coveralls": "^3.0.0",
"cross-env": "^5.0.5",
"eslint": "^4.8.0",
"mocha": "^4.0.1",
"nyc": "^11.2.1",
"sails": "^0.12.14",
"sails-disk": "^0.10.8",
"should": "^7.1.1"
"should": "^13.1.1"
},
"dependencies": {
"slug": "^0.9.1",
"slugg": "^1.2.1",
"uuid": "^3.0.1"
}
}
2 changes: 1 addition & 1 deletion test/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before(function(done) {
level: 'info'
},
hooks: {
slugs: require('../'),
slugs: require('../lib'),
grunt: false,
views: false
},
Expand Down
68 changes: 48 additions & 20 deletions test/unit/models/Post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ describe('PostModel', function() {
})
.then(function(post) {
post.should.have.property('title');
post.title.should.be.a.String();
post.title.should.be.eql('This is a new post!!!');
post.title.should.eql('This is a new post!!!');

post.should.have.property('author');
post.author.should.be.a.String();
post.author.should.be.eql('Jérémie Ledentu');
post.author.should.eql('Jérémie Ledentu');

post.should.have.property('slug');
post.slug.should.be.a.String();
post.slug.should.be.eql('this-is-a-new-post');
post.slug.should.eql('this-is-a-new-post');

post.should.have.property('slugAuthor');
post.slugAuthor.should.be.a.String();
post.slugAuthor.should.be.eql('jeremie-ledentu');
post.slugAuthor.should.eql('jeremie-ledentu');
done();
})
.catch(done);
Expand All @@ -38,19 +34,15 @@ describe('PostModel', function() {
})
.then(function(post) {
post.should.have.property('title');
post.title.should.be.a.String();
post.title.should.be.eql('This is a new post!!');
post.title.should.eql('This is a new post!!');

post.should.have.property('author');
post.author.should.be.a.String();
post.author.should.be.eql('Jérémie Ledentu');
post.author.should.eql('Jérémie Ledentu');

post.should.have.property('slug');
post.slug.should.be.a.String();
post.slug.should.match(/^this-is-a-new-post-/);

post.should.have.property('slugAuthor');
post.slugAuthor.should.be.a.String();
post.slugAuthor.should.match(/^jeremie-ledentu-/);
done();
})
Expand All @@ -65,11 +57,9 @@ describe('PostModel', function() {
})
.then((post) => {
post.should.have.property('slug');
post.slug.should.be.a.String();
post.slug.should.match(/^new-/);

post.should.have.property('author');
post.author.should.be.a.String();
post.slugAuthor.should.match(/^jeremie-ledentu-/);

done();
Expand All @@ -88,6 +78,46 @@ describe('PostModel', function() {
done();
});
});

it('should not use lowercase if lowercase === false in the config', done => {
sails.config.slugs.lowercase = false;

Post.create({
title: 'THIS IS A TITLE IN UPPERCASE',
content: 'Post content',
author: 'Jérémie Ledentu'
})
.then(post => {
post.should.have.property('slug');
post.slug.should.eql('THIS-IS-A-TITLE-IN-UPPERCASE');
post.slugAuthor.should.match(/^Jeremie-Ledentu-/);

sails.config.slugs.lowercase = true;

done();
})
.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 All @@ -101,10 +131,8 @@ describe('PostModel', function() {
Post.findOneBySlug('this-is-a-new-post')
.then(function(post) {
post.should.have.property('title');
post.title.should.be.a.String();
post.title.should.be.eql('This is a new post!!!');
post.slug.should.be.a.String();
post.slug.should.be.eql('this-is-a-new-post');
post.title.should.eql('This is a new post!!!');
post.slug.should.eql('this-is-a-new-post');
done();
})
.catch(done);
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 deab05c

Please sign in to comment.