Skip to content

Commit

Permalink
Move the generic registry to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Jun 8, 2024
1 parent 1f36300 commit f2a5d82
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
36 changes: 0 additions & 36 deletions src/utils/common/ecosystem.ts
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
type Configuration = Record<string, any>;
type Builder<T extends Configuration = Configuration> = (config: T) => () => any;

export class Registry<K extends string> {
private registry: Map<K, Builder> = new Map();

register(key: K, builder: Builder): void {
if (this.registry.has(key)) {
throw new Error(`Builder with key '${key}' is already registered`);
}
this.registry.set(key, builder);
}

has(key: K): boolean {
return this.registry.has(key);
}

get(key: K): Builder {
if (!this.has(key)) {
throw new Error(`There is no builder with key '${key}' registered`);
}

return this.registry.get(key)!;
}

remove(key: K): boolean {
if (!this.registry.has(key)) {
throw new Error(`Builder with key '${key}' does not exist`);
}
return this.registry.delete(key);
}

allNames(): K[] {
return Array.from(this.registry.keys());
}
}
2 changes: 1 addition & 1 deletion src/utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './charsets';
export * from './casing';
export * from './objectManipulation';
export * from './sorting';
export * from './ecosystem';
export * from './registry';
36 changes: 36 additions & 0 deletions src/utils/common/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type Configuration = Record<string, any>;
type Builder<T extends Configuration = Configuration> = (config: T) => () => any;

export class Registry<K extends string> {
private registry: Map<K, Builder> = new Map();

register(key: K, builder: Builder): void {
if (this.registry.has(key)) {
throw new Error(`Builder with key '${key}' is already registered`);
}
this.registry.set(key, builder);
}

has(key: K): boolean {
return this.registry.has(key);
}

get(key: K): Builder {
if (!this.has(key)) {
throw new Error(`There is no builder with key '${key}' registered`);
}

return this.registry.get(key)!;
}

remove(key: K): boolean {
if (!this.registry.has(key)) {
throw new Error(`Builder with key '${key}' does not exist`);
}
return this.registry.delete(key);
}

allNames(): K[] {
return Array.from(this.registry.keys());
}
}

0 comments on commit f2a5d82

Please sign in to comment.