From 44ba66b39fa9cc6ca0c779b32e611444963ac540 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Thu, 4 Aug 2022 01:18:12 +0200 Subject: [PATCH] Add default value function to ModelRepository get method. --- src/Model/Repositories/ModelRepository.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Model/Repositories/ModelRepository.ts b/src/Model/Repositories/ModelRepository.ts index 1e848cc..da9d70d 100644 --- a/src/Model/Repositories/ModelRepository.ts +++ b/src/Model/Repositories/ModelRepository.ts @@ -153,13 +153,14 @@ export class ModelRepository /** * Get the model identified by its identifier. * @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" // Model exists, returning it. ? this.models[identifier] // Model does not exists, returning NULL. - : null; + : defaultValue(identifier); } }