diff --git a/packages/builder/src/helpers/duplicate.js b/packages/builder/src/helpers/duplicate.js index e84a600138..1547fcd4d1 100644 --- a/packages/builder/src/helpers/duplicate.js +++ b/packages/builder/src/helpers/duplicate.js @@ -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}` } diff --git a/packages/builder/src/helpers/tests/duplicate.test.js b/packages/builder/src/helpers/tests/duplicate.test.js index a571054e0c..400abed0aa 100644 --- a/packages/builder/src/helpers/tests/duplicate.test.js +++ b/packages/builder/src/helpers/tests/duplicate.test.js @@ -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") }) }) })