mongoose-url-slugs
Advanced tools
Comparing version 0.3.0 to 1.0.0
# History | ||
## V 1.0.0 (April 18, 2017) | ||
* *BREAKING CHANGES* | ||
* Added alwaysRecreate ((https://github.com/PeterJausovec)PeterJausovec) | ||
* Added undefinedVal option. | ||
* Added additional tests ((https://github.com/phillee)phillee) | ||
## V 0.3.0 (April 18, 2017) | ||
* Added onHook option (mtimofiiv) | ||
* Added onHook option ((https://github.com/mtimofiiv)mtimofiiv) | ||
* Updated dependencies | ||
@@ -6,0 +12,0 @@ |
60
index.js
@@ -113,12 +113,13 @@ var extend = require('extend'); | ||
separator: '-', | ||
undefinedVal: 'undefined', | ||
maxLength: null, | ||
update: false, | ||
alwaysRecreate: false, | ||
index: true, | ||
index_type: String, | ||
index_default: '', | ||
index_trim: true, | ||
index_unique: true, | ||
index_required: false, | ||
index_sparse: false, | ||
recreate: false | ||
indexType: String, | ||
indexDefault: '', | ||
indexTrim: true, | ||
indexUnique: true, | ||
indexRequired: false, | ||
indexSparse: false | ||
}; | ||
@@ -136,3 +137,13 @@ | ||
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, sparse: options.index_sparse}; | ||
schemaField[options.field] = { | ||
type: options.indexType, | ||
default: options.indexDefault, | ||
trim: options.indexTrim, | ||
index: options.index, | ||
unique: options.indexUnique, | ||
required: options.indexRequired, | ||
sparse: options.indexSparse | ||
}; | ||
schema.add(schemaField); | ||
@@ -142,3 +153,3 @@ } | ||
schema.methods.ensureUniqueSlug = function(slug, cb) { | ||
if (!options.index_unique) return cb(null, slug); | ||
if (!options.indexUnique) return cb(null, slug); | ||
var doc = this, | ||
@@ -187,9 +198,8 @@ model = doc.constructor, | ||
var slugFieldsModified = doc.isNew; | ||
if (doc.recreate) { | ||
slugFieldsModified = true; | ||
} else { | ||
if (!doc.isNew && !options.update && currentSlug) { | ||
return next(); | ||
} | ||
} | ||
// Skip if it's an edit and the plugin is configured to not update. | ||
if (!doc.isNew && !options.update && currentSlug) return next(); | ||
// Skip if it's an edit and the user explicitly sets a slug and plugin is not configured to always recreate slug. | ||
else if (!doc.isNew && doc.isModified(options.field) && currentSlug && !options.alwaysRecreate) return next(); | ||
@@ -202,16 +212,20 @@ var toSlugify = ''; | ||
var slugPart = doc.get(slugField, String); | ||
if (slugPart) toSlugify += slugPart + ' '; | ||
if (slugPart !== undefined) toSlugify += slugPart + ' '; | ||
} | ||
toSlugify = toSlugify.substr(0, toSlugify.length - 1); | ||
if (toSlugify.length) toSlugify = toSlugify.substr(0, toSlugify.length - 1); | ||
else toSlugify = options.undefinedVal; | ||
} else { | ||
if (doc.isModified(slugFields)) slugFieldsModified = true; | ||
toSlugify = doc.get(slugFields, String); | ||
var slugPart = doc.get(slugFields, String); | ||
if (slugPart !== undefined) toSlugify += slugPart; | ||
if (!toSlugify.length) toSlugify = options.undefinedVal; | ||
} | ||
if (!slugFieldsModified && currentSlug) return next(); | ||
if (!options.alwaysRecreate && !slugFieldsModified && currentSlug) return next(); | ||
var newSlug = options.generator(removeDiacritics(toSlugify), options.separator); | ||
if (!newSlug.length && options.index_sparse) { | ||
if (options.indexSparse && newSlug === options.undefinedVal) { | ||
doc.set(options.field, undefined); | ||
doc.markModified(options.field); // sometimes required :) | ||
return next(); | ||
@@ -225,3 +239,3 @@ } | ||
doc.set(options.field, finalSlug); | ||
doc.markModified(options.field, finalSlug); // sometimes required :) | ||
doc.markModified(options.field); // sometimes required :) | ||
next(); | ||
@@ -228,0 +242,0 @@ }); |
@@ -9,3 +9,3 @@ { | ||
"description": "Create URL compatiable slugs on mongoose models, ensuring uniqueness.", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
@@ -12,0 +12,0 @@ "mongoose slugs", |
@@ -16,3 +16,6 @@ [![Build Status](https://travis-ci.org/talha-asad/mongoose-url-slugs.svg?branch=master)](https://travis-ci.org/talha-asad/mongoose-url-slugs) | ||
``` | ||
## V1 Breaking Changes | ||
Option keys are now all Camel case, as opposed to previously. | ||
## What is a Slug? | ||
@@ -75,3 +78,3 @@ | ||
## Default options | ||
## Options and defaults | ||
@@ -81,12 +84,14 @@ * **field** (Default: 'slug') - Slug field to use for storage. | ||
* **separator** (Default: '-') - Separator to use for invalid characters. | ||
* **generator(text, separator)** (Default: lowercases and then replaces all non alphanumeric characters to seperator) - Function to generate slug. | ||
* **generator(text, separator)** (Default: lowercases and replaces all non alphanumeric to seperator) - Function to generate slug. | ||
* **undefinedVal** (Default: 'undefined') - Uses this string when slug dependent fields don't exist. | ||
* **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. | ||
* **index_default** (Default: '') - Mongoose schema slug index default value. | ||
* **index_trim** (Default: True) - Mongoose schema slug index trim value. | ||
* **index_unique** (Default: True) - Mongoose schema slug index unique value. | ||
* **index_required** (Default: True) - Mongoose schema slug index required value. | ||
* **index_sparse** (Default: False) - Mongoose schema slug index sparse value. | ||
* **update** (Default: False) - Update slug when dependent fields change. | ||
* **alwaysRecreate** (Default: False) - If true, will recreate slug regardless of change on dependent fields. | ||
* **index** (Default: True) - Mark slug field as an index in mongoose schema. | ||
* **indexType** (Default: String) - Mongoose schema slug index type. | ||
* **indexDefault** (Default: '') - Mongoose schema slug index default value. | ||
* **indexTrim** (Default: True) - Mongoose schema slug index trim value. | ||
* **indexUnique** (Default: True) - Mongoose schema slug index unique value. | ||
* **indexRequired** (Default: True) - Mongoose schema slug index required value. | ||
* **indexSparse** (Default: False) - Mongoose schema slug index sparse value. | ||
* **onHook** (Default: 'validate') - Mongoose document hook to update slug. | ||
@@ -93,0 +98,0 @@ |
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21249
215
0
118