gandalf-gate: guard + gatekeeper utilities
This commit is contained in:
parent
ed9d765ea1
commit
d11176429b
10
README.md
10
README.md
|
|
@ -1,3 +1,7 @@
|
||||||
# gandalf-gate
|
# gandalf-gate 🧙
|
||||||
|
A tiny guard utility.
|
||||||
A tiny guard utility — you shall not pass invalid input.
|
```js
|
||||||
|
const { youShallNotPass, gatekeeper } = require('@community/gandalf-gate');
|
||||||
|
youShallNotPass(user.isAdmin, 'admins only');
|
||||||
|
const gate = gatekeeper(3); // allow 3, then throws
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// gandalf-gate — a tiny guard utility. "You shall not pass" invalid input.
|
||||||
|
function youShallNotPass(condition, message = 'You shall not pass!') {
|
||||||
|
if (!condition) throw new Error(message);
|
||||||
|
}
|
||||||
|
// A simple gatekeeper: allow at most `max` calls, then refuse.
|
||||||
|
function gatekeeper(max) {
|
||||||
|
let count = 0;
|
||||||
|
return () => { if (count++ >= max) throw new Error('You shall not pass! (limit reached)'); };
|
||||||
|
}
|
||||||
|
module.exports = { youShallNotPass, gatekeeper };
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
{ "name": "@community/gandalf-gate", "version": "1.0.0", "main": "gate.js", "license": "MIT",
|
||||||
|
"description": "A tiny guard utility — you shall not pass invalid input." }
|
||||||
Loading…
Reference in New Issue