URL Value Replacer

1.0.0 · active · verified Sun Apr 19

The `replace-url-values` package, currently at version `1.0.0`, is designed as a focused utility to programmatically substitute specified values within a given URL string. It likely facilitates the dynamic generation of URLs by replacing placeholders in path segments (e.g., `:id`) or modifying existing query parameters. While specific details about its API and internal mechanisms are not publicly available through a readily identifiable npm registry entry or a public GitHub repository at the provided link, such utilities typically offer a simple function to map specific keys to their desired replacement values, returning a new URL string. The package is noted to ship with TypeScript types, suggesting good integration for type-safe development environments. Due to the lack of public information, its release cadence and key differentiators beyond its core functionality remain unclear, but it aims to provide a straightforward approach to a common web development task.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates replacing both path placeholders and query parameters in a URL string, including adding new parameters and handling URL encoding of replacement values.

import { replaceUrlValues } from 'replace-url-values';

// Example 1: Replacing placeholders in a URL path like '/products/:productId'
const urlTemplateWithPathParams = '/products/:productId/reviews/:reviewId';
const pathReplacements = {
  productId: '456',
  reviewId: '789'
};
const finalUrl1 = replaceUrlValues(urlTemplateWithPathParams, pathReplacements);
console.log('Replaced path URL:', finalUrl1);
// Expected output: /products/456/reviews/789

// Example 2: Replacing specific query parameter values in an existing URL
const urlWithQueryParams = 'https://example.com/search?q=apple&page=1';
const queryReplacements = {
  q: 'banana and orange', // Replace existing 'q'
  page: '2',              // Replace existing 'page'
  sort: 'desc'            // Add a new parameter 'sort'
};
const finalUrl2 = replaceUrlValues(urlWithQueryParams, queryReplacements);
console.log('Replaced query params URL:', finalUrl2);
// Expected output (order might vary, but parameters should be correct):
// https://example.com/search?q=banana%20and%20orange&page=2&sort=desc

// Example 3: Handling missing placeholders gracefully (assumption: no change for unmatched keys)
const incompleteTemplate = '/users/:userId/profile';
const partialReplacements = { userId: '101' };
const finalUrl3 = replaceUrlValues(incompleteTemplate, partialReplacements);
console.log('Partial replacements:', finalUrl3);
// Expected output: /users/101/profile

// Example 4: When no matches are found, the original URL is returned (assumption)
const noMatchUrl = 'https://example.com/data';
const noMatchReplacements = { unknownKey: 'value' };
const finalUrl4 = replaceUrlValues(noMatchUrl, noMatchReplacements);
console.log('No matches:', finalUrl4);

view raw JSON →