singulure
v1.0.0
Published
A tiny JavaScript utility to ensure a function can only be called a single time — forever.
Maintainers
Readme
singulure
A tiny JavaScript utility to ensure a function can only be called a single time — forever.
Why singulure?
singulure is a lightweight, modern JavaScript utility that guarantees a function can be invoked only once, ever. Perfect for initialization routines, event guards, or any scenario where you want to prevent repeated calls.
Written as an ES module with zero dependencies, singulure is minimal, performant, and straightforward to use both in frontend and backend projects.
Installation
npm install singulureUsage
import { singulure } from 'singulure';
const initialize = singulure(() => {
console.log('Initialized!');
});
initialize(); // Logs "Initialized!"
initialize(); // Does nothing on subsequent callsAPI
singulure(fn: Function): Function
Wraps the function fn so it can only be called once. All future calls return the cached result from the first call.
- fn — The function to wrap.
- Returns — A new function that invokes
fnonce and then always returns the first result.
Testing
Run the test suite with:
npm testMake sure you have a test.js file in the project root.
License
MIT © Geoffrey Callaghan
Sponsorship
This project is proudly sponsored by FabForm.io — build amazing forms effortlessly.
