Skip to content

Commit

Permalink
simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolzak committed Jan 7, 2025
1 parent a7840b0 commit 5639e40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
23 changes: 9 additions & 14 deletions src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,15 @@ class ExpressionState {
formattedName = `p${formattedName}`;
}

// Check if we've seen this stripped name before
if (this.formattedNameToOriginalNameMap.has(formattedName)) {
const existing = this.formattedNameToOriginalNameMap.get(formattedName);
if (existing !== originalName) {
// If we have a collision (e.g., "user-name" and "username" both become "username"),
// append a numeric suffix (username_2, username_3, etc.)
let suffix = 2;
while (
this.formattedNameToOriginalNameMap.has(`${formattedName}_${suffix}`)
) {
suffix++;
}
formattedName = `${formattedName}_${suffix}`;
}
let nameSuffix = 1;
let originalFormattedName = formattedName;

while (
this.formattedNameToOriginalNameMap.has(formattedName) &&
this.formattedNameToOriginalNameMap.get(formattedName) !== originalName
) {
nameSuffix++;
formattedName = `${originalFormattedName}_${nameSuffix}`;
}

this.formattedNameToOriginalNameMap.set(formattedName, originalName);
Expand Down
5 changes: 4 additions & 1 deletion test/connected.update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ describe("Update Item", () => {
.data((attr, op) => {
op.set(attr.custom.prop1, updates.prop1);
op.set(attr.custom["prop1 "], updates.prop1);
op.set(attr.custom["prop1 "], updates.prop1);
op.add(attr.views, created.custom.prop3);
op.set(attr.license, op.name(attr.files[0]));
op.add(attr.recentCommits[0].views, updates.recentCommitsViews);
Expand All @@ -1394,7 +1395,7 @@ describe("Update Item", () => {

expect(params).to.deep.equal({
UpdateExpression:
"SET #stars = (if_not_exists(#stars, :stars_default_value_u0) - :stars_u0), #files = list_append(if_not_exists(#files, :files_default_value_u0), :files_u0), #description = :description_u0, #custom.#prop1 = :custom_u0, #custom.#prop1_2 = :custom_u1, #license = #files[0], #repoOwner = :repoOwner_u0, #repoName = :repoName_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0 REMOVE #about, #recentCommits[1].#message ADD #followers :followers_u0, #views :views_u0, #recentCommits[0].#views :views_u1 DELETE #tags :tags_u0",
"SET #stars = (if_not_exists(#stars, :stars_default_value_u0) - :stars_u0), #files = list_append(if_not_exists(#files, :files_default_value_u0), :files_u0), #description = :description_u0, #custom.#prop1 = :custom_u0, #custom.#prop1_2 = :custom_u1, #custom.#prop1_3 = :custom_u2, #license = #files[0], #repoOwner = :repoOwner_u0, #repoName = :repoName_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0 REMOVE #about, #recentCommits[1].#message ADD #followers :followers_u0, #views :views_u0, #recentCommits[0].#views :views_u1 DELETE #tags :tags_u0",
ExpressionAttributeNames: {
"#followers": "followers",
"#stars": "stars",
Expand All @@ -1405,6 +1406,7 @@ describe("Update Item", () => {
"#custom": "custom",
"#prop1": "prop1",
"#prop1_2": "prop1 ",
"#prop1_3": "prop1 ",
"#views": "views",
"#recentCommits": "recentCommits",
"#message": "message",
Expand All @@ -1424,6 +1426,7 @@ describe("Update Item", () => {
":tags_u0": params.ExpressionAttributeValues[":tags_u0"],
":custom_u0": "def",
":custom_u1": "def",
":custom_u2": "def",
":views_u0": 200,
":views_u1": 3,
":repoName_u0": repoName,
Expand Down

0 comments on commit 5639e40

Please sign in to comment.