Laser-Mace is my personal Swiss army knife of tools—a collection of utilities that I’ve found indispensable over the years. Instead of reinventing the wheel or endlessly copy-pasting code, I drop anything useful here for easy reuse. Whether it’s Crono, a sweet game loop manager to handle frame rates and control how often certain things run, createLazyState, a super convenient factory function for smart caching, or PeerNet, my favorite tool that takes the hassle out of creating peer-to-peer networks, Laser-Mace has it all.
- greetLaserMace(): A simple function to greet you from the world of Laser-Mace.
console.log(greetLaserMace()); // Output: "Hello from LaserMace!"
- Crono (ChronoTrigger): A game loop manager with FPS control.
Start(): Begin the game loop.Stop(): Halt the loop.setLoop(loopFunction): Define the loop function.runAt(fpsTarget, callback): Run a callback at a specified FPS.CurrentFPS(): Get the current frames per second.
- createLazyState(definitions): Efficiently manage state with caching strategies (e.g., "always", "once", "timed").
- rng(low, high, decimals): Generate random numbers with optional decimal precision.
- randomItem(collection): Pick a random item from an array.
- log(level, message, keywords, ...data): Advanced logging with log levels and keyword-based filters.
- Log levels:
offerrorwarningdebug
- storage.save(key, value): Save data to
localStorage. - storage.load(key, defaultValue): Load data from
localStoragewith a fallback. - storage.listKeys(): List all keys in
localStorage.
- sendRequest(url, payload, timeoutMs?): Make HTTP requests with JSON payloads. Timeout defaults to
5000ms. - getKeyNameByValue(obj, value): Retrieve an object's key by its value.
- attachOnClick(id, fn, params, callback): Bind functions to elements by their
id.
Laser-Mace is written in TypeScript and the sources need to be compiled before use.
npm install
npm run build # or rely on `npm install` triggering `prepare`The dist/ directory is generated during the build and is excluded from git.
Run the unit tests using Jest, which is installed as a dev dependency.
npm install
npm testimport { greetLaserMace, Crono, createLazyState, rng } from 'laser-mace';
// Greet from Laser-Mace
console.log(greetLaserMace());
// Use Crono for a game loop
Crono.setLoop((time) => {
console.log(`Game running at: ${Crono.CurrentFPS()} FPS`);
});
Crono.Start();
// Random number generator
console.log(rng(1, 100));
// Create and manage lazy state
const state = createLazyState({
example: ["timed", () => Date.now(), 5000],
});
console.log(state.example);Got a crazy idea? Open a PR and let’s build the Laser-Mace universe together.
MIT – Go wild.
Laser-Mace: Because every project deserves a little fun and chaos.