or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-url-superb

Check if a string is a URL

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-url-superb@6.1.x

To install, run

npx @tessl/cli install tessl/npm-is-url-superb@6.1.0

index.mddocs/

is-url-superb

is-url-superb provides a simple and reliable utility for validating whether a given string is a valid URL. It leverages the native JavaScript URL constructor for validation, ensuring robust and standards-compliant URL checking with support for both strict and lenient validation modes.

Package Information

  • Package Name: is-url-superb
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install is-url-superb

Core Imports

import isUrl from 'is-url-superb';

For importing types (when needed):

import isUrl, { type Options } from 'is-url-superb';

Basic Usage

import isUrl from 'is-url-superb';

// Basic URL validation
isUrl('https://sindresorhus.com');
//=> true

isUrl('unicorn');
//=> false

// Lenient mode - allows URLs without protocol
isUrl('example.com');
//=> false

isUrl('example.com', {lenient: true});
//=> true

Capabilities

URL Validation

Validates whether a string is a properly formatted URL using the native URL constructor.

/**
 * Check if a string is a URL
 * @param url - The string to validate as a URL
 * @param options - Configuration options (optional)
 * @returns Returns true if the string is a valid URL, false otherwise
 * @throws Throws TypeError if input is not a string
 */
function isUrl(url: string, options?: Options): boolean;

Parameters:

  • url (string): The string to validate as a URL
  • options (Options, optional): Configuration options, defaults to {lenient: false} if not provided

Behavior:

  • Trims whitespace from the input string
  • Returns false if the string contains spaces
  • Uses native URL constructor for validation
  • In lenient mode, automatically prepends 'https://' to URLs without protocol
  • Strict mode (default) requires protocol to be present

Error Handling:

  • Throws TypeError if the input is not a string

Usage Examples:

import isUrl from 'is-url-superb';

// Valid URLs
isUrl('https://sindresorhus.com');           // => true
isUrl('  https://sindresorhus.com  ');       // => true (trims whitespace)
isUrl('http://example.com/path');            // => true
isUrl('ftp://files.example.com');            // => true

// Invalid URLs  
isUrl('unicorn');                            // => false
isUrl('abc https://sindresorhus.com');       // => false (contains spaces)
isUrl('https://sindresorhus.com abc');       // => false (contains spaces)
isUrl('https://sindresorhus.com/abc def');   // => false (contains spaces)
isUrl('//sindresorhus.com');                 // => false (no protocol)

// Lenient mode examples
isUrl('//sindresorhus.com', {lenient: true});    // => true
isUrl('localhost', {lenient: true});              // => true
isUrl('192.168.0.1', {lenient: true});           // => true
isUrl('www.example.com', {lenient: true});       // => true

// Error cases
isUrl(123);                                  // => TypeError: Expected a string
isUrl(null);                                 // => TypeError: Expected a string

Types

/**
 * Configuration options for URL validation
 */
interface Options {
  /**
   * Allow URLs without a protocol by automatically prepending 'https://'
   * @default false
   */
  readonly lenient?: boolean;
}

Environment Requirements

  • Node.js: >= 12
  • Module System: ES Module (supports CommonJS via require)
  • Dependencies: None (uses native URL constructor)
  • Browser Support: Modern browsers supporting URL constructor