Skip to content

Commit

Permalink
improve handling of empty optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
manusant committed May 2, 2024
1 parent 02886e2 commit 3b02796
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "katxupa",
"version": "1.9.8",
"version": "1.10.0",
"description": "Delicious Dish for Typescript and JavaScript projects",
"author": "Manuel Santos <ney.br.santos@gmail.com>",
"license": "MIT",
Expand Down
20 changes: 10 additions & 10 deletions src/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class Optional<T> {
*
* @return an empty Optional object.
* */
static empty(): Optional<undefined> {
return Optional.of(undefined);
static empty<R>(): Optional<R> {
return Optional.of<R>(undefined);
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Optional<T> {
* @param optionals - A spread parameter that accepts a variable number of Optional objects
* @return Returns the first non-empty optional from the list of optionals, or an empty optional if all optionals are empty.
* */
static coalesce<T>(...optionals: Optional<T | undefined | null>[]): Optional<T | undefined | null> {
static coalesce<T>(...optionals: Optional<T>[]): Optional<T> {
for (const optional of optionals) {
if (optional.isPresent()) {
return optional;
Expand Down Expand Up @@ -324,7 +324,7 @@ export class Optional<T> {
* @return Returns a new Optional object with the mapped value.
* returns a new Optional object with the mapped value, the default Optional value or empty.
* */
map<U>(mapper: (value: T) => U, defaultValue?: U): Optional<U | undefined> {
map<U>(mapper: (value: T) => U, defaultValue?: U): Optional<U> {
if (this.isPresent()) {
return Optional.of(mapper(this.value!));
}
Expand All @@ -348,7 +348,7 @@ export class Optional<T> {
* Optional object with a mapped value of type U.
* @return Returns a new Optional object with the mapped value, or empty.
* */
flatMap<U>(mapper: (value: T) => Optional<U>): Optional<U | undefined> {
flatMap<U>(mapper: (value: T) => Optional<U>): Optional<U> {
if (this.isPresent()) {
return mapper(this.value!);
}
Expand Down Expand Up @@ -381,7 +381,7 @@ export class Optional<T> {
* @param mapper - A function that takes the current value of the optional and returns a promise of an Optional object.
* @return An Optional object that contains the result of the mapper function, or an empty Optional object if the current optional value is empty
* */
async flatMapAsync<U>(mapper: (value: T) => Promise<Optional<U>>): Promise<Optional<U | undefined | null>> {
async flatMapAsync<U>(mapper: (value: T) => Promise<Optional<U>>): Promise<Optional<U>> {
if (this.isPresent()) {
return mapper(this.value!).then(result => result.isPresent() ? result : Optional.empty());
}
Expand All @@ -407,7 +407,7 @@ export class Optional<T> {
* @return Returns a new Optional object containing the filtered value if the original Optional object is present.
* Returns an empty Optional object if the original Optional object is not present.
* */
filter(predicate: (value: T) => boolean): Optional<T | undefined> {
filter(predicate: (value: T) => boolean): Optional<T> {
if (this.isEmpty()) {
return this; // Return itself if it's empty
}
Expand Down Expand Up @@ -437,7 +437,7 @@ export class Optional<T> {
* @return Returns a new Optional object containing the converted value if the original Optional object is present.
* Returns an empty Optional object if the original Optional object is not present.
* */
convert<U>(converter: (value: T) => U): Optional<U | undefined> {
convert<U>(converter: (value: T) => U): Optional<U> {
return this.isPresent() ? Optional.of(converter(this.value!)) : Optional.empty();
}

Expand Down Expand Up @@ -654,7 +654,7 @@ export class Optional<T> {
* @param defaultProvider (optional) - Provider for default value in case of empty optional
* @return Returns a new Optional object that contains the result of executing the callback function on the value stored in the original Optional object. Returns empty or default optional otherwise
* */
run<R>(callback: (value: T) => R, defaultProvider?: () => R): Optional<R | undefined> {
run<R>(callback: (value: T) => R, defaultProvider?: () => R): Optional<R> {
if (this.isPresent()) {
const result = callback(this.value as T);
return Optional.of(result);
Expand Down Expand Up @@ -687,7 +687,7 @@ export class Optional<T> {
* @param defaultProvider (optional) - Provider for default value in case of empty optional
* @return A Promise that resolves to the result of the asynchronous operation performed by the callback function.
* */
async runAsync<R>(callback: (value: T) => Promise<R>, defaultProvider?: () => R): Promise<Optional<R | undefined>> {
async runAsync<R>(callback: (value: T) => Promise<R>, defaultProvider?: () => R): Promise<Optional<R>> {
if (this.isPresent()) {
return Optional.of(await callback(this.value as T));
}
Expand Down

0 comments on commit 3b02796

Please sign in to comment.