Răsfoiți Sursa

Merge pull request #3964 from overleaf/jpa-unique-project-name-with-year

[ProjectHelper] _addNumericSuffixToProjectName: add heuristic for year

GitOrigin-RevId: 24c57d68706bdabba620137674946e3c8e675939
Shane Kilkelly 5 ani în urmă
părinte
comite
6893cce6c9

+ 10 - 0
services/web/app/src/Features/Project/ProjectHelper.js

@@ -141,6 +141,16 @@ function _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
     n = parseInt(match[1])
     n = parseInt(match[1])
   }
   }
 
 
+  const prefixMatcher = new RegExp(`^${basename} \\(\\d+\\)$`)
+  const projectNamesWithSamePrefix = Array.from(allProjectNames).filter(name =>
+    prefixMatcher.test(name)
+  )
+  const nIsLikelyAYear = n > 1000 && projectNamesWithSamePrefix.length < n / 2
+  if (nIsLikelyAYear) {
+    basename = name
+    n = 1
+  }
+
   while (n <= last) {
   while (n <= last) {
     const candidate = suffixedName(basename, n)
     const candidate = suffixedName(basename, n)
     if (!allProjectNames.has(candidate)) {
     if (!allProjectNames.has(candidate)) {

+ 20 - 0
services/web/test/unit/src/Project/ProjectDetailsHandlerTests.js

@@ -259,6 +259,8 @@ describe('ProjectDetailsHandler', function () {
           { _id: 138, name: 'numeric (38)' },
           { _id: 138, name: 'numeric (38)' },
           { _id: 139, name: 'numeric (39)' },
           { _id: 139, name: 'numeric (39)' },
           { _id: 140, name: 'numeric (40)' },
           { _id: 140, name: 'numeric (40)' },
+          { _id: 141, name: 'Yearbook (2021)' },
+          { _id: 142, name: 'Yearbook (2021) (1)' },
         ],
         ],
         readAndWrite: [
         readAndWrite: [
           { _id: 4, name: 'name2' },
           { _id: 4, name: 'name2' },
@@ -364,6 +366,24 @@ describe('ProjectDetailsHandler', function () {
       )
       )
       expect(name).to.equal('numeric (41)')
       expect(name).to.equal('numeric (41)')
     })
     })
+
+    it('should handle years in name', async function () {
+      const name = await this.handler.promises.generateUniqueName(
+        this.user._id,
+        'unique-name (2021)',
+        []
+      )
+      expect(name).to.equal('unique-name (2021)')
+    })
+
+    it('should handle duplicating with year in name', async function () {
+      const name = await this.handler.promises.generateUniqueName(
+        this.user._id,
+        'Yearbook (2021)',
+        []
+      )
+      expect(name).to.equal('Yearbook (2021) (2)')
+    })
   })
   })
 
 
   describe('fixProjectName', function () {
   describe('fixProjectName', function () {