Merge pull request #11896 from Budibase/fix/create-duplicate-queries
Fix for database query duplication
This commit is contained in:
commit
4671d5fc4c
|
@ -3,16 +3,17 @@
|
|||
* e.g.
|
||||
* name all names result
|
||||
* ------ ----------- --------
|
||||
* ("foo") ["foo"] "foo (1)"
|
||||
* ("foo") ["foo", "foo (1)"] "foo (2)"
|
||||
* ("foo (1)") ["foo", "foo (1)"] "foo (2)"
|
||||
* ("foo") ["foo", "foo (2)"] "foo (1)"
|
||||
* ("foo") ["foo"] "foo 1"
|
||||
* ("foo") ["foo", "foo 1"] "foo 2"
|
||||
* ("foo 1") ["foo", "foo 1"] "foo 2"
|
||||
* ("foo") ["foo", "foo 2"] "foo 1"
|
||||
*
|
||||
* Repl
|
||||
*/
|
||||
export const duplicateName = (name, allNames) => {
|
||||
const baseName = name.split(" (")[0]
|
||||
const isDuplicate = new RegExp(`${baseName}\\s\\((\\d+)\\)$`)
|
||||
const duplicatePattern = new RegExp(`\\s(\\d+)$`)
|
||||
const baseName = name.split(duplicatePattern)[0]
|
||||
const isDuplicate = new RegExp(`${baseName}\\s(\\d+)$`)
|
||||
|
||||
// get the sequence from matched names
|
||||
const sequence = []
|
||||
|
@ -28,7 +29,6 @@ export const duplicateName = (name, allNames) => {
|
|||
return false
|
||||
})
|
||||
sequence.sort((a, b) => a - b)
|
||||
|
||||
// get the next number in the sequence
|
||||
let number
|
||||
if (sequence.length === 0) {
|
||||
|
@ -46,5 +46,5 @@ export const duplicateName = (name, allNames) => {
|
|||
}
|
||||
}
|
||||
|
||||
return `${baseName} (${number})`
|
||||
return `${baseName} ${number}`
|
||||
}
|
||||
|
|
|
@ -9,34 +9,34 @@ describe("duplicate", () => {
|
|||
|
||||
const duplicate = duplicateName(name, names)
|
||||
|
||||
expect(duplicate).toBe("foo (1)")
|
||||
expect(duplicate).toBe("foo 1")
|
||||
})
|
||||
|
||||
it("with multiple existing", async () => {
|
||||
const names = ["foo", "foo (1)", "foo (2)"]
|
||||
const names = ["foo", "foo 1", "foo 2"]
|
||||
const name = "foo"
|
||||
|
||||
const duplicate = duplicateName(name, names)
|
||||
|
||||
expect(duplicate).toBe("foo (3)")
|
||||
expect(duplicate).toBe("foo 3")
|
||||
})
|
||||
|
||||
it("with mixed multiple existing", async () => {
|
||||
const names = ["foo", "foo (1)", "foo (2)", "bar", "bar (1)", "bar (2)"]
|
||||
const names = ["foo", "foo 1", "foo 2", "bar", "bar 1", "bar 2"]
|
||||
const name = "foo"
|
||||
|
||||
const duplicate = duplicateName(name, names)
|
||||
|
||||
expect(duplicate).toBe("foo (3)")
|
||||
expect(duplicate).toBe("foo 3")
|
||||
})
|
||||
|
||||
it("with incomplete sequence", async () => {
|
||||
const names = ["foo", "foo (2)", "foo (3)"]
|
||||
const names = ["foo", "foo 2", "foo 3"]
|
||||
const name = "foo"
|
||||
|
||||
const duplicate = duplicateName(name, names)
|
||||
|
||||
expect(duplicate).toBe("foo (1)")
|
||||
expect(duplicate).toBe("foo 1")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue