gandalf-gate: guard + gatekeeper utilities

This commit is contained in:
gandalf 2026-08-07 17:01:52 +02:00
parent ed9d765ea1
commit d11176429b
4 changed files with 21 additions and 3 deletions

View File

@ -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
```

10
gate.js Normal file
View File

@ -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 };

2
package.json Normal file
View File

@ -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." }

2
test.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
node -e "const {youShallNotPass,gatekeeper}=require('./gate'); youShallNotPass(true); let g=gatekeeper(1); g(); try{g();process.exit(1)}catch{console.log('ok')}"