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 🙏

© 2024 – Pkg Stats / Ryan Hefner

remove-filename-hash

v0.0.8

Published

Swaps the substring of a filename or URL that contains a hash with a replacement string.

Downloads

19

Readme

remove-filename-hash

Installation

$ npm install remove-filename-hash

Background

Hashed URLs and filenames are commonly created as part of a production build process for modern web applications. By designating a portion of a URL to include a series of characters representing a hash of the file's contents, you can ensure that once a given URL is cached, you never have to revalidate it again against a remote server.

The hash values are frequently represented as lowercase hexadecimal characters, although alternative character sets, like URL-safe Base 64 is sometimes be used as well.

The problem

There are times when you may need to either remove the hash from a URL or filename string entirely, or replace it with some constant placeholder value, like [hash].

One motivating use case involves performing runtime comparisons of two URLs inside of a service worker, to see if they represent different versions of the same underlying assets. Another, less esoteric use case, has to do with writing integration tests for web apps that need to interrogate cache state. Direct string comparisons of the set of cached URLs against a known-good set won't work unless the URLs can first be normalized, to remove hashes.

Usage

import {
	createRegExp,
	HEX_CHARACTER_CLASS,
	removeHash,
} from 'remove-filename-hash';

// Match a hash of 8 hex characters, preceded and followed by '.'
// This pattern matches the default asset hashing used by many bundlers.
const eightCharHexRegExp = createRegExp({
	characters: HEX_CHARACTER_CLASS,
	size: 8,
	before: '.',
	after: '.',
});
// eightCharHexRegExp will be /\.[a-f0-9]{8}\./d
// Remove the hash and replace it with '[hash]'.
const replaced = removeHash({
	stringWithHash: 'http://example.com/main.abcd1234.js',
	replacement: '[hash]',
	regexps: eightCharHexRegExp,
});
// replaced will be 'http://example.com/main.[hash].js'

// Instead of using createRegexp(), you can provide any RegExp that contains one
// capture group (representing the string to be removed) and which has the 'd'
// flag enabled.
const manualRegExp = /([a-f0-9]{6}~)/d;
// Remove the 6 hex character hash and the ~, using '' as the replacement.
const removed = removeHash({
	stringWithHash: 'http://example.com/abc123~main.js',
	replacement: '',
	regexps: manualRegExp,
});
// removed will be 'http://example.com/main.js'

API

removeHash()

Returns a string with the hash value (determined by one of the matching RegExps) swapped out for the replacement placeholder string.

This will throw an error if an invalid RegExp (one without a single capture group, or one without match indices enabled) is used, or if there is no instance of the hash to remove.

removeHash({
  // A string which contains a hash that needs to be removed/replaced.
  stringWithHash: string,
  // The replacement value, or an empty string to remove the hash.
  replacement: string,
  // Either a single RegExp, or an array of RegExps.
  // The first matching pattern is used.
  regexps: RegExp | Array<RegExp>,
});

createRegExp()

Returns a RegExp that can then be passed to removeHash(). Usage of this function is optional, but it will ensure that your RegExp has the proper capture group and match indices enabled.

createRegExp({
	// A string representing the set of characters that used for the hash. See
	// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes
	characters: string,
	// The number of characters in each hash.
	size: number,
	// Characters that always precede the hash, or an empty string.
	// This string won't be replaced.
	before: string,
	// Characters that always follow the hash, or an empty string.
	// This string won't be replaced.
	after: string,
});

HEX_CHARACTER_CLASS

A constant value of '[0-9a-f]' that can be passed to createRegExp().

BASE64_URL_CHARACTER_CLASS

A constant value of '[A-Za-z0-9-_]' that can be passed to createRegExp().