Fast and robust URL validator
This library provides a faster and more robust URL validation compared to is-url-superb. It uses optimized validation logic with early returns and efficient regex patterns to achieve better performance.
- ⚡ Fast - Optimized for performance with early validation checks
- 🛡️ Robust - Comprehensive validation including hostname, protocol, and structure checks
- 🎯 Flexible - Configurable options for different use cases
- 📦 Small - Zero dependencies
- 🔧 TypeScript - Full TypeScript support with type definitions
- 🌐 ESM & CommonJS - Supports both module systems
npm install swift-url-checkimport isUrl from 'swift-url-check';
isUrl('https://opensly.in');
//=> true
isUrl('not a url');
//=> false
isUrl('example.com');
//=> false
isUrl('example.com', { lenient: true });
//=> trueThis package is ESM-only. For CommonJS support, use dynamic import:
const isUrl = (await import('swift-url-check')).default;
isUrl('https://example.com');
//=> trueimport isUrl, { IsUrlOptions } from 'swift-url-check';
const options: IsUrlOptions = {
lenient: true,
allowLocal: false
};
isUrl('https://example.com', options);
//=> trueReturns true if the string is a valid URL, false otherwise.
Type: string
The string to validate as a URL.
Type: object
Type: boolean
Default: false
Allow URLs without protocol. When true, strings like "example.com" will be considered valid.
isUrl('example.com');
//=> false
isUrl('example.com', { lenient: true });
//=> trueType: boolean
Default: true
Allow localhost and local IP addresses. When false, localhost, 127.0.0.1, 192.168.x.x, 10.x.x.x, etc. will be rejected.
isUrl('http://localhost:3000');
//=> true
isUrl('http://localhost:3000', { allowLocal: false });
//=> falseType: string[]
Default: ['http:', 'https:']
Specify which protocols are considered valid.
isUrl('ftp://example.com');
//=> false
isUrl('ftp://example.com', { protocols: ['ftp:', 'ftps:'] });
//=> trueThe validator checks for:
- Valid protocol format
- Proper hostname structure
- No whitespace in URLs
- Valid domain parts (labels)
- Proper TLD format
- IPv4 and IPv6 support
- Port number validation
- Path, query, and hash support
Run tests:
npm testRun tests with coverage:
npm run test:coverageWatch mode for development:
npm run test:watchThe library has comprehensive test coverage with 47 test cases covering:
- Basic URL validation
- URL components (paths, query strings, hash fragments, ports)
- Subdomains and IP addresses
- All configuration options
- Edge cases and error conditions
Run the benchmark:
npm run benchmarkCompare with is-url-superb:
npm run benchmark:compare1.89x faster than is-url-superb (88.5% improvement)
swift-url-check: 3,234,110 ops/sec (0.309µs per operation)
is-url-superb: 1,715,301 ops/sec (0.583µs per operation)
This library is optimized for speed with:
- Early type checking
- Fast-path validations before URL parsing
- Efficient regex patterns
- Minimal allocations
| Feature | swift-url-check | is-url-superb |
|---|---|---|
| Performance | ⚡ 1.89x Faster | Standard |
| TypeScript | ✅ Built-in | ❌ Requires @types |
| ESM Support | ✅ ESM-only | ✅ ESM-only |
| Local URL Control | ✅ Configurable | ❌ No option |
| Protocol Control | ✅ Configurable | ❌ Fixed |
| Dependencies | 0 | 1 (is-url) |
MIT