Optional gives the power to differentiate between the absence of a value and the null or undefined reference
The Optional class is an utility class that provides a way to handle potentially null or undefined values in a more concise and expressive manner. It allows wrapping a value in an Optional object, which can then be used to perform various operations on the value, such as checking if it is present, retrieving it, applying transformations, and handling empty values.
Overall, this simple yet powerful class helps create code that’s, simply put, more readable and less error-prone than its procedural counterpart.
Global Functions
The best way to wrap and starting using optional is through the global utility functions.
optionalOf
Wraps a value that might be undefined or null in an Optional container.
const result1 = optionalOf("Hello");
if (result1.isPresent()) {
console.log(result1.get()); // Prints: Hello
}
const result2 = optionalOf(null);
if (result2.isEmpty()) {
console.log("Value is not present"); // Prints: Value is not present
}
const result3 = optionalOf(undefined);
console.log(result3.orElse("Default")); // Prints: Default
const result4 = await optionalOf(user)
.orElseThrow(() => new HttpError(409, "User doesn't exist"))
.map(user => {
return {
...user,
userData,
};
})
.runAsync(user => this.userRepository.save(user));
console.log(result4); // Prints: saved user content
allPresent
Checks if all the elements in an array of Optional objects have a non-empty value.
The of method creates a new Optional object with a specified value. It wraps the value, allowing for more concise and expressive code when dealing with potentially null or undefined values.
const value = 42;
const optionalValue = Optional.of(value);
empty
The empty method returns an empty Optional object.
const emptyOptional = Optional.empty();
allPresent
The allPresent method checks if all elements in an array of Optional objects are present.
Retrieve the value of the Optional object if it is present, or throw an error if the Optional object is empty.
const optionalValue = Optional.of("Hello");
const value = optionalValue.orElseThrow(() => new Error("Value is not present"));
console.log(value); // Output: "Hello"
const emptyOptional = Optional.empty();
emptyOptional.orElseThrow(() => new Error("Value is not present")); // Throws an error
map
Transform the value inside the Optional object using a provided mapper function. It returns a new Optional object with the mapped value. If the original Optional object is empty, it throws an error.
const optional = Optional.of(5); // Create an Optional object with a value of 5
const mappedOptional = optional.map(value => value * 2); // Map the value to its double
console.log(mappedOptional.get()); // Output: 10
const emptyOptional = Optional.empty(); // Create an empty Optional object
const defaultValue = 0;
const mappedEmptyOptional = emptyOptional.map(value => value * 2, defaultValue); // Map the value to its double or use the default value if empty
console.log(mappedEmptyOptional.get()); // Output: 0
flatMap
Apply a mapper function to the value inside the Optional object and return a new Optional object with the mapped value. If the original Optional object is empty, it throws an error.
Chaining asynchronous operations on an optional value. It takes a mapper function that returns a promise of an Optional object. If the current optional value is present, it applies the mapper function and returns the result as an Optional object. If the current optional value is empty, it returns an empty Optional object.
Filter the value inside the Optional object based on a given predicate function. It returns a new Optional object containing the filtered value if the original Optional object is present, or an empty Optional object if the original Optional object is not present.
const optional = Optional.of([1, 2, 3, 4, 5]); // Create an Optional object with an array value
const filteredOptional = optional.filter(value => value > 3); // Filter the array to keep only values greater than 3
console.log(filteredOptional.get()); // Output: [4, 5]
const emptyOptional = Optional.empty(); // Create an empty Optional object
const filteredEmptyOptional = emptyOptional.filter(value => value > 3); // Filter the empty Optional object
console.log(filteredEmptyOptional.isEmpty()); // Output: true
isEmpty
Checks if the value inside the Optional object is empty or not.
const optionalValue = Optional.of("Hello"); // Create an Optional object with a non-empty value
console.log(optionalValue.isEmpty()); // Output: false
const emptyOptional = Optional.empty(); // Create an Optional object with an empty value
console.log(emptyOptional.isEmpty()); // Output: true
ifEmpty
Executes a specified action if the value inside the Optional object is empty.
const optional = Optional.of(null);
optional.ifEmpty(() => console.log("Value is empty")); // Output: "Value is empty"
ifEmptyThrow
Throws an error if the value inside the Optional object is empty, otherwise it returns the Optional object.
const optional = Optional.of("value");
optional.ifEmptyThrow(() => new Error("Value is empty")); // Returns the optional object
const emptyOptional = Optional.empty();
emptyOptional.ifEmptyThrow(() => new Error("Value is empty")); // Throws an error
ifEmptyGet
Returns the value inside the Optional object if it is not empty, otherwise it returns a default value provided by a callback function.
Allows you to execute a callback function on the value stored in the Optional object and return a new Optional object with the result.
const optional = Optional.of(5); // Create an Optional object with a value of 5
const newOptional = optional.run(value => value * 2); // Execute the callback function on the value and create a new Optional object with the result
console.log(newOptional.get()); // Output: 10
runAsync
Allows you to asynchronously execute a callback function on the value contained within the Optional object.
const optional = Optional.of(5); // Create an Optional object with a value of 5
const asyncCallback = async (value: number) => {
// Perform some asynchronous operation on the value
const result = await someAsyncFunction(value);
return result;
};
const resultPromise = optional.runAsync(asyncCallback); // Execute the async callback on the value
resultPromise.then(result => {
console.log(result); // Output the result of the asynchronous operation
}).catch(error => {
console.error(error); // Handle any errors that occurred during the asynchronous operation
});
else
Used to provide an alternative value or action when the optional value is empty.
Used to execute a callback function asynchronously if the optional value is empty. It returns a promise that resolves to the result of the callback function.