@mac777/project-pinecone-models 1.1.24 → 1.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/Event.js +34 -20
  2. package/package.json +1 -1
  3. package/src/Event.ts +38 -23
package/dist/Event.js CHANGED
@@ -288,33 +288,47 @@ eventSchema.pre('save', async function (next) {
288
288
  if (this.isModified('title') && !this.slug) {
289
289
  if (this.title) {
290
290
  // Convert to lowercase, replace spaces with hyphens, remove special characters
291
- let slug = this.title
291
+ let baseSlug = this.title
292
292
  .toLowerCase()
293
+ .trim()
293
294
  .replace(/[^\w\s-]/g, '') // Remove special characters except spaces and hyphens
294
295
  .replace(/\s+/g, '-') // Replace spaces with hyphens
295
296
  .replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
296
- .trim();
297
- // Ensure slug is not empty and add random suffix if needed
298
- if (!slug)
299
- slug = 'event';
300
- // Add timestamp suffix to ensure uniqueness
301
- const timestamp = Date.now().toString(36);
302
- slug = `${slug}-${timestamp}`;
297
+ .replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
298
+ // Ensure slug is not empty
299
+ if (!baseSlug)
300
+ baseSlug = 'event';
301
+ // Truncate to reasonable length (50 chars) to keep URLs clean
302
+ if (baseSlug.length > 50) {
303
+ baseSlug = baseSlug.substring(0, 50).replace(/-+$/, ''); // Remove trailing hyphen after truncation
304
+ }
305
+ // Try base slug first
306
+ let slug = baseSlug;
307
+ let counter = 1;
308
+ let isUnique = false;
309
+ // Check uniqueness and add counter if needed
310
+ while (!isUnique && counter < 100) {
311
+ const existingEvent = await mongoose_1.default.models.Event.findOne({
312
+ slug: slug,
313
+ _id: { $ne: this._id }
314
+ });
315
+ if (!existingEvent) {
316
+ isUnique = true;
317
+ }
318
+ else {
319
+ // Add counter suffix for uniqueness
320
+ slug = `${baseSlug}-${counter}`;
321
+ counter++;
322
+ }
323
+ }
324
+ // Fallback: if still not unique after 100 attempts, add random suffix
325
+ if (!isUnique) {
326
+ const randomSuffix = Math.random().toString(36).substring(2, 8);
327
+ slug = `${baseSlug}-${randomSuffix}`;
328
+ }
303
329
  this.slug = slug;
304
330
  }
305
331
  }
306
- // Ensure slug uniqueness
307
- if (this.slug) {
308
- const existingEvent = await mongoose_1.default.models.Event.findOne({
309
- slug: this.slug,
310
- _id: { $ne: this._id }
311
- });
312
- if (existingEvent) {
313
- // Add random suffix to make it unique
314
- const randomSuffix = Math.random().toString(36).substring(2, 8);
315
- this.slug = `${this.slug}-${randomSuffix}`;
316
- }
317
- }
318
332
  this.updatedAt = new Date();
319
333
  next();
320
334
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mac777/project-pinecone-models",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/src/Event.ts CHANGED
@@ -378,38 +378,53 @@ eventSchema.pre('save', async function(next) {
378
378
  if (this.isModified('title') && !this.slug) {
379
379
  if (this.title) {
380
380
  // Convert to lowercase, replace spaces with hyphens, remove special characters
381
- let slug = this.title
381
+ let baseSlug = this.title
382
382
  .toLowerCase()
383
+ .trim()
383
384
  .replace(/[^\w\s-]/g, '') // Remove special characters except spaces and hyphens
384
385
  .replace(/\s+/g, '-') // Replace spaces with hyphens
385
386
  .replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
386
- .trim();
387
-
388
- // Ensure slug is not empty and add random suffix if needed
389
- if (!slug) slug = 'event';
390
-
391
- // Add timestamp suffix to ensure uniqueness
392
- const timestamp = Date.now().toString(36);
393
- slug = `${slug}-${timestamp}`;
387
+ .replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
388
+
389
+ // Ensure slug is not empty
390
+ if (!baseSlug) baseSlug = 'event';
391
+
392
+ // Truncate to reasonable length (50 chars) to keep URLs clean
393
+ if (baseSlug.length > 50) {
394
+ baseSlug = baseSlug.substring(0, 50).replace(/-+$/, ''); // Remove trailing hyphen after truncation
395
+ }
396
+
397
+ // Try base slug first
398
+ let slug = baseSlug;
399
+ let counter = 1;
400
+ let isUnique = false;
401
+
402
+ // Check uniqueness and add counter if needed
403
+ while (!isUnique && counter < 100) {
404
+ const existingEvent = await mongoose.models.Event.findOne({
405
+ slug: slug,
406
+ _id: { $ne: this._id }
407
+ });
408
+
409
+ if (!existingEvent) {
410
+ isUnique = true;
411
+ } else {
412
+ // Add counter suffix for uniqueness
413
+ slug = `${baseSlug}-${counter}`;
414
+ counter++;
415
+ }
416
+ }
417
+
418
+ // Fallback: if still not unique after 100 attempts, add random suffix
419
+ if (!isUnique) {
420
+ const randomSuffix = Math.random().toString(36).substring(2, 8);
421
+ slug = `${baseSlug}-${randomSuffix}`;
422
+ }
394
423
 
395
424
  this.slug = slug;
396
425
  }
397
426
  }
398
427
 
399
- // Ensure slug uniqueness
400
- if (this.slug) {
401
- const existingEvent = await mongoose.models.Event.findOne({
402
- slug: this.slug,
403
- _id: { $ne: this._id }
404
- });
405
-
406
- if (existingEvent) {
407
- // Add random suffix to make it unique
408
- const randomSuffix = Math.random().toString(36).substring(2, 8);
409
- this.slug = `${this.slug}-${randomSuffix}`;
410
- }
411
- }
412
-
413
428
  this.updatedAt = new Date();
414
429
  next();
415
430
  });