Merge branch 'devel' of https://github.com/VulcanJS/Vulcan into devel

This commit is contained in:
SachaG 2018-10-10 17:35:03 +09:00
commit 0b78ac68f8
2 changed files with 29 additions and 16 deletions

View file

@ -168,6 +168,35 @@ Utils.slugify = function (s) {
return slug;
};
Utils.getUnusedSlug = function (collection, slug) {
let suffix = "";
let index = 0;
// test if slug is already in use
while (!!collection.findOne({slug: slug+suffix})) {
index++;
suffix = "-"+index;
}
return slug+suffix;
};
// Different version, less calls to the db but it cannot be used until we figure out how to use async for onCreate functions
// Utils.getUnusedSlug = async function (collection, slug) {
// let suffix = '';
// let index = 0;
//
// const slugRegex = new RegExp('^' + slug + '-[0-9]+$');
// // get all the slugs matching slug or slug-123 in that collection
// const results = await collection.find( { slug: { $in: [slug, slugRegex] } }, { fields: { slug: 1, _id: 0 } });
// const usedSlugs = results.map(item => item.slug);
// // increment the index at the end of the slug until we find an unused one
// while (usedSlugs.indexOf(slug + suffix) !== -1) {
// index++;
// suffix = '-' + index;
// }
// return slug + suffix;
// };
Utils.getUnusedSlugByCollectionName = function (collectionName, slug) {
return Utils.getUnusedSlug(getCollection(collectionName), slug);

View file

@ -13,20 +13,4 @@ Utils.sanitize = function(s) {
});
};
Utils.getUnusedSlug = async function (collection, slug) {
let suffix = '';
let index = 0;
const slugRegex = new RegExp('^' + slug + '-[0-9]+$');
// get all the slugs matching slug or slug-123 in that collection
const results = await Connectors.find( collection, { slug: { $in: [slug, slugRegex] } }, { fields: { slug: 1, _id: 0 } });
const usedSlugs = results.map(item => item.slug);
// increment the index at the end of the slug until we find an unused one
while (usedSlugs.indexOf(slug + suffix) !== -1) {
index++;
suffix = '-' + index;
}
return slug + suffix;
};
export { Utils };