NameReservers is a TypeScript/JavaScript utility for managing and validating reserved names, usernames, or strings in your application. It supports exact matches, prefix/suffix rules, contains rules, and regex rules, along with a bypass system for flexible exclusions.
It is ideal for platforms where you want to prevent users from choosing restricted names such as admin, root, system words, or any sensitive identifiers.
- Installation
- Usage
- Creating a Guard
- Rule System
- API Methods
- Advanced Examples
- Bypass System
- Debug Helpers
- Edge Cases
- Contribution
npm install @uhpenry/guard-names
# or
yarn add @uhpenry/guard-names
# or
pnpm add @uhpenry/guard-namesimport { NameReservers } from '@uhpenry/guard-names';
// Create a new guard
const guard = new NameReservers(['admin*', '*panel', '*test*']);import { NameReservers } from '@uhpenry/guard-names';
const guard = new NameReservers(
[
'root', // Exact match
'admin*', // Prefix match
'*panel', // Suffix match
'*sys*', // Contains match
{ pattern: '^test[0-9]+$', mode: 'regex' }, // Regex match
],
[
'*panel', // bypass list
],
{
includeBypass: false,
}
);newList– array of rules to add (strings with wildcards or objects withpatternandmode)bypassList– array of strings to bypass unlessincludeBypass = trueoptions.includeBypass– iftrue, bypass rules are ignored
NameReservers supports 5 types of rules:
| Mode | Format Example | Description |
|---|---|---|
| exact | "admin" |
Blocks only the exact name "admin" |
| prefix | "admin*" |
Blocks any name starting with "admin" |
| suffix | "*panel" |
Blocks any name ending with "panel" |
| contains | "*sys*" |
Blocks any name containing "sys" anywhere inside |
| regex | {pattern: '^test[0-9]+$', mode: 'regex'} |
Blocks any name matching a custom regular expression |
⚠️ Prefix ="admin*"→ blocks"admin123"⚠️ Suffix ="*panel"→ blocks"controlpanel"⚠️ Contains ="*sys*"→ blocks"mysystem"
Checks if a name is reserved.
guard.isReserved('admin'); // true
guard.isReserved('systempanel'); // false if bypassedReturns the rule that blocks a given name, or null if none.
guard.getBlockingRule('admin123');
// { pattern: 'admin', mode: 'prefix' }Returns all active rules (excluding bypassed rules).
includeNewLists – if true, also includes user-added rules.
guard.getReserved(); // Returns active normalized rulesReturns all active rules as a comma-separated string.
guard.getAllReservesCsv(); // "root,admin,panel,..."Returns all active rules as plain text, one per line.
guard.getAllReservesPlainText();
/*
root
admin
panel
...
*/Returns all active rules as a JSON array of strings (patterns).
guard.getAllReservesJson();
// ["root","admin","panel",...]Returns only custom rules added by the user.
Returns the list of rules in the bypass list.
Returns whether the guard ignores the bypass list (true) or not (false).
const guard = new NameReservers(
['admin*', '*panel', '*test*', { pattern: '^user[0-9]+$', mode: 'regex' }],
['*panel'],
{ includeBypass: false }
);
// Exact match
console.log(guard.isReserved('root')); // true
// Prefix match
console.log(guard.isReserved('admin123')); // true
// Suffix match
console.log(guard.isReserved('controlpanel')); // false (bypassed)
// Contains match
console.log(guard.isReserved('mysystem')); // true
// Regex match
console.log(guard.isReserved('user42')); // trueThe bypass system allows you to ignore certain rules:
const guard = new NameReservers(['*panel'], ['*panel'], {
includeBypass: false,
});
console.log(guard.isReserved('controlpanel')); // false → bypassed
const guard2 = new NameReservers(['*panel'], ['*panel'], {
includeBypass: true,
});
console.log(guard2.isReserved('controlpanel')); // true → bypass ignoredguard.getUserAdded(); // returns user-added rules
guard.getBypassList(); // returns bypass list
guard.getIncludeBypass(); // returns true/false- Case insensitive:
"Admin"is treated the same as"admin" - Multiple matches: the first rule that applies blocks the name
- Regex rules must be valid JS regex
- Bypass list only affects active rules unless
includeBypass = true
If you want to improve NameReservers:
git clone https://github.com/uhpenry/guard-names.git
pnpm install
pnpm log