npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@genway-ai/youtube-link-utils

v1.2.0

Published

Tiny browser-first utilities to extract and validate YouTube video links.

Readme

youtube-link-utils

Tiny browser-first utilities to extract and validate YouTube video links.

Installation

npm install @genway-ai/youtube-link-utils

Usage

Node.js / ES Modules

import {
  isValidYouTubeUrl,
  extractYouTubeVideoId,
  convertYouTubeToEmbedUrl,
  getYouTubeVideoInfo,
} from '@genway-ai/youtube-link-utils';

// Basic validation
const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
console.log(isValidYouTubeUrl(url)); // true

// Extract video ID
const videoId = extractYouTubeVideoId(url);
console.log(videoId); // 'dQw4w9WgXcQ'

// Convert to embed URL
const embedUrl = convertYouTubeToEmbedUrl(url);
console.log(embedUrl); // 'https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1...'

// Fetch video information (requires axios)
const videoInfo = await getYouTubeVideoInfo(url);
console.log(videoInfo?.title); // Video title

CommonJS

const {
  isValidYouTubeUrl,
  extractYouTubeVideoId,
} = require('@genway-ai/youtube-link-utils');

const isValid = isValidYouTubeUrl('https://youtu.be/dQw4w9WgXcQ');
console.log(isValid); // true

Browser (CDN)

<script src="https://unpkg.com/@genway-ai/youtube-link-utils/dist/index.browser.js"></script>
<script>
  const { isValidYouTubeUrl, extractYouTubeVideoId } = YouTubeLinkUtils;

  const isValid = isValidYouTubeUrl(
    'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
  );
  console.log(isValid); // true
</script>

API Reference

isValidYouTubeUrl(url: string): boolean

Validates if a URL is a valid YouTube URL format.

extractYouTubeVideoId(url: string): string | null

Extracts the video ID from a YouTube URL.

convertYouTubeToEmbedUrl(url: string): string | null

Converts a YouTube URL to an embed URL.

getYouTubeVideoInfo(url: string): Promise<VideoInfo | null>

Fetches video information from YouTube's oEmbed API. Requires axios.

validateYouTubeUrlComprehensive(url: string, checkExists?: boolean): Promise<YouTubeValidationResult>

Comprehensive validation with optional existence check.

React Hook

useYouTubeValidationLoading()

A React hook for validating YouTube URLs with loading states.

// Import the hook directly from the react subpath
import { useYouTubeValidationLoading } from '@genway-ai/youtube-link-utils/react';

function VideoForm() {
  const { isValidating, validateWithLoading, clearValidation } =
    useYouTubeValidationLoading();
  const [url, setUrl] = useState('');
  const [isValid, setIsValid] = useState(null);

  const handleValidate = async () => {
    const result = await validateWithLoading(url);
    setIsValid(result);
  };

  const handleClear = () => {
    clearValidation();
    setUrl('');
    setIsValid(null);
  };

  return (
    <div>
      <input
        value={url}
        onChange={e => setUrl(e.target.value)}
        placeholder="Enter YouTube URL"
        disabled={isValidating}
      />
      <button onClick={handleValidate} disabled={isValidating}>
        {isValidating ? 'Validating...' : 'Validate'}
      </button>
      <button onClick={handleClear}>Clear</button>
      {isValid !== null && (
        <p>{isValid ? 'Valid YouTube URL!' : 'Invalid YouTube URL'}</p>
      )}
    </div>
  );
}

The hook returns:

  • isValidating: Boolean indicating if validation is in progress
  • validateWithLoading: Async function that validates a URL and returns a boolean
  • clearValidation: Function to clear the current validation state

Peer Dependencies

This package requires axios for network-related functions:

npm install axios

For React hook usage, install React:

npm install react

For browser-only usage (validation/parsing), neither axios nor React are required.

Supported URL Formats

  • https://www.youtube.com/watch?v=VIDEO_ID
  • https://youtu.be/VIDEO_ID
  • https://www.youtube.com/embed/VIDEO_ID
  • https://www.youtube.com/shorts/VIDEO_ID
  • https://www.youtube.com/playlist?list=PLAYLIST_ID

TypeScript

This package includes full TypeScript definitions.

import type { YouTubeValidationResult } from '@genway-ai/youtube-link-utils';