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

Added unique option #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
41 changes: 41 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"esnext": true,
"globalstrict": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true,
"validthis": true,
"globals": {
"console": false,
"angular": false,
// Angular Mocks
"inject": false,
"module": false,
// JASMINE
"describe": false,
"xdescribe": false,
"it": false,
"xit": false,
"before": false,
"beforeEach": false,
"after": false,
"afterEach": false,
"expect": false,
"require" : false,
"gulp" : false
}
}
29 changes: 29 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $ npm install mongoose-slug

## Usage

#### Different field
```js
var slug = require('mongoose-slug');
schema.plugin(slug('name'));
Expand All @@ -21,6 +22,8 @@ song.name = 'frank ab';
song.slug; // > frank-ab
```

#### Multiple Candidated

To use different slug candidates pass them as array

```js
Expand All @@ -36,6 +39,32 @@ person.slug; // > john-doe

````

#### Unique

To create unique slugs add the `unique: true` options to the slug:

```js
var slug = require('mongoose-slug');
schema.plugin(slug(null,{unique: true})
var Song = mongoose.model('Song', schema);

var song = new Song(title : 'Redemption Song');

song.save(function(err, song){
song.slug; // >redemption-song
});

var newSong = new Song(title : 'Redemption Song');

newSong.save(function(err, song){
song.slug; // >redemption-song-2
});
```
This can be used within the other options

### TODO
- Refactor the tests

## License

MIT
46 changes: 37 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

'use strict';
/**
* deps
*/
Expand All @@ -17,14 +17,18 @@ var slug = require('speakingurl');
* - `.replace` characters to replace defaulted to `[^a-zA-Z]`
* - `.separator` separator to use, defaulted to `-`
* - `required` whether a slug is required, defaults to `true`
* - `unique` whether the slug should be unique, defaults to `false`
*
* @param {String} prop
* @param {Object} options
* @return {Function}
*/

module.exports = function(prop, opts){
return (function slugize(schema){
return function slugize(schema){

var unique = (opts && opts.unique) ? true : false;

var title;
schema.add({
slug: {
Expand All @@ -50,16 +54,40 @@ module.exports = function(prop, opts){
title = this[prop || 'title'];
}

var require = (opts && opts.required === false) ? false : true
, presets = (opts && opts.override) ? true : false;
var require = (opts && opts.required === false) ? false : true;

if (require && !title) return next(new Error(prop + ' is required to create a slug'));
if (require && !title) {
return next(new Error(prop + ' is required to create a slug'));
}

var mySlug = slug(title, opts);
if (opts && opts.track && self.slugs && self.slugs.indexOf( mySlug) == -1) self.slugs.push( mySlug );
if (title && !self.slug) self.slug = mySlug;
var isChanged = self.isNew || (self.slug !== mySlug);

if(!isChanged){
return next();
}

if (opts && opts.track && self.slugs && self.slugs.indexOf( mySlug) === -1){
self.slugs.push( mySlug );
}

if (title && !self.slug){
self.slug = mySlug;
}

if(unique){

this.constructor.count({slug: self.slug}, function(err, number){
if(number >= 1 && self.isNew){
self.slug = self.slug + '-' + number;
}
next();
});
}
else{
next();
}

next();
});
});
};
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
"scripts": {
"test": "mocha"
},
"version": "1.3.1",
"version": "1.3.2",
"dependencies": {
"speakingurl": "~0.8.4"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"mongoose": "*"
"mockgoose": "^2.0.1",
"mongoose": "*",
"should": "*"
}
}
109 changes: 89 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
'use strict';

var mongoose = require('mongoose')
, should = require('should')
, Schema = mongoose.Schema
, model = mongoose.model.bind(mongoose)
, slug = require('..')
, to = require('./db');
var mongoose = require('mongoose');
var should = require('should');
var schema = mongoose.Schema;
var model = mongoose.model.bind(mongoose);
var slug = require('..');
var to = require('./db');

// Defining models

var artistSchema = schema({ title: String, baz: String }).plugin(slug());
var Artist = model('Artist', artistSchema);

var thingSchema = schema({ title: String, baz: String }).plugin(slug());
var Thing = model('Thing', thingSchema);

var personSchema = schema({ name: String, occupation: String }).plugin(slug(['name', 'occupation']));
var Person = model('Person', personSchema);

var uniqueSchema = schema({title: String, foo: String}).plugin(slug(null,{unique: true}));
var Unique = model('Unique', uniqueSchema);

var uniqueCustomSchema = schema({foo: String, bar: String}).plugin(slug(['foo','bar'],{unique: true}));
var uniqueCustom = model('UniqueCustom', uniqueCustomSchema);

describe('mongoose-slug', function(){

before(function(){
before(function(done){
mongoose.connect(to);

// TODO
// refactor this cleaning

model('Artist').remove({}, function(){
model('Thing').remove({}, function(){
model('Person').remove({}, function(){
model('Unique').remove({}, function(){
model('UniqueCustom').remove({}, function(){
done();
});
});
});
});
});
});

it('should create the slug with default source property(title)', function(done){
var schema = Schema({ title: String, baz: String }).plugin(slug())
, Artist = model('Artist', schema);

new Artist({ title: 'some artist'})
.save(function(err, doc){
Expand All @@ -26,8 +57,6 @@ describe('mongoose-slug', function(){
});

it('should create the slug with utf8 converted into latin chars', function(done){
var schema = Schema({ title: String, baz: String }).plugin(slug())
, Thing = model('Thing', schema);

new Thing({ title: 'Schöner Titel läßt grüßen!? Bel été !'})
.save(function(err, doc){
Expand All @@ -39,8 +68,6 @@ describe('mongoose-slug', function(){
});

it('should create the slug with multiple source property', function(done){
var personSchema = Schema({ name: String, occupation: String }).plugin(slug(['name', 'occupation']))
, Person = model('Person', personSchema);

new Person({ name: 'John Doe', occupation: 'Scam Artist'})
.save(function(err, doc){
Expand All @@ -52,14 +79,56 @@ describe('mongoose-slug', function(){
});
});

after(function(done){
model('Artist').remove({
title: 'some artist'
}, function () {
model('Person').remove({
name: 'John Doe'
}, done)
it('should create a unique slug', function(done){

new Unique({title: 'Test Title'})
.save(function(err, doc){
doc.slug.should.eql('test-title');

new Unique({title: 'Test Title'})
.save(function(err, unique){
if(err){
console.log(err);
}
unique.slug.should.eql('test-title-1');
done();
});

});
});

it('should not change the slug when updating document with unchanged title', function(done){
new Unique({title: 'Title'})
.save(function(err, doc){
doc.slug.should.eql('title');

Unique.findOne({title: 'Title'}, function(err, doc){
doc.save(function(err, newDoc){
newDoc.slug.should.eql('title');
done();
});
});
});
});

it('should create a unique slug from custom field', function(done){

new uniqueCustom({foo: 'Foo', bar: 'Bar'})
.save(function(err, doc){
if(err){
console.log(err);
}
doc.slug.should.eql('foo-bar');

new uniqueCustom({foo: 'Foo', bar: 'Bar'})
.save(function(err, unique){
if(err){
console.log(err);
}
unique.slug.should.eql('foo-bar-1');
done();
});

});
});
});