The Binder class implements the KoadzBinder interface, providing a singleton pattern implementation to manage and invoke functions dynamically by name. It is designed to store functions in a map and allows adding, retrieving, and executing these functions.
# Install package globally
npm install -g @koadz/binder@lastest
Getting the Instance of the Binder class:
import {
Binder, // Singleton instance
BinderClass, // Class
} from "@koadz/binder";
const binderRef = new Binder(); // Custom instance
Adding Functions to the Binder instance:
// Adding a single function
Binder.append("sum", (a: number, b: number) => a + b);
// Adding multiple functions
Binder.append({
subtract: (a: number, b: number) => a - b,
multiply: (a: number, b: number) => a * b,
});
Retrieving and Calling Functions:
const sumFunction = Binder.get("sum");
const result = sumFunction(5, 3); // result will be 8
// Directly calling a function
const result = Binder.call("multiply", 5, 3); // result will be 15
Listing Function names:
console.log(Binder.list()); // ["sum", "subtract", "multiply"]
Piping multiple Function names:
const initialData = {
users: [],
// other properties
};
binder.pipe(["getUser", "cacheUserData"], initialData);
Overriding Existing Functions:
// Overriding the "sum" function with a new implementation
Binder.override("sum", (a: number, b: number) => a * b);
// Now calling "sum" will use the new implementation
const result = Binder.call("sum", 5, 3); // result will be 15