node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



NPM version Build Status: Linux Build Status: Windows Coverage Status

Removes all separators from the end of a string.


npm install remove-trailing-separator


const removeTrailingSeparator = require('remove-trailing-separator');
removeTrailingSeparator('/foo/bar/')   // '/foo/bar' 
removeTrailingSeparator('/foo/bar///') // '/foo/bar' 
// leaves only/last separator 
removeTrailingSeparator('/')    // '/' 
removeTrailingSeparator('///')  // '/' 
// returns empty string 
removeTrailingSeparator('') // '' 

Notable backslash, or win32 separator behavior

\ is considered a separator only on WIN32 systems. All POSIX compliant systems see backslash as a valid file name character, so it would break POSIX compliance to remove it there.

In practice, this means that this code will return different things depending on what system it runs on:

// UNIX  => '\\foo\\' 
// WIN32 => '\\foo'