Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 29, 2019
1 parent 198905a commit 3ac66ab
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -9,6 +9,6 @@ tildify('/Users/sindresorhus/dev');
//=> '~/dev'
```
*/
declare function tildify(string: string): string;
declare function tildify(absolutePath: string): string;

export = tildify;
11 changes: 7 additions & 4 deletions index.js
Expand Up @@ -2,9 +2,12 @@
const path = require('path');
const os = require('os');

const home = os.homedir();
const homeDirectory = os.homedir();

module.exports = string => {
string = path.normalize(string) + path.sep;
return (string.indexOf(home) === 0 ? string.replace(home + path.sep, `~${path.sep}`) : string).slice(0, -1);
module.exports = absolutePath => {
const normalizedPath = path.normalize(absolutePath) + path.sep;

return (normalizedPath.indexOf(homeDirectory) === 0 ?
normalizedPath.replace(homeDirectory + path.sep, `~${path.sep}`) :
normalizedPath).slice(0, -1);
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -27,10 +27,10 @@
"collapse",
"path",
"home",
"dir",
"directory",
"user",
"expand"
"expand",
"convert"
],
"devDependencies": {
"ava": "^1.4.1",
Expand Down
10 changes: 5 additions & 5 deletions test.js
Expand Up @@ -4,22 +4,22 @@ const os = require('os');
const test = require('ava');
const tildify = require('.');

const home = os.homedir();
const homeDirectory = os.homedir();

test('tildify home', t => {
const fixture = home;
const fixture = homeDirectory;
t.is(tildify(fixture), '~');
});

test('tildify path', t => {
const fixture = path.resolve(home, 'tildify');
const fixture = path.resolve(homeDirectory, 'tildify');
t.is(tildify(fixture)[0], '~');
t.true(tildify(fixture).endsWith('tildify'));
t.not(tildify(fixture), fixture);
});

test('ensure only a fully matching path is replaced', t => {
const fixture = path.resolve(`${home}foo`, 'tildify');
const fixture = path.resolve(`${homeDirectory}foo`, 'tildify');
t.is(tildify(fixture), fixture);
});

Expand All @@ -29,6 +29,6 @@ test('ignore relative paths', t => {
});

test('only tildify when home is at the start of a path', t => {
const fixture = path.join('tildify', home);
const fixture = path.join('tildify', homeDirectory);
t.is(tildify(fixture), fixture);
});

0 comments on commit 3ac66ab

Please sign in to comment.