-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
92 lines (82 loc) · 2.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var generateSlug = require('slug');
function sanitizeFieldName(val) {
val = String(val).trim();
if (val.length === 0) {
throw new Error('Invalid source');
}
return val;
}
module.exports = exports = function sluggablePlugin(schema, options) {
options = options || {};
var slug = options.field ? String(options.field) : 'slug',
unique = options.unique ? true : false,
source = options.source ? options.source : 'title',
separator = options.separator ? String(options.separator) : '-',
updatable = ((options.updatable) || (options.updatable === undefined)) ? true : false,
charmap = (options.charmap) ? options.charmap : generateSlug.charmap,
multicharmap = (options.multicharmap) ? options.multicharmap : generateSlug.multicharmap,
symbols = (options.symbols || options.symbols === undefined) ? true : false;
schema.pre('save', unique, function (next, done) {
if (updatable === false && this[slug]) {
next();
done();
return;
}
var value = '',
field = '',
errorFields = [];
if (typeof source === 'string') {
field = sanitizeFieldName(source);
errorFields.push(field);
value = String(this[field] || '').trim();
} else if (source instanceof Array) {
var array = [],
i;
for (i = 0; i < source.length; i++) {
field = sanitizeFieldName(source[i]);
errorFields.push(field);
array.push(String(this[field] || '').trim());
}
value = array.join(separator);
} else if(Object.prototype.toString.call(source) == '[object Function]') {
value = source(this, separator);
} else {
throw new Error('Source can be an array or a string');
}
value = generateSlug(value, {
replacement: separator,
lower: true,
charmap: charmap,
multicharmap: multicharmap,
symbols: symbols
});
if (value.length === 0) {
throw new Error('One of the fields is requried: ' + String(errorFields.join(', ')));
}
if (!unique) {
this[slug] = value;
next();
return;
}
var where = {},
suffix = 1,
self = this;
function findNewSlug(search) {
where[slug] = search;
where['_id'] = { '$ne': self._id };
self.constructor.findOne(where, function (err, data) {
if (err) {
throw err;
}
if (!data) {
self[slug] = search;
next();
done();
return;
}
findNewSlug(String(value) + String(separator) + String(++suffix));
});
}
findNewSlug(value);
});
};