UNPKG

executor-fn

Version:

A lightweight function wrapper with optional state, history, undo/redo, and reset support. Use like a normal function or unlock powerful time-travel features.

66 lines (45 loc) • 1.7 kB
import Executor from "../executor.js" // šŸ”¹ Example 1: Basic usage const add = Executor((a, b) => a + b) console.log("Add result:", add(10, 5)) // āž” 15 // šŸ”¹ Example 2: Stateful usage with history const calc = Executor((a, b) => a + b, { storeHistory: true, callNow: true, initialArgs: [2, 3] }) console.log("Initial value:", calc.value) // āž” 5 calc(10, 5) // āž” 15 calc(50, 1) // āž” 51 console.log("History:", calc.history) // āž” [5, 15, 51] calc.undo() console.log("After undo:", calc.value) // āž” 15 calc.redo() console.log("After redo:", calc.value) // āž” 51 calc.reset() console.log("After reset:", calc.value) // āž” 5 // 3ļøāƒ£ Default Arguments Support const greet = Executor((name = "Guest") => `Hello, ${name}!`, { callNow: true }) console.log(greet.value) // "Hello, Guest!" console.log(greet("Ada")) // "Hello, Ada!" // 4ļøāƒ£ Logging Current Value greet.log() // Logs: "Hello, Guest!" // 5ļøāƒ£ Straight Calling Executor(() => console.log("Straight call executed!"), { callNow: true }) /* Take caution 😬 if you're doing: const myGreet = greet("Ada") myGreet.log() OR console.log(myGreet) šŸ‘‰ Test code properly Also myGreet.value OR myGreet.[anything] would return undefined. Just call greet("Kelvin") and... greet.value = Updated value (in this case Kelvin) āœ… And greet.[anything] Would work šŸ˜Ž See advanced.js and advanced2.js and other examples and you will start bending functions to your will in no time. Also, Don't be scared. Executor, a function wrapper which does state management and time manipulation / time travel is super easy to learn. */