Features
FunctionWrapper provides over 80+ static methods to extend and wrap functions. Some of the core capabilities include:
-
Logging and Monitoring
log(fn): Logs function calls and results.time(fn): Measures execution duration.profile(fn): Logs execution stats with timestamp.stats(fn): Tracks call count and average execution time.
-
Control Flow Enhancements
retry(fn, retries, delayMs): Automatically retries a function on failure.once(fn),oncePerArgs(fn): Execute only once globally or per argument set.after(fn, n),before(fn, n): Control execution after or before a number of calls.limit(fn, max): Limits total number of calls.lock(fn): Prevents concurrent execution.
-
Asynchronous Handling
debounce(fn, wait): Delays execution until idle.throttle(fn, wait): Limits execution frequency.delayFn(fn, delay),delayResult(fn, ms),delayEach(fn, ms): Flexible delayed execution.cancelable(fn): Creates cancelable promises.queue(fn): Serializes asynchronous calls.
-
Data Manipulation
memo(fn): Caches results for identical arguments.transformOutput(fn, transformer): Post-process function results.mask(fn, masker): Preprocess arguments before calling the function.randomizeArgs(fn): Shuffles arguments randomly.randomBehavior(fn, behaviors): Introduces probabilistic behaviors.
-
Validation and Safety
validate(fn, validator): Validates arguments.ensure(fn, validator): Validates output.catch(fn, onError, fallback): Graceful error handling.sandbox(fn, timeout): Safe execution with error handling and time limit.safeJson(fn): Safe JSON parsing and handling.
-
Functional Utilities
chain(fn): Chain multiple transformations in a fluent interface.pipe(...fns): Compose functions in a pipeline.tap(fn, tapFn): Tap into function results without affecting output.hook(fn, { before, after }): Run pre- and post-execution hooks.feedback(fn, logger): Collect input/output logs.
-
Simulation and Testing
simulate(fn, options): Simulate failures, delays, and data corruption.test(fn, testCases): Run predefined tests on a function.predict(fn, modelFn): Attach predictive models to results.
-
Batching and History
batch(fn, chunkSize, cb): Execute functions in batches.history(fn): Track inputs and outputs withgetHistoryandclearHistory.watch(fn): Subscribe to function call results.changelog(fn): Track changes in object outputs.
-
Advanced Features
undoable(fn, inverseFn): Support undo operations.locale(fn, formatter): Apply locale-based input/output formatting.smartIdle(fn, options): Execute during idle periods with cancellation.eventual(fn, checkFn, interval): Poll until a condition is met.timeLimit(fn, timeout): Force a maximum execution duration.