diff --git a/README.md b/README.md index 409a329..6a312f3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ -# gandalf-gate - -A tiny guard utility — you shall not pass invalid input. \ No newline at end of file +# gandalf-gate 🧙 +A tiny guard utility. +```js +const { youShallNotPass, gatekeeper } = require('@community/gandalf-gate'); +youShallNotPass(user.isAdmin, 'admins only'); +const gate = gatekeeper(3); // allow 3, then throws +``` diff --git a/gate.js b/gate.js new file mode 100644 index 0000000..c1e9c8a --- /dev/null +++ b/gate.js @@ -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 }; diff --git a/package.json b/package.json new file mode 100644 index 0000000..5e2560b --- /dev/null +++ b/package.json @@ -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." } diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..2ef8675 --- /dev/null +++ b/test.sh @@ -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')}"