safe-join
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Safe Join

a tiny zero dependency library to join strings with slashes in them that isn't too smart about it!

Install

yarn add safe-join # or npm i safe-join

Usage

import { safeJoin } from 'safe-join'
safeJoin('foo', 'bar') // 'foo/bar'
safeJoin('foo/', 'bar') // 'foo/bar'
safeJoin('foo/', '/bar') // 'foo/bar'
safeJoin('foo', '/bar') // 'foo/bar'
safeJoin('http://foo/', '/bar') // 'http://foo/bar'
 
// works on multiple args too
safeJoin('foo', '/bar/', '/baz') // 'foo/bar/baz'
// etc

You might normally use path.join for this in Node but this works in the browser, also it doesnt swallow /'s when you need //.

Uses typescript because why not.

It does NOT handle query strings for you e.g. safeJoin("https://foo", "bar", "&search=baz") so be sure to handle those on your own. or use https://github.com/jfromaniello/url-join

entire source code

so you know exactly what this

export function safeJoin(...args: string[]) {
  return args.reduce(_safeJoin)
}
// thanks to https://twitter.com/swyx/status/1106839872096985088
function _safeJoin(a: string, b: string) {
  return a.replace(/\/$/, '') + '/' + b.replace(/^\//, '')
}

alternatives

https://github.com/jfromaniello/url-join

TSDX Bootstrap

This project was bootstrapped with TSDX.

Readme

Keywords

none

Package Sidebar

Install

npm i safe-join

Weekly Downloads

2,880

Version

0.1.3

License

none

Unpacked Size

6.7 kB

Total Files

18

Last publish

Collaborators

  • sw-yx