canonical-path
Advanced tools
Comparing version 0.0.2 to 1.0.0
30
index.js
var path = require('path'); | ||
var i; | ||
var slashRegex = /\\/g; | ||
function canonicalPath(filePath) { | ||
if( path.sep === '\\') { | ||
filePath = filePath.replace(/\\/g, '/'); | ||
} | ||
return filePath; | ||
function canonical(p) { | ||
return p.replace(slashRegex, '/'); | ||
} | ||
@@ -13,15 +10,18 @@ | ||
return function() { | ||
return canonicalPath(fn.apply(path, arguments)); | ||
return canonical(fn.apply(path, arguments)); | ||
}; | ||
} | ||
var fns = ['normalize', 'join', 'resolve', 'relative', 'dirname', 'basename', 'extname']; | ||
var props = ['sep', 'delimiter']; | ||
fns.forEach(function(fn) { | ||
exports[fn] = wrapWithCanonical(path[fn]); | ||
// Wrap the functions that return a path | ||
var toChange = ['normalize', 'join', 'resolve', 'relative', 'dirname', 'format']; | ||
toChange.forEach(function(fn) { | ||
module.exports[fn] = wrapWithCanonical(path[fn]); | ||
}); | ||
props.forEach(function(prop) { | ||
exports[prop] = path[prop]; | ||
// and leave the rest alone | ||
var toLeave = ['basename', 'delimiter', 'extname', 'isAbsolute', 'parse', 'sep']; | ||
toLeave.forEach(function(prop) { | ||
module.exports[prop] = path[prop]; | ||
}); | ||
exports.canonical = canonicalPath; | ||
module.exports.original = path; | ||
module.exports.canonical = canonical; |
var path = require('./index'); | ||
const s = path.sep; | ||
describe("canonical-path", function() { | ||
@@ -6,3 +8,3 @@ describe("normalize", function() { | ||
expect(path.normalize('a/c/../b')).toEqual('a/b'); | ||
expect(path.normalize('a\\c\\..\\b')).toEqual('a/b'); | ||
expect(path.normalize(`a${s}c${s}..${s}b`)).toEqual('a/b'); | ||
}); | ||
@@ -14,6 +16,34 @@ }); | ||
expect(path.join('a/b', 'c/d')).toEqual('a/b/c/d'); | ||
expect(path.join('a\\b', 'c\\d')).toEqual('a/b/c/d'); | ||
expect(path.join(`a${s}b`, `c${s}d`)).toEqual('a/b/c/d'); | ||
}); | ||
}); | ||
describe("resolve", function() { | ||
it("should return a resolved path only using forward slashes", function() { | ||
expect(path.resolve('/a/b', 'x/../c')).toEqual(path.resolve('/') + 'a/b/c'); | ||
expect(path.resolve(`a${s}c${s}..${s}b`)).toEqual(path.resolve('') + '/a/b'); | ||
}); | ||
}); | ||
describe("relative", function() { | ||
it("should return a relative path only using forward slashes", function() { | ||
expect(path.relative('a/b', 'a/d/e/f')).toEqual('../d/e/f'); | ||
expect(path.relative(`a${s}b`, `a${s}d${s}e${s}f`)).toEqual('../d/e/f'); | ||
}); | ||
}); | ||
describe("dirname", function() { | ||
it("should return the dirname of a path only using forward slashes", function() { | ||
expect(path.dirname('a/b/c')).toEqual('a/b'); | ||
expect(path.dirname(`a${s}b${s}c`)).toEqual('a/b'); | ||
}); | ||
}); | ||
describe("format", function() { | ||
it("should return a formatted path only using forward slashes", function() { | ||
expect(path.format({dir: 'a/b', base: 'c'})).toEqual('a/b/c'); | ||
expect(path.format({dir: `a${s}b`, base: 'c'})).toEqual('a/b/c'); | ||
}); | ||
}); | ||
describe("canonical", function() { | ||
@@ -20,0 +50,0 @@ it("should return a path with forward slashes", function() { |
{ | ||
"name": "canonical-path", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "paths that always use forward slashes", | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"scripts": { | ||
"test": "jasmine-node index.spec.js" | ||
"test": "jasmine index.spec.js" | ||
}, | ||
@@ -14,3 +15,4 @@ "repository": "https://github.com/petebacondarwin/node-canonical-path", | ||
"slashes", | ||
"OS" | ||
"OS", | ||
"windows" | ||
], | ||
@@ -20,4 +22,4 @@ "author": "Pete Bacon Darwin", | ||
"devDependencies": { | ||
"jasmine-node": "~1.12.0" | ||
"jasmine": "^3.2.0" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # Canonical Path Utility |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12893
8
198
0