singulure
Version:
A tiny JavaScript utility to ensure a function can only be called a single time — forever.
60 lines (35 loc) • 1.37 kB
Markdown
> A tiny JavaScript utility to ensure a function can only be called a single time — forever.
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.
```bash
npm install singulure
````
```js
import { singulure } from 'singulure';
const initialize = singulure(() => {
console.log('Initialized!');
});
initialize(); // Logs "Initialized!"
initialize(); // Does nothing on subsequent calls
```
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 `fn` once and then always returns the first result.
Run the test suite with:
```bash
npm test
```
Make sure you have a `test.js` file in the project root.
MIT © Geoffrey Callaghan
---
This project is proudly sponsored by [FabForm.io](https://fabform.io) — build amazing forms effortlessly.
```