# truncate-path

> Smart and effective path truncation.

Latest version **1.0.3** (published 2023-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install truncate-path
pnpm add truncate-path
yarn add truncate-path
bun add truncate-path
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2023-09-05 |
| First published | 2021-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Anthony Mugendi |
| Maintainers | nguru |
| Keywords | path, file, truncate, string, shorten, filename, fs, directory |

## Links

- npm: https://www.npmjs.com/package/truncate-path
- Repository: https://github.com/mugendi/truncate-path
- Homepage: https://github.com/mugendi/truncate-path#readme
- Issues: https://github.com/mugendi/truncate-path/issues
- npm.io page: https://npm.io/package/truncate-path

## Dependencies (1)

- [is-url](https://npm.io/package/is-url.md) ^1.2.4

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2023-09-05
- 1.0.2 — 2023-09-05
- 1.0.1 — 2021-08-11
- 1.0.0 — 2021-08-11

## README

# Why

Sometimes you need to display truncated paths in your application. This module does just that. It also works perfectly for urls too!

```javascript
let truncatePath = require('truncate-path');

let file = '/your/very/very/long/path-to/my-modules/test-module/node_modules/chokidar/types/index.d.ts';

console.log("Local File:", truncatePath(file, 10));


let url="https://cdn.pixabay.com/download/audio/2023/04/03/audio_047543feac.mp3?filename=relaxing-145038.mp3&g-recaptcha-response=03ADUVZwAycgBPXsjVYD2lKzJNgZQdBFMeLK9oxBVA4hBSQCFriPkOOwz19q6Copxw41AnkONr4T7ekwCTWZDBcON-fbd4AHSJ6Xnro0h19hM5LaXXak6G6_o-y5pHhB3Bcx_mUMOa2dPQP4lXUBKqPN4idD2-e8G7UdhEHGcy-XT_8EIANjVtyHFO7SHHji4WkGGnJGYdQVQlKlXMkYrwrIAe7ILR6aWWFu-f42tbghVh3vmm9ls1tv1yKJEWqblV4bNL-CaK47yLiAZt7PlruJELzcsb6yVpDTOtLK4hznimRAUPKzl9DN7t7GrRacprKIAsJBnHZF4zCiqClxJsAPdXsJCI66frxZ0VP5EQvg19ur7mwoJC9rwhMRPuLh8fgulTt8i1svGn6zMsj737VwlP3ak4kYlWftnV1H2N3Wt9u1GsUSbVtVAMi2RKYg0vRIB_Pgzq2aEV7GtWcGJ87qTk-wLiQ_FIo9qg_XJaRBUnwEhThnxql95hpp7e1AMsD0LUlksSBuWmGvI2lJbj0Z3FIKWQnwU78Q&remote_template=1";

console.log("Long URL:", truncatePath(url));
```

This outputs:

```text
Local File: /your/[..]/index.d.ts

Long URL: //cdn.pixabay.com/download/audio/[..]/audio_047543feac.mp3

```

## API

### `truncatePath( file, [length] )`

The default length is 40 characters.

Please note that the path generated is not always the length defined but the closest (complete) path that can be generated.

By **complete path**, we mean that directory and file names are not split midway.

As such **"/this/that/those/file.ext"** can never be **"/this/that/th[...]/fi.ext"** or something like that. Because that would be a very ugly path indeed!

Enjoy!

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