https://github.com/niradler/fn-lifecycle
Control the before and after of running a function, can be used for validation, caching, data mutation and more.
https://github.com/niradler/fn-lifecycle
Last synced: 12 months ago
JSON representation
Control the before and after of running a function, can be used for validation, caching, data mutation and more.
- Host: GitHub
- URL: https://github.com/niradler/fn-lifecycle
- Owner: niradler
- License: mit
- Created: 2023-02-27T22:10:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-14T22:11:02.000Z (over 3 years ago)
- Last Synced: 2025-06-06T11:03:48.483Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://niradler.github.io/fn-lifecycle/
- Size: 171 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# function lifecycle
Control the before and after of running a function, can be used for validation, caching, data mutation and more.
## Features
- Wraps functions to create a lifecycle of before and after
- Supports TypeScript
- Can be used for validation, caching, and more
- Easy to use and integrate into existing projects
- Provides a flexible and customizable solution for managing function lifecycles
## Usage
```sh
npm i fn-lifecycle
```
```ts
import { Lifecycle } from "fn-lifecycle";
const lc = new Lifecycle({ name: 1 });
function validate(name) {
if (!name || (name && name.length < this._.config.name)) {
throw new Error("name is missing");
}
}
const addGreet = (name) => `Hi, ${name}`;
const greet = lc.before(validate).after(addGreet).decorate(whatIsMyName);
const myName = "Nir";
await greet(myName);
expect(await greet(myName)).toBe(`Hi, ${myName}`);
```
check the tests folder for more complete examples.
### Contributing
Contributions are always welcome! If you have any ideas or suggestions for how to improve this package, please feel free to open an issue or submit a pull request.