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
JavaScript
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.
*/