Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "home", | ||
"version": "0.1.0", | ||
"description": "home", | ||
"version": "0.1.1", | ||
"description": "Gets the home dir or resolves home directories: `home.resolve('~/path/to')`.", | ||
"main": "index.js", | ||
@@ -14,6 +14,10 @@ "scripts": { | ||
"keywords": [ | ||
"home" | ||
"home", | ||
"user-home", | ||
"resolve", | ||
"cross-platform", | ||
"windows" | ||
], | ||
"engines": { | ||
"node": ">=0.11.0" | ||
"node": ">=0.10.0" | ||
}, | ||
@@ -20,0 +24,0 @@ "author": "kaelzhang", |
# home [](http://badge.fury.io/js/home) [](https://travis-ci.org/kaelzhang/node-home) [](https://gemnasium.com/kaelzhang/node-home) | ||
<!-- description --> | ||
A tiny utility to get the home directory, or resolve a path begins with `'~'`, with cross-platform compatibility. | ||
@@ -15,6 +15,63 @@ ## Install | ||
var home = require('home'); | ||
home(); // '/Users/kael' | ||
var some_path = '~/workspace'; | ||
home.resolve(some_path); // '/Users/kael/workspace' | ||
home.resolve(some_path, 'abc'); // '/Users/kael/workspace/abc' | ||
``` | ||
## home() | ||
Returns `path` the home directory specified by operating system. | ||
## home.resolve([from...], to) | ||
Resolves `to` to an absolute path, if `to` begins with `'~'`, it will be cooked before `path.resolve()`d. | ||
```js | ||
home.resolve('~/file'); // 'Users/kael/file' | ||
``` | ||
The usage of `home.resolve` is very similar to [`path.resolve`](http://nodejs.org/api/path.html#path_path_resolve_from_to) | ||
Another way to think of it is as a sequence of cd commands in a shell. | ||
```js | ||
home.resolve('foo/bar', '~/file/', '..', 'a/../subfile'); | ||
// -> '/Users/kael/subfile' | ||
``` | ||
Is equivalent to: | ||
```js | ||
home.resolve('foo/bar', '/Users/kael/file/', '..', 'a/../subfile'); | ||
``` | ||
Is similar to: | ||
```sh | ||
cd foo/bar | ||
cd ~/file/ | ||
cd .. | ||
cd a/../subfile | ||
pwd | ||
``` | ||
## What about `home.relative()`, `home.join()` ? | ||
For now, `home` doesn't support those, which I thought is unnecessary to make this module too complicated. | ||
I'd rather `home.resolve()` the directories | ||
```js | ||
var dir = '~/dir'; | ||
dir = home.resolve(dir); | ||
path.join(dir, './abc'); | ||
``` | ||
## License | ||
MIT | ||
May be freely distributed under the [MIT license](https://raw.githubusercontent.com/kaelzhang/node-home/master/LICENSE-MIT). | ||
Copyright (c) Kael Zhang and [other contributors](https://github.com/kaelzhang/node-home/graphs/contributors). |
169
test/home.js
'use strict'; | ||
var expect = require('chai').expect; | ||
var home = require('../'); | ||
var home = require('../'); | ||
// Node.js test cases from https://github.com/joyent/node/blob/master/test/simple/test-path.js | ||
var isWindows = process.platform === 'win32'; | ||
if (isWindows) { | ||
// windows | ||
var resolveTests = | ||
// arguments result | ||
[ | ||
[ | ||
['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a' | ||
], | ||
[ | ||
['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe' | ||
], | ||
[ | ||
['c:/ignore', 'c:/some/file'], 'c:\\some\\file' | ||
], | ||
[ | ||
['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir' | ||
], | ||
[ | ||
['.'], process.cwd() | ||
], | ||
[ | ||
['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative' | ||
], | ||
[ | ||
['c:/', '//'], 'c:\\' | ||
], | ||
[ | ||
['c:/', '//dir'], 'c:\\dir' | ||
], | ||
[ | ||
['c:/', '//server/share'], '\\\\server\\share\\' | ||
], | ||
[ | ||
['c:/', '//server//share'], '\\\\server\\share\\' | ||
], | ||
[ | ||
['c:/', '///some//dir'], 'c:\\some\\dir' | ||
] | ||
]; | ||
} else { | ||
// Posix | ||
var resolveTests = | ||
// arguments result | ||
[ | ||
[ | ||
['/var/lib', '../', 'file/'], '/var/file' | ||
], | ||
[ | ||
['/var/lib', '/../', 'file/'], '/file' | ||
], | ||
[ | ||
['a/b/c/', '../../..'], process.cwd() | ||
], | ||
[ | ||
['.'], process.cwd() | ||
], | ||
[ | ||
['/some/dir', '.', '/absolute/'], '/absolute' | ||
] | ||
]; | ||
} | ||
// var home = require('path') | ||
var resolve = home.resolve; | ||
describe("home.resolve(), with no '~' path:", function(){ | ||
resolveTests.forEach(function (c) { | ||
var args = c[0]; | ||
var result = c[1]; | ||
it( | ||
'normal: ' + args.map(JSON.stringify).join(', ') | ||
+ ' -> ' + JSON.stringify(result), function(){ | ||
expect(resolve.apply(home, args)).to.equal(result); | ||
}); | ||
}); | ||
}); | ||
if (isWindows) { | ||
var HOME = process.env.USERPROFILE; | ||
// windows | ||
var homeResolveTests = | ||
// arguments result | ||
[ | ||
[ | ||
['~'], HOME | ||
], | ||
[ | ||
['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a' | ||
], | ||
[ | ||
['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe' | ||
], | ||
[ | ||
['c:/ignore', 'c:/some/file'], 'c:\\some\\file' | ||
], | ||
[ | ||
['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir' | ||
], | ||
[ | ||
['.'], process.cwd() | ||
], | ||
[ | ||
['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative' | ||
], | ||
[ | ||
['c:/', '//'], 'c:\\' | ||
], | ||
[ | ||
['c:/', '//dir'], 'c:\\dir' | ||
], | ||
[ | ||
['c:/', '//server/share'], '\\\\server\\share\\' | ||
], | ||
[ | ||
['c:/', '//server//share'], '\\\\server\\share\\' | ||
], | ||
[ | ||
['c:/', '///some//dir'], 'c:\\some\\dir' | ||
] | ||
]; | ||
} else { | ||
var HOME = process.env.HOME; | ||
// Posix | ||
var homeResolveTests = | ||
// arguments result | ||
[ | ||
[ | ||
['~'], HOME | ||
], | ||
[ | ||
['~/var/lib', '../', 'file/'], HOME + '/var/file' | ||
], | ||
[ | ||
['~/var/lib', '/../', 'file/'], '/file' | ||
], | ||
[ | ||
['~/some/dir', '.', '~/absolute/'], HOME + '/absolute' | ||
], | ||
[ | ||
['~/some/dir', '.', '/absolute/'], '/absolute' | ||
] | ||
]; | ||
} | ||
describe("home():", function(){ | ||
it("home(), should return home dir", function(){ | ||
expect(home()).to.equal(HOME); | ||
}); | ||
}); | ||
describe("home.resolve(), with '~' path:", function(){ | ||
homeResolveTests.forEach(function (c, i) { console.log(c, i) | ||
var args = c[0]; | ||
var result = c[1]; | ||
it(args.map(JSON.stringify).join(', ') + ' -> ' + JSON.stringify(result), function(){ | ||
expect(resolve.apply(home, args)).to.equal(result); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
9290
206
77
3