Skip to content

Commit

Permalink
refactor: generate new unique name until unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Feb 18, 2025
1 parent 4d083fc commit 22fbf6b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ export default class CoapServer implements ProtocolServer {
}

private createThingUrlPath(thing: ExposedThing) {
const urlPath = slugify(thing.title, { lower: true });
let urlPath = slugify(thing.title, { lower: true });

if (this.things.has(urlPath)) {
return Helpers.generateUniqueName(urlPath);
while (this.things.has(urlPath)) {
urlPath = Helpers.generateUniqueName(urlPath);
}

return urlPath;
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export default class HttpServer implements ProtocolServer {
public async expose(thing: ExposedThing, tdTemplate: WoT.ExposedThingInit = {}): Promise<void> {
let urlPath = slugify(thing.title, { lower: true });

if (this.things.has(urlPath)) {
while (this.things.has(urlPath)) {
urlPath = Helpers.generateUniqueName(urlPath);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/binding-websockets/src/ws-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default class WebSocketServer implements ProtocolServer {
public expose(thing: ExposedThing): Promise<void> {
let urlPath = slugify(thing.title, { lower: true });

if (this.thingNames.has(urlPath)) {
while (this.thingNames.has(urlPath)) {
urlPath = Helpers.generateUniqueName(urlPath);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class Helpers implements Resolver {
if (suffix !== null) {
return name.slice(0, -suffix[1].length) + (1 + parseInt(suffix[1]));
} else {
return name + "_2";
return name + "_";
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/HelpersTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,14 @@ class HelperTest {
expect.fail("not properly set-up");
}
}

@test "should generate unique names over and over again"() {
let urlPath = "bla";
let paths = [urlPath];

Check failure on line 205 in packages/core/test/HelpersTest.ts

View workflow job for this annotation

GitHub Actions / eslint

'paths' is never reassigned. Use 'const' instead
for (let i = 0; i < 5; i++) {
urlPath = Helpers.generateUniqueName(urlPath);
expect(paths.includes(urlPath)).to.equal(false);
paths.push(urlPath);
}
}
}

0 comments on commit 22fbf6b

Please sign in to comment.