johnutilsjs
v1.0.0
Published
JavaScript utility functions for common tasks
Downloads
5
Maintainers
Readme
johnutilsjs
JavaScript utility functions for common tasks
Installation
npm install johnutilsjsUsage
Import the entire library
import { parseUrl, detectPrefix } from 'johnutilsjs';Import specific utilities
import { parseUrl } from 'johnutilsjs/urlParser';API
URL Parser
Deployment-agnostic URL parsing that works with or without URL prefixes.
parseUrl(pathname, validRoutes)
Parses a URL pathname and extracts prefix, route, and clean path.
Parameters:
pathname(string): The URL pathname to parsevalidRoutes(string[]): Array of valid route names
Returns: Object with:
prefix(string): URL prefix if detected (e.g., '/myapp')route(string|null): Valid route name if foundcleanPath(string): Normalized path for navigation
Example:
import { parseUrl } from 'johnutilsjs';
const validRoutes = ['home', 'about', 'contact'];
// Development mode (no prefix)
parseUrl('/home', validRoutes);
// { prefix: '', route: 'home', cleanPath: '/home' }
// Production mode (with prefix)
parseUrl('/myapp/about', validRoutes);
// { prefix: '/myapp', route: 'about', cleanPath: '/myapp/about' }
// Invalid route redirects to root
parseUrl('/invalid', validRoutes);
// { prefix: '', route: null, cleanPath: '/' }
// Prefix with invalid route redirects to prefix root
parseUrl('/myapp/invalid', validRoutes);
// { prefix: '/myapp', route: null, cleanPath: '/myapp/' }detectPrefix(pathname, segments, validRoutes)
Detects if a pathname contains a URL prefix.
Parameters:
pathname(string): The URL pathnamesegments(string[]): Path segments (frompathname.split('/').filter(Boolean))validRoutes(string[]): Array of valid route names
Returns: String containing the detected prefix or empty string
Example:
import { detectPrefix } from 'johnutilsjs';
const validRoutes = ['home', 'about'];
detectPrefix('/myapp/home', ['myapp', 'home'], validRoutes);
// '/myapp'
detectPrefix('/home', ['home'], validRoutes);
// ''Development
Run tests
npm testLicense
MIT
