Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
typescript-optional
Advanced tools
Optional (like Java) implementation in TypeScript
Optional<T>
is a type which may or may not contain a payload of type T
.
It provides a common interface regardless of whether an instance is present or is empty.
This module is inspired by Optional class in Java 8+.
npm install --save typescript-optional
// import Optional type from this module
import Optional from 'typescript-optional';
Optional<T>
objectslet nullableString: string | null = /* some nullable value */;
// all the following variables will be parameterized as Optional<string>.
let optional = Optional.ofNullable(nullableString);
let optionalPresent1 = Optional.ofNullable("foo");
let optionalPresent2 = Optional.ofNonNull("foo"); // accepts non-null value (or else throws TypeError)
let optionalEmpty1: Optional<string> = Optional.empty(); // type hinting required
let optionalEmpty2 = Optional.empty<string>(); // or parameterize explicitly
let optional: Optional<string> = Optional.ofNullable( /* some optional value: null | string */ );
// force to retrieve the payload. (or else throws TypeError.)
// this method is not used match.
optional.get();
// be whether a payload is present or not.
optional.isPresent
// be whether this is empty or not. (negation of `isPresent` property)
optional.isEmpty
// execute the given consumer if a payload is present.
optional.ifPresent(value => console.log(value));
// execute the first argument (consumer) if a payload is present, execute the second argument (emptyAction) otherwise.
optional.ifPresentOrElse(value => console.log(value), () => console.log("empty"));
// filter a payload with additional predicate.
optional.filter(value => value.length > 0);
// map a payload with the given mapper.
optional.map(value => value.length);
// map a payload with the given mapper which returns value wrapped with Optional type.
let powerIfPositive: (x: Number) => Optional<Number>
= x => (x > 0) ? Optional.ofNonNull(x * x) : Optional.empty();
let numberOptional: Optional<number> = Optional.ofNullable(/* some optional value: null | number */)
numberOptional.flatMap(value => powerIfPositive(value));
// return this if this is present, return the given another optional otherwise.
let another: Optional<string> = Optional.ofNullable(/* ... */);
optional.or(another);
// retrieve a payload if this is present, return the given value otherwise.
optional.orElse("bar");
// retrieve a payload if this is present, return a value supplied by the given function otherwise.
optional.orElseGet(() => "bar");
// retrieve a payload if this is present, throws an exception supplied by the given function otherwise.
optional.orElseThrow(() => new Error());
MIT License - LICENSE.md
FAQs
Optional (like Java) implementation in TypeScript
The npm package typescript-optional receives a total of 8,146 weekly downloads. As such, typescript-optional popularity was classified as popular.
We found that typescript-optional demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.