mongoose-url-slugs
Advanced tools
Comparing version 0.0.10 to 0.1.0
39
index.js
@@ -16,2 +16,3 @@ var extend = require('extend'); | ||
separator: '-', | ||
maxLength: null, | ||
update: false, | ||
@@ -23,16 +24,17 @@ index: true, | ||
index_unique: true, | ||
index_required: false | ||
index_required: false, | ||
index_sparse: false | ||
}; | ||
module.exports = function(slugFields, options) { | ||
options = extend(true, defaultOptions, options); | ||
if (slugFields.indexOf(' ') > -1) { | ||
slugFields = slugFields.split(' '); | ||
} | ||
return (function (schema) { | ||
if (options.addField) { | ||
var schemaField = {}; | ||
schemaField[options.field] = {type: options.index_type, default: options.index_default, trim: options.index_trim, index: options.index, unique: options.index_unique, required: options.index_required}; | ||
schemaField[options.field] = {type: options.index_type, default: options.index_default, trim: options.index_trim, index: options.index, unique: options.index_unique, required: options.index_required, sparse: options.index_sparse}; | ||
schema.add(schemaField); | ||
@@ -47,3 +49,3 @@ } | ||
q[options.field] = new RegExp('^' + slug); | ||
q['_id'] = {$ne: doc._id}; | ||
q._id = {$ne: doc._id}; | ||
var fields = {}; | ||
@@ -60,13 +62,17 @@ fields[options.field] = 1; | ||
count = docSlug.match(new RegExp(slug + options.separator + '([0-9]+)$')); | ||
count = ((count instanceof Array)? parseInt(count[1]) : 0) + 1; | ||
count = ((count instanceof Array)? parseInt(count[1], 10) : 0) + 1; | ||
} | ||
return (count > max)? count : max; | ||
}, 0); | ||
if (max == 1) cb(null, false, slug + options.separator + (max + 1)); // avoid slug-1, rather do slug-2 | ||
else if (max > 0) cb(null, false, slug + options.separator + max); | ||
else cb(null, false, slug); | ||
if (max == 1) max++; // avoid slug-1, rather do slug-2 | ||
var suffix = options.separator + max; | ||
if (options.maxLength) return cb(null, false, slug.substr(0, options.maxLength - suffix.length) + suffix); | ||
else return cb(null, false, slug + suffix); | ||
} | ||
}); | ||
}; | ||
schema.statics.findBySlug = function (slug, fields, additionalOptions, cb) { | ||
@@ -82,3 +88,3 @@ var q = {}; | ||
if (!doc.isNew && !options.update && currentSlug) return next(); | ||
var slugFieldsModified = doc.isNew? true : false; | ||
@@ -98,6 +104,9 @@ var toSlugify = ''; | ||
} | ||
if (!slugFieldsModified) return next(); | ||
var newSlug = options.generator(toSlugify, options.separator); | ||
if (options.maxLength) newSlug = newSlug.substr(0, options.maxLength); | ||
doc.ensureUniqueSlug(newSlug, function (e, exists, finalSlug) { | ||
@@ -109,5 +118,5 @@ if (e) return next(e); | ||
}); | ||
}); | ||
}); | ||
}; |
@@ -9,3 +9,3 @@ { | ||
"description": "Create URL compatiable slugs on mongoose models, ensuring uniqueness.", | ||
"version": "0.0.10", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -30,3 +30,11 @@ "mongoose slugs", | ||
"extend": "2.0.0" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/_mocha test" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.10.0", | ||
"mocha": "^2.0.1", | ||
"mongoose": "^3.8.19" | ||
} | ||
} | ||
} |
@@ -0,4 +1,10 @@ | ||
[![Build Status](https://travis-ci.org/mindblaze/mongoose-url-slugs.png?branch=master)](https://travis-ci.org/mindblaze/mongoose-url-slugs) | ||
[![Dependency Status](https://www.versioneye.com/user/projects/5478679560944d12df000046/badge.svg)](https://www.versioneye.com/user/projects/5478679560944d12df000046) | ||
[![NPM version](https://badge.fury.io/js/mongoose-url-slugs.svg)](http://badge.fury.io/js/mongoose-url-slugs) | ||
[![NPM stats](https://nodei.co/npm/mongoose-url-slugs.png?downloads=true)](https://www.npmjs.org/package/mongoose-url-slugs) | ||
# Mongoose URL Slugs | ||
A simple URL based slugs generation for mongoose models. | ||
A simple URL based slug generator for mongoose models. | ||
@@ -52,8 +58,9 @@ | ||
* **field** (Default: 'slug') - Parts that are uploaded simultaneously. | ||
* **field** (Default: 'slug') - field to use for slug building/ | ||
* **addField** (Default: True) - Add slug field to mongoose schema. | ||
* **separator** (Default: '-') - Separator to use for invalid characters. | ||
* **generator(text, separator)** (Default: lowercases and then replaces all alphanumeric characters to separators) - Function to generate slug. | ||
* **update** (Default: false) - Update slug when slug building fields change. | ||
* **index** (Default: true) - Mark slug field as index in mongoose schema. | ||
* **generator(text, separator)** (Default: lowercases and then replaces all non alphanumeric characters to seperator) - Function to generate slug. | ||
* **maxLength** (Default: null) - If set, restricts slug length to specified value. | ||
* **update** (Default: False) - Update slug when slug building fields change. | ||
* **index** (Default: True) - Mark slug field as index in mongoose schema. | ||
* **index_type** (Default: String) - Mongoose schema slug index type. | ||
@@ -64,6 +71,7 @@ * **index_default** (Default: '') - Mongoose schema slug index default value. | ||
* **index_required** (Default: True) - Mongoose schema slug index required value. | ||
* **index_sparse** (Default: False) - Mongoose schema slug index sparse value. | ||
## History | ||
* v0.1.0 (2014-11-28) -- Added index_sparse, maxLength options. | ||
* v0.0.10 (2014-10-14) -- Dependencies updated. | ||
@@ -70,0 +78,0 @@ * v0.0.9 (2014-06-14) -- findBySlug bug fixed. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
9992
5
0
97
104
3