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

[ProjectHelper] _addNumericSuffixToProjectName: add heuristic for year

GitOrigin-RevId: 24c57d68706bdabba620137674946e3c8e675939
This commit is contained in:
Shane Kilkelly 2021-04-29 09:16:37 +01:00 committed by Copybot
parent 3c9cd69bfa
commit 6893cce6c9
2 changed files with 30 additions and 0 deletions

View file

@ -141,6 +141,16 @@ function _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
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) {
const candidate = suffixedName(basename, n)
if (!allProjectNames.has(candidate)) {

View file

@ -259,6 +259,8 @@ describe('ProjectDetailsHandler', function () {
{ _id: 138, name: 'numeric (38)' },
{ _id: 139, name: 'numeric (39)' },
{ _id: 140, name: 'numeric (40)' },
{ _id: 141, name: 'Yearbook (2021)' },
{ _id: 142, name: 'Yearbook (2021) (1)' },
],
readAndWrite: [
{ _id: 4, name: 'name2' },
@ -364,6 +366,24 @@ describe('ProjectDetailsHandler', function () {
)
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 () {