# remove-filename-hash

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

Latest version **0.0.8** (published 2022-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install remove-filename-hash
pnpm add remove-filename-hash
yarn add remove-filename-hash
bun add remove-filename-hash
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2022-09-02 |
| First published | 2022-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 0 |
| Unpacked size | 29.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jeff Posnick |
| Maintainers | jeffposnick |
| Keywords | asset hash, content hash, hash |

## Links

- npm: https://www.npmjs.com/package/remove-filename-hash
- Repository: https://github.com/jeffposnick/remove-filename-hash
- Homepage: https://github.com/jeffposnick/remove-filename-hash#readme
- Issues: https://github.com/jeffposnick/remove-filename-hash/issues
- npm.io page: https://npm.io/package/remove-filename-hash

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2022-09-02
- 0.0.7 — 2022-08-20
- 0.0.6 — 2022-08-20
- 0.0.5 — 2022-08-20
- 0.0.4 — 2022-08-20
- 0.0.3 — 2022-08-20
- 0.0.2 — 2022-08-20

## README

# remove-filename-hash

## Installation

```sh
$ npm install remove-filename-hash
```

## Background

[Hashed URLs](https://bundlers.tooling.report/hashing/) 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](https://www.rfc-editor.org/rfc/rfc4648#section-5) 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](https://jeffy.info/2021/10/10/smart-caching-hashes.html)
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](https://github.com/jeffposnick/yt-playlist-notifier/blob/add56f0b9c9d237a57b0d9126dd7b250ddefd910/tests/sw.spec.ts)
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

```js
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.

```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://v8.dev/features/regexp-match-indices) enabled.

```js
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()`.

---
_Source: https://npm.io/package/remove-filename-hash · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
