Add default value function to ModelRepository get method.

This commit is contained in:
Madeorsk 2022-08-04 01:18:12 +02:00
parent e7050a2fc0
commit 44ba66b39f
1 changed files with 3 additions and 2 deletions

View File

@ -153,13 +153,14 @@ export class ModelRepository<T extends Model = Model>
/** /**
* Get the model identified by its identifier. * Get the model identified by its identifier.
* @param identifier - Identifier of the model. * @param identifier - Identifier of the model.
* @param defaultValue - Function called when there is no value matching the given identifier in the repository. Return null by default.
*/ */
get(identifier: string): T|null get(identifier: string, defaultValue: (identifier: string) => T|null = () => null): T|null
{ {
return typeof this.models?.[identifier] !== "undefined" return typeof this.models?.[identifier] !== "undefined"
// Model exists, returning it. // Model exists, returning it.
? this.models[identifier] ? this.models[identifier]
// Model does not exists, returning NULL. // Model does not exists, returning NULL.
: null; : defaultValue(identifier);
} }
} }