get-youtube-id
Extracts the video ID from any YouTube URL format using pattern matching with an optional fuzzy fallback.
Repository Health
Technical Analysis
get-youtube-id is a lightweight, dependency-free JavaScript utility that extracts an 11-character YouTube video ID from a URL string. It recognizes the handful of URL shapes YouTube has used over the years — watch?v=, youtu.be/, /embed/, /v/ — and falls back to a fuzzy token-scanning pass that looks for any 11-character segment resembling a video ID when no known pattern matches.
Shipped as a single UMD module with a bundled TypeScript declaration file, it works equally well in Node.js, in the browser via a global, or as an AMD module, making it a drop-in helper for any project that needs to normalize YouTube links — video embeds, oEmbed lookups, thumbnail generation, or link validation — without writing regex by hand.
What You Get
- A single exported function that takes a URL string and returns the 11-character video ID or null
- Support for the five common YouTube URL patterns: watch?v=, youtu.be/, embed/, /v/, and query-string variants with extra parameters
- An opt-in fuzzy matching pass that scans URL tokens for an 11-character ID when no known pattern matches
- Bundled TypeScript type declarations (index.d.ts) for type-safe usage without an extra @types package
- UMD build that works via CommonJS require, AMD define, or a browser global with no build step
Common Use Cases
- Normalizing user-submitted YouTube links before storing or rendering them
- Building a video embed component that accepts any YouTube URL shape and needs just the ID
- Validating that a pasted link is actually a YouTube video before making an oEmbed/API call
- Generating thumbnail URLs (e.g. img.youtube.com/vi/<id>) from an arbitrary YouTube link
Under The Hood
Architecture The entire implementation is a single UMD-wrapped function in index.js with no internal layering: it takes a URL and an options object, first attempts an ordered list of five regular expressions covering the known YouTube URL shapes, and, if none match and fuzzy mode is enabled (the default), falls back to splitting the URL on delimiter characters and returning the first 11-character token. There is no state, no I/O, and no dependency graph — the whole surface area is this one function, so there is nothing structural to break.
Tech Stack The package declares zero runtime dependencies; its only devDependency is an old pinned version of the tape test framework (~0.2.2). It ships as a hand-written UMD wrapper with no build tooling (no bundler, transpiler, or TypeScript compiler config) — the plain ES5-style JavaScript source is the published artifact, alongside a manually authored index.d.ts for TypeScript consumers.
Code Quality
Tests run directly via node test using tape, covering roughly thirty real-world YouTube URL variants collected from Stack Overflow plus a dedicated case for disabling fuzzy mode. There is no linter or formatter configuration in the repo and no CI workflow, so quality enforcement relies entirely on the test suite and manual review. Error handling is minimal by design — the function returns null on no match rather than throwing.
What Makes It Unique There is nothing technically novel here — it is a small, well-curated regex-and-fallback utility for a narrow, common problem. Its value is the curated set of real-world URL patterns and the pragmatic fuzzy-matching safety net, not any distinctive architecture or algorithm.