https://github.com/lilbunnyrabbit/optional
TypeScript implementation of java.util.Optional<T>
https://github.com/lilbunnyrabbit/optional
java-optional npm-package null-safety optional type-safety typescript typescript-library
Last synced: 4 months ago
JSON representation
TypeScript implementation of java.util.Optional<T>
- Host: GitHub
- URL: https://github.com/lilbunnyrabbit/optional
- Owner: lilBunnyRabbit
- License: mit
- Created: 2024-11-27T22:50:26.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2025-01-24T00:19:49.000Z (5 months ago)
- Last Synced: 2025-01-24T01:20:50.750Z (5 months ago)
- Topics: java-optional, npm-package, null-safety, optional, type-safety, typescript, typescript-library
- Language: TypeScript
- Homepage: https://lilbunnyrabbit.github.io/optional/
- Size: 120 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# [TypeScript](https://www.typescriptlang.org/) implementation of [java.util.Optional](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)
[](https://www.npmjs.com/package/@lilbunnyrabbit/optional)
[](https://www.npmjs.com/package/@lilbunnyrabbit/optional)> **Definition**
> A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.
> Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value is present).
>
> This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided.The main difference is that it doesn't throw errors if the value is not defined and simply just returns `null`.
## Installation
To use this package in your project, run:
```sh
npm i @lilbunnyrabbit/optional
```## Simple Example
```ts
import { Optional } from "@lilbunnyrabbit/optional";const optional: Optional = Optional(123);
const value: number | null = optional.get();
if (optional.isPresent()) {
const copy: Optional.Present = optional;
const presentValue: number = optional.get();
} else {
const copy: OptionalEmpty = optional;
const emptyValue: null = optional.get();
}optional.ifPresent((value) => console.log("Present:", value));
const nested: string = optional.filter((value: number) => value > 3)
.map((value: number) => String(value))
.orElse(456); // "123"
```## Documentation
If you're looking for detailed API docs, check out the [full documentation](http://lilbunnyrabbit.github.io/optional) generated via [Typedoc](https://typedoc.org/).
## Development
This section provides a guide for developers to set up the project environment and utilize various npm scripts defined in the project for efficient development and release processes.
### Setting Up
Clone the repository and install dependencies:
```sh
git clone https://github.com/lilBunnyRabbit/optional.git
cd optional
npm install
```### NPM Scripts
The project includes several npm scripts to streamline common tasks such as building, testing, and cleaning up the project.
| Script | Description | Command |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
| **`build`** | Compiles the [TypeScript](https://www.typescriptlang.org/) source code to JavaScript, placing the output in the `dist` directory. Essential for preparing the package for publication or testing. | `npm run build` |
| **`test`** | Executes the test suite using [Jest](https://jestjs.io/). Crucial for ensuring that your code meets all defined tests and behaves as expected. | `npm test` |
| **`clean`** | Removes both the `dist` directory and the `node_modules` directory. Useful for resetting the project's state during development or before a fresh install. | `npm run clean` |
| **`changeset`** | Manages versioning and changelog generation based on conventional commit messages. Helps prepare for a new release by determining which parts of the package need version updates. | `npm run changeset` |
| **`release`** | Publishes the package to npm. Uses `changeset publish` to automatically update package versions and changelogs before publishing. Streamlines the release process. | `npm run release` |
| **`generate:docs`** | Generates project documentation using [Typedoc](https://typedoc.org/). Facilitates the creation of comprehensive and accessible API documentation. | `npm run generate:docs` |These scripts are designed to facilitate the development process, from cleaning and building the project to running tests and releasing new versions. Feel free to use and customize them as needed for your development workflow.
## Contribution
Contributions are always welcome! For any enhancements or bug fixes, please open a pull request linked to the relevant issue. If there's no existing issue related to your contribution, feel free to create one.
## Support
Your support is greatly appreciated! If this package has been helpful, consider supporting by buying me a coffee.
[](https://www.buymeacoffee.com/lilBunnyRabbit)
## License
MIT © Andraž Mesarič-Sirec