{"library":"regexparam","title":"Tiny Route Pattern to RegExp Utility","description":"regexparam is a minimalist (399B) utility designed to convert URL-like route patterns (e.g., `/users/:id`) into JavaScript `RegExp` objects, extracting parameter keys in the process. It serves as a more lightweight and focused alternative to libraries like `path-to-regexp`. The current stable version is `3.0.0`. Releases occur periodically, addressing bug fixes and introducing minor breaking changes, typically every few months for major versions. Key differentiators include its extremely small footprint and its specific focus on basic routing patterns: static, parameter (with or without suffixes), optional parameters, wildcards, and optional wildcards. Unlike more comprehensive routing solutions, regexparam only handles parsing string patterns and does not manage a `keys` dictionary directly or mutate variables. It ships with CommonJS, ESModule, and UMD formats and includes TypeScript type definitions.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install regexparam"],"cli":null},"imports":["import { parse } from 'regexparam'","import { inject } from 'regexparam'","import type { Result } from 'regexparam'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse, inject } from 'regexparam';\n\n// A helper function to extract parameters from a matched path\nfunction exec(path: string, result: ReturnType<typeof parse>): Record<string, string | null> {\n  let i = 0;\n  const out: Record<string, string | null> = {};\n  const matches = result.pattern.exec(path);\n\n  if (!matches) {\n    return {}; // No match\n  }\n\n  while (i < result.keys.length) {\n    // Matches array index starts from 1 for captured groups\n    out[result.keys[i]] = matches[++i] || null;\n  }\n  return out;\n}\n\n// Example 1: Parsing a route with optional parameters\nconst route1 = parse('/books/:genre/:title?');\nconsole.log('Route 1 pattern:', route1.pattern.source);\nconsole.log('Route 1 keys:', route1.keys);\n\nconst path1a = '/books/horror';\nconsole.log(`Matching \"${path1a}\":`, route1.pattern.test(path1a));\nconsole.log(`Extracted params for \"${path1a}\":`, exec(path1a, route1));\n\nconst path1b = '/books/science-fiction/dune';\nconsole.log(`Matching \"${path1b}\":`, route1.pattern.test(path1b));\nconsole.log(`Extracted params for \"${path1b}\":`, exec(path1b, route1));\n\n// Example 2: Injecting parameters into a route\nconst injectedPath1 = inject('/users/:id', { id: 'alice' });\nconsole.log('Injected path (user alice):', injectedPath1);\n\nconst injectedPath2 = inject('/posts/:slug/*', { slug: 'my-post', '*': 'comments/123' });\nconsole.log('Injected path (post with wildcard):', injectedPath2);\n\n// Example 3: Handling missing non-optional parameters during injection\nconst incompleteInjection = inject('/products/:category/:item', { category: 'electronics' });\nconsole.log('Incomplete injection:', incompleteInjection); // Missing 'item' parameter\n","lang":"typescript","description":"Demonstrates how to parse a route pattern into a RegExp and keys, test paths against it, extract parameters, and inject parameters into a route string using `parse` and `inject`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}