-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const { sep: sepPlatform } = require('path'); | ||
|
||
const formatPath = (str, sep) => { | ||
const rDedup = new RegExp('\\' + sep + '{2,}', 'g'); | ||
return str.replace(/\/|\\/g, sep).replace(rDedup, sep); | ||
}; | ||
|
||
const joinPath = (...args) => { | ||
const argsSize = args.length; | ||
|
||
// defaults to platform-specific path separator | ||
let sep = sepPlatform; | ||
if (typeof args[argsSize - 1] === 'object') sep = args.pop().sep; | ||
|
||
const result = args.join(sep); | ||
|
||
// Similar behaviour as path.join() | ||
// https://nodejs.org/api/path.html#path_path_join_paths | ||
if (result.length === 0) return '.'; | ||
|
||
return formatPath(result, sep); | ||
}; | ||
|
||
module.exports = joinPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
require('chai').should(); | ||
const { sep } = require('path'); | ||
const join = require('../lib/join_path'); | ||
|
||
describe('joinPath', () => { | ||
it('one path', () => { | ||
const content = 'foo/bar'; | ||
join(content).should.eql('foo' + sep + 'bar'); | ||
}); | ||
|
||
it('no argument', () => { | ||
join().should.eql('.'); | ||
}); | ||
|
||
it('zero-length string', () => { | ||
join('').should.eql('.'); | ||
}); | ||
|
||
it('two paths', () => { | ||
join('foo', 'bar').should.eql('foo' + sep + 'bar'); | ||
}); | ||
|
||
it('one path - custom separator', () => { | ||
const custom = '\\'; | ||
join('foo/bar', { sep: custom }).should.eql('foo' + custom + 'bar'); | ||
}); | ||
|
||
it('two paths - custom separator', () => { | ||
const custom = '\\'; | ||
join('foo', 'bar', { sep: custom }).should.eql('foo' + custom + 'bar'); | ||
}); | ||
|
||
it('deduplicate separators', () => { | ||
join('foo/', '/bar').should.eql('foo' + sep + 'bar'); | ||
}); | ||
|
||
it('starting and ending slashes', () => { | ||
join('/foo/', '/bar/').should.eql(sep + 'foo' + sep + 'bar' + sep); | ||
}); | ||
|
||
it('mixed slashes', () => { | ||
join('foo/', '\\bar').should.eql('foo' + sep + 'bar'); | ||
}); | ||
|
||
it('mixed and >2 slashes', () => { | ||
join('foo////', '\\\\bar').should.eql('foo' + sep + 'bar'); | ||
}); | ||
|
||
it('mixed and >2 slashes - custom separator', () => { | ||
const custom = '\\'; | ||
join('foo////', '\\\\bar', { sep: custom }).should.eql('foo' + custom + 'bar'); | ||
}); | ||
|
||
it('three paths', () => { | ||
join('foo', 'bar', 'baz').should.eql('foo' + sep + 'bar' + sep + 'baz'); | ||
}); | ||
|
||
it('three paths - custom separator', () => { | ||
const custom = '\\'; | ||
join('foo', 'bar', 'baz', { sep: custom }).should.eql('foo' + custom + 'bar' + custom + 'baz'); | ||
}); | ||
}); |