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.0
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Safe Join

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

import join from 'safe-join'
join('foo', 'bar') // 'foo/bar'
join('foo/', 'bar') // 'foo/bar'
join('foo/', '/bar') // 'foo/bar'
join('foo', '/bar') // 'foo/bar'
join('http://foo/', '/bar') // 'http://foo/bar'

// works on multiple args too
join('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. join("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 default function safeJoin(...args: string[]) {
  return args.reduce(_safeJoin)
}

function _safeJoin(a: string, b: string) {
  let isTrailingSlash, isLeadingSlash
  if (a.length > 1 && a.slice(-1)[0] === '/') isTrailingSlash = true
  if (b.length > 1 && b[0] === '/') isLeadingSlash = true
  if (isTrailingSlash && isLeadingSlash) return a + b.slice(1)
  else if (!isTrailingSlash && !isLeadingSlash) return a + '/' + b
  else return a + b
}

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