We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 359968d commit 45869a4Copy full SHA for 45869a4
lib/simple-slugify.js
@@ -29,4 +29,27 @@ module.exports = function (schema) {
29
30
next();
31
});
32
+
33
+ schema.pre(['findOneAndUpdate'], async function (next) {
34
+ const regex = /[&/\\#,+()$~%.'":*?<>{} ]/g;
35
36
+ let slug = this.get(this.schema.tree.slug.source)
37
+ .toString()
38
+ .toLowerCase()
39
+ .trim()
40
+ .replace(regex, '-');
41
42
+ try {
43
+ slug = (await this.model.exists({ slug }))
44
+ ? `${slug}-${crypto.randomBytes(5).toString('hex')}`
45
+ : slug;
46
+ } catch (error) {
47
+ // eslint-disable-next-line no-console
48
+ console.log(error);
49
+ }
50
51
+ this.set({ slug });
52
53
+ next();
54
+ });
55
};
0 commit comments