# safe-join

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

Latest version **0.1.3** (published 2019-03-16) · 0 weekly downloads

## Install

```sh
npm install safe-join
pnpm add safe-join
yarn add safe-join
bun add safe-join
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2019-03-16 |
| First published | 2019-03-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | sw-yx |

## Links

- npm: https://www.npmjs.com/package/safe-join
- npm.io page: https://npm.io/package/safe-join

## Recent versions

- 0.1.3 (latest) — 2019-03-16
- 0.1.2 — 2019-03-16
- 0.1.1 — 2019-03-16
- 0.1.0 — 2019-03-16

## README

# 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

```ts
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

```ts
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](https://github.com/jaredpalmer/tsdx).

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