diff --git a/package.json b/package.json index 9997587..43b7bb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "katxupa", - "version": "1.9.7", + "version": "1.9.8", "description": "Delicious Dish for Typescript and JavaScript projects", "author": "Manuel Santos ", "license": "MIT", diff --git a/src/optional.ts b/src/optional.ts index d85b6cf..a73cf60 100644 --- a/src/optional.ts +++ b/src/optional.ts @@ -322,17 +322,16 @@ export class Optional { * @param mapper - A function that takes the current value of type T and returns a new value of type U. * @param defaultValue - default value when the optional is empty * @return Returns a new Optional object with the mapped value. - * returns a new Optional object with the mapped value or the default Optional value. - * Throws an error if the original Optional object is empty. + * returns a new Optional object with the mapped value, the default Optional value or empty. * */ - map(mapper: (value: T) => U, defaultValue?: U): Optional { + map(mapper: (value: T) => U, defaultValue?: U): Optional { if (this.isPresent()) { return Optional.of(mapper(this.value!)); } if (defaultValue) { return Optional.of(defaultValue); } - throw new Error("Map operation cannot be called on an empty Optional"); + return Optional.empty(); } /** @@ -347,14 +346,13 @@ export class Optional { * * @param mapper (function) - A function that takes the value of type T inside the Optional object and returns an * Optional object with a mapped value of type U. - * @return Returns a new Optional object with the mapped value. - * Throws an error if the original Optional object is empty. + * @return Returns a new Optional object with the mapped value, or empty. * */ - flatMap(mapper: (value: T) => Optional): Optional { + flatMap(mapper: (value: T) => Optional): Optional { if (this.isPresent()) { return mapper(this.value!); } - throw new Error("FlatMap operation cannot be called on an empty Optional"); + return Optional.empty(); } /** @@ -387,7 +385,7 @@ export class Optional { if (this.isPresent()) { return mapper(this.value!).then(result => result.isPresent() ? result : Optional.empty()); } - return Promise.reject(new Error("FlatMap operation cannot be called on an empty Optional")); + return Optional.empty(); } /**