Skip to content

Commit 88744b5

Browse files
committed
better names for import
1 parent fc64ffb commit 88744b5

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

packages/compass-data-modeling/src/services/open-and-download-diagram.spec.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,42 @@ describe('open-and-download-diagram', function () {
2626
expect(decodedEdits).to.deep.equal(FlightDiagram.edits);
2727
});
2828

29-
it('should return the correct diagram name', function () {
30-
const existingNames = ['Flights', 'Berlin Public Transport'];
31-
32-
expect(
33-
getDiagramName(existingNames, 'Airbnb'),
34-
'should return the expected name when it does not exist'
35-
).to.equal('Airbnb');
36-
37-
expect(
38-
getDiagramName(existingNames, 'Flights'),
39-
'should return the next expected name when it exists'
40-
).to.equal('Flights (1)');
41-
42-
existingNames.push('Flights (1)');
29+
context('getDiagramName', function () {
30+
const usecases = [
31+
{
32+
existingNames: [],
33+
name: 'Airbnb',
34+
expectedName: 'Airbnb',
35+
message: 'should return the expected name when it does not exist',
36+
},
37+
{
38+
existingNames: ['Airbnb'],
39+
name: 'Airbnb',
40+
expectedName: 'Airbnb (1)',
41+
message: 'should return the next expected name when it exists',
42+
},
43+
{
44+
existingNames: ['Airbnb (1)'],
45+
name: 'Airbnb (1)',
46+
expectedName: 'Airbnb (2)',
47+
message:
48+
'should return the next expected name when name with (number) exists',
49+
},
50+
{
51+
existingNames: ['Airbnb', 'Airbnb (1)', 'Airbnb (2)'],
52+
name: 'Airbnb (1)',
53+
expectedName: 'Airbnb (3)',
54+
message:
55+
'should return the next expected name when multiple versions exist',
56+
},
57+
];
4358

44-
expect(
45-
getDiagramName(existingNames, 'Flights'),
46-
'should return the next expected name when multiple versions exist'
47-
).to.equal('Flights (2)');
59+
for (const { existingNames, name, expectedName, message } of usecases) {
60+
it(message, function () {
61+
const result = getDiagramName(existingNames, name);
62+
expect(result).to.equal(expectedName);
63+
});
64+
}
4865
});
4966

5067
context('getDiagramContentsFromFile', function () {

packages/compass-data-modeling/src/services/open-and-download-diagram.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,28 @@ export async function getDiagramContentsFromFile(
7979
});
8080
}
8181

82+
function getNameAndCount(expectedName: string): [string, number] {
83+
const { groups = {} } =
84+
expectedName.match(/^(?<name>.+?)(\s\((?<count>\d+)\))?$/) ?? {};
85+
return [groups.name ?? expectedName, groups.count ? Number(groups.count) : 0];
86+
}
87+
8288
export function getDiagramName(
8389
existingNames: string[],
8490
expectedName: string
8591
): string {
86-
let name = expectedName;
87-
let index = 1;
88-
while (existingNames.includes(name)) {
89-
name = `${expectedName} (${index})`;
90-
index += 1;
92+
if (!existingNames.includes(expectedName)) {
93+
return expectedName;
9194
}
92-
return name;
95+
const [initialName, initialCount] = getNameAndCount(expectedName);
96+
97+
const finalCount = existingNames.reduce((accumulatedCount, name) => {
98+
const [baseName, count] = getNameAndCount(name);
99+
if (baseName === initialName && count >= accumulatedCount) {
100+
return count + 1;
101+
}
102+
return accumulatedCount;
103+
}, initialCount + 1);
104+
105+
return `${initialName} (${finalCount})`;
93106
}

0 commit comments

Comments
 (0)