Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

safe-join

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-join

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

  • 0.1.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-4.87%
Maintainers
1
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 16 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc