Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict replace, fix #3 #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';
var path = require('path');
var osHomedir = require('os-homedir');
var home = osHomedir();

module.exports = function (str) {
return str.replace(home, '~');
str += path.sep;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use path.resolve(str) on the str, otherwise you'll end up with double // if the input string ends in a /.

str = str.replace(home + path.sep, '~' + path.sep);
str = str.slice(0, -1);
return str;
};
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ var path = require('path');
var osHomedir = require('os-homedir');
var home = osHomedir();

test(function (t) {
var fixture = home;
t.assert(tildify(fixture) === '~');
t.end();
});

test(function (t) {
var fixture = path.resolve(home, 'tildify');
t.assert(tildify(fixture)[0] === '~');
t.assert(/tildify$/.test(tildify(fixture)));
t.assert(tildify(fixture) !== fixture);
t.end();
});

test(function (t) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give all the tests a descriptive title?

test('title', function () {});

var fixture = path.resolve(home + 'foo', 'tildify');
t.assert(tildify(fixture) === fixture);
t.end();
});