Comparing version 1.0.1 to 1.1.0
@@ -12,6 +12,5 @@ # {%= name %} {%= badge("fury") %} | ||
## Install | ||
{%= include("install-npm", {save: 'true'}) %} | ||
## Usage | ||
@@ -29,3 +28,3 @@ | ||
## API | ||
{%= comments("index.js") %} | ||
{%= apidocs("index.js") %} | ||
@@ -32,0 +31,0 @@ ## Author |
72
index.js
@@ -1,13 +0,12 @@ | ||
/** | ||
* relative <https://github.com/jonschlinkert/relative> | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Licensed under the MIT license. | ||
*/ | ||
'use strict'; | ||
var path = require('path'); | ||
var isDir = require('is-directory'); | ||
/** | ||
* Expose `relative` | ||
*/ | ||
module.exports = relative; | ||
/** | ||
* Return the relative path from `a` to `b`. | ||
@@ -19,4 +18,4 @@ * | ||
* var relative = require('relative'); | ||
* relative('test/fixtures/foo.txt', 'docs/new/file.txt'); | ||
* //=> '../../docs/new/file.txt' | ||
* relative('a/b/foo.txt', 'c/d/file.txt'); | ||
* //=> '../../c/d/file.txt' | ||
* ``` | ||
@@ -30,19 +29,17 @@ * | ||
var relative = module.exports = function relative(from, to) { | ||
function relative(a, b) { | ||
if (arguments.length === 1) { | ||
to = from; | ||
from = process.cwd(); | ||
b = a; a = process.cwd(); | ||
} | ||
if (!isDir(from)) { | ||
from = path.dirname(from); | ||
a = normalize(path.resolve(a)); | ||
b = normalize(path.resolve(b)); | ||
if (/\./.test(path.basename(a))) { | ||
a = path.dirname(a); | ||
} | ||
from = path.resolve(from); | ||
to = path.resolve(to); | ||
return path.relative(a, b); | ||
} | ||
return path.relative(from, to); | ||
}; | ||
/** | ||
@@ -54,8 +51,8 @@ * Get the path relative to the given base path. | ||
* ```js | ||
* relative.toBase('test/fixtures', 'test/fixtures/docs/new/file.txt'); | ||
* //=> 'docs/new/file.txt' | ||
* relative.toBase('a/b', 'a/b/c/d/file.txt'); | ||
* //=> 'c/d/file.txt' | ||
* ``` | ||
* | ||
* @param {String} `basepath` The base directory | ||
* @param {String} `filepath` The full filepath | ||
* @param {String} `base` The base directory | ||
* @param {String} `fp` The full filepath | ||
* @return {String} The relative path | ||
@@ -65,12 +62,25 @@ * @api public | ||
relative.toBase = function toBase(basepath, filepath) { | ||
filepath = path.resolve(filepath); | ||
basepath = path.resolve(basepath); | ||
relative.toBase = function toBase(base, fp) { | ||
base = normalize(path.resolve(base)); | ||
fp = normalize(path.resolve(fp)); | ||
if (filepath.indexOf(basepath) === 0) { | ||
filepath = filepath.replace(basepath, ''); | ||
if (fp.indexOf(base) === 0) { | ||
fp = fp.replace(base, ''); | ||
} | ||
// Remove leading slash if one was created | ||
return filepath.replace(/^[\\\/]*/, ''); | ||
// Remove leading slash, if created | ||
return fp.replace(/^[\\\/]*/, ''); | ||
}; | ||
/** | ||
* Normalize slashes in paths. This is necessary b/c | ||
* paths are not calculated the same by node.js when | ||
* windows paths are used. | ||
* | ||
* @api private | ||
*/ | ||
function normalize(str) { | ||
return str.replace(/[\\\/]+/g, '/'); | ||
} |
{ | ||
"name": "relative", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.", | ||
@@ -16,8 +16,6 @@ "author": { | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/relative/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/relative/blob/master/LICENSE-MIT" | ||
}, | ||
"main": "index.js", | ||
@@ -30,27 +28,18 @@ "scripts": { | ||
}, | ||
"dependencies": { | ||
"is-directory": "^0.2.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "~1.9.0", | ||
"mocha": "~1.17.1", | ||
"normalize-path": "^0.3.0", | ||
"should": "^4.0.4" | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
"keywords": [ | ||
"absolute", | ||
"check", | ||
"calculate", | ||
"file", | ||
"filepath", | ||
"is", | ||
"fs", | ||
"normalize", | ||
"path", | ||
"path.relative", | ||
"path-relative", | ||
"relative", | ||
"resolve", | ||
"slash", | ||
"slashes", | ||
"uri", | ||
"url" | ||
"resolve" | ||
] | ||
} | ||
} |
@@ -12,6 +12,4 @@ # relative [![NPM version](https://badge.fury.io/js/relative.svg)](http://badge.fury.io/js/relative) | ||
## Install with [npm](npmjs.org) | ||
## Install | ||
### Install with [npm](npmjs.org) | ||
```bash | ||
@@ -21,2 +19,3 @@ npm i relative --save | ||
## Usage | ||
@@ -34,3 +33,3 @@ | ||
## API | ||
### [relative](index.js#L30) | ||
### [relative](index.js#L28) | ||
@@ -47,12 +46,12 @@ Return the relative path from `a` to `b`. | ||
var relative = require('relative'); | ||
relative('test/fixtures/foo.txt', 'docs/new/file.txt'); | ||
//=> '../../docs/new/file.txt' | ||
relative('a/b/foo.txt', 'c/d/file.txt'); | ||
//=> '../../c/d/file.txt' | ||
``` | ||
### [toBase](index.js#L63) | ||
### [.toBase](index.js#L59) | ||
Get the path relative to the given base path. | ||
* `basepath` **{String}**: The base directory | ||
* `filepath` **{String}**: The full filepath | ||
* `base` **{String}**: The base directory | ||
* `fp` **{String}**: The full filepath | ||
* `returns` **{String}**: The relative path | ||
@@ -63,4 +62,4 @@ | ||
```js | ||
relative.toBase('test/fixtures', 'test/fixtures/docs/new/file.txt'); | ||
//=> 'docs/new/file.txt' | ||
relative.toBase('a/b', 'a/b/c/d/file.txt'); | ||
//=> 'c/d/file.txt' | ||
``` | ||
@@ -82,2 +81,2 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on November 17, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 20, 2014._ |
19
test.js
/** | ||
* relative <https://github.com/jonschlinkert/relative> | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert | ||
* Licensed under the MIT license. | ||
@@ -10,7 +11,9 @@ */ | ||
var should = require('should'); | ||
var normalize = require('normalize-path'); | ||
var path = require('path'); | ||
var relative = require('./'); | ||
var cwd = process.cwd(); | ||
var relative = require('./'); | ||
function normalize(str) { | ||
return str.replace(/[\\\/]+/g, '/'); | ||
} | ||
@@ -23,11 +26,11 @@ describe('relative', function() { | ||
it('should resolve the relative path from a file to a directory', function() { | ||
normalize(relative('test/fixtures/foo.txt', 'docs')).should.equal('../../docs'); | ||
normalize(relative('test\\fixtures\\foo.txt', 'docs')).should.equal('../../docs'); | ||
}); | ||
it('should resolve the relative path from a directory to a directory', function() { | ||
normalize(relative('test/fixtures', 'docs')).should.equal('../docs'); | ||
normalize(relative('test/fixtures', 'docs')).should.equal('../../docs'); | ||
}); | ||
it('should resolve the relative path from a directory to a file', function() { | ||
normalize(relative('test/fixtures', 'docs/foo.txt')).should.equal('../docs/foo.txt'); | ||
normalize(relative('test/fixtures', 'docs/foo.txt')).should.equal('../../docs/foo.txt'); | ||
}); | ||
@@ -58,3 +61,3 @@ | ||
var actual = relative('test/fixtures', 'docs', true) | ||
normalize(actual).should.equal('../docs'); | ||
normalize(actual).should.equal('../../docs'); | ||
}); | ||
@@ -64,3 +67,3 @@ | ||
var actual = relative('test/fixtures', 'docs/foo.txt', true) | ||
normalize(actual).should.equal('../docs/foo.txt'); | ||
normalize(actual).should.equal('../../docs/foo.txt'); | ||
}); | ||
@@ -67,0 +70,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
2
152
9581
76
- Removedis-directory@^0.2.3
- Removedis-directory@0.2.3(transitive)