Stop wasting your life on slash normalization.
Works everywhere ("everywhere" means in browsers too).
Zero dependencies because why would I need any?
npm install unslashimport unslash, { s, slash } from 'unslash';
// Just join some paths. No magic.
s('path', 'to', 'file');
// → 'path/to/file'
// Need a trailing slash? Done.
s(/t/, 'path', 'to', 'file');
// → 'path/to/file/'
// Don’t want it? Gone.
s(/!t/, 'path/to/file/');
// → 'path/to/file'
// Want a leading slash? Here you go.
s(/l/, 'path', 'to', 'file');
// → '/path/to/file'
// Need to collapse that mess? Fixed.
s(/c/, 'path///to', 'this//file');
// → 'path/to/this/file'
// Want to force forward slashes? Me too.
s(/f/, 'path\\to', 'this/file');
// → 'path/to/this/file'
// Need Windows paths? What is wrong with you?!
s(/!f/, 'path', 'to', 'file');
// → 'path\to\file'
// Combine them. Because you can.
s(/tlfc/, 'path\\\\to\\file');
// → '/path/to/file/'
s(/!t!l!fc/, '/path///to/file/');
// → 'path\to\file'
// Your precious protocols are safe — don’t worry.
s(/tlfc/, 'http://example.com', 'path', 'to', 'file');
// → 'http://example.com/path/to/file/'For all DRY lovers like me, you can create reusable formatters:
// Create a formatter
const normalize = s(/fc/); // Force forward, collapse
// Use it
normalize('path\\to\\file');
// → 'path/to/file'
// Extend it on the fly if you're feeling spicy
normalize(/t/, 'path\\to\\file');
// → 'path/to/file/'For the lazy ones, snormalize (or sn) is already pre-configured with /fc/ (Force forward + Collapse):
import { sn } from 'unslash';
sn('path\\to//file');
// → 'path/to/file'
sn(/t/, 'path\\to//file');
// → 'path/to/file/'Use these in your regex literal (e.g., /tfc/):
t/!t— Add/remove Trailing slashl/!l— Add/remove Leading slashf/!f— Force Forward slashes / backward slashesc/!c— Collapse multiple slashes / Don't collapse
Yeah, I know.
I don’t give a shit.
I was too lazy to look for them and made my own.
Created with GPT help. Cry about it.
It’d be cool if this package got popular and made me rich.