checklogrun
v0.2.4
Published
The library promotes the separation of responsibilities, allowing the logic, valitadion and logging to be kept in distinct modules, organizing the code into separate files and reducing the complexity of the main funciton
Downloads
142
Maintainers
Readme
Introduction
CheckLogRun is a JavaScript library designed to manage the execution flow of functions with flexible callback capabilities. It allows users to define a primary function and optionally integrate callbacks that run before, after, or modify the return value of the main function.
Table of Contents
Installation
To use CheckLogRun, simply clone this repository or include the files in your project. It is structured as a modular library, and you can import only the parts you need.
git clone https://github.com/your-repo/checklogrun.git
If using via npm:
npm install checklogrun
Usage
Basic Structure
The main concept behind CheckLogRun is that you initialize it with a primary function using the .main()
method. After that, you can attach additional callbacks (before, after, or on return) that will modify the behavior.
The library exposes several key methods:
- main(callback) - Defines the primary function to be executed.
- cbb(callback) - Adds a "before" callback, which runs before the main function.
- cba(callback) - Adds an "after" callback, which runs after the main function.
- cbr(callback) - Modifies the returned value from the main function.
- getFunction() - Returns the final composed function, ready to be called.
Methods
.main(callback)
This is the core method where you define the main function to be executed. This function will be called during the composed function execution.
import checklogrun from 'checklogrun';
const myFunction = (input) => { return `Processing ${input}`; };
checklogrun()
.main(myFunction)
.getFunction()("data");
// Output: "Processing data"
.cbb(callback)
This method allows you to define a callback that will run before the main function.
checklogrun()
.main(myFunction)
.cbb((input) => console.log(`Before Main with ${input}`))
.getFunction()("data");
// Output:
// "Before Main with data"
// "Processing data"
.cba(callback)
Similar to .cbb(), but the callback will run after the main function.
checklogrun()
.main(myFunction)
.cba((returnValue, input) => console.log(`After Main: ${returnValue}, Input: ${input}`))
.getFunction()("data");
// Output:
// "Processing data"
// "After Main: Processing data, Input: data"
.cbr(callback)
This method modifies the returned value of the main function. It is useful when you need to adjust the output.
const returnValueModifier = (returnValue, input) => return `Modified: ${returnValue} on ${input}`;
checklogrun()
.main(myFunction)
.cbr(returnValueModifier)
.getFunction()("data");
// Output: "Modified: Processing data on data"
Examples
Here’s a simple example of chaining callbacks together:
import checklogrun from 'checklogrun'
const mainFunction = (input) => `Main Executed on ${input}`;
const beforeCallback = (input) => console.log(`Before with ${input}`);
const afterCallback = (returnValue, input) => console.log(`After with ${returnValue} and ${input}`);
const returnModifier = (returnValue, input) => `Modified ${returnValue} for ${input}`;
const composedFunction = checklogrun()
.main(mainFunction)
.cbb(beforeCallback)
.cba(afterCallback)
.cbr(returnModifier)
.getFunction();
console.log(composedFunction("data"));
// Output:
// "Before with data"
// "Main Executed on data"
// "After with Main Executed on data and data"
// "Modified Main Executed on data for data"
Contributing
Contributions are welcome! If you would like to contribute to CheckLogRun, please fork the repository and create a pull request with your changes.