tildify
Convert an absolute path to a tilde path:
/Users/sindresorhus/dev
โ~/dev
Install
$ npm install tildify
Usage
import tildify from 'tildify';
tildify('/Users/sindresorhus/dev');
//=> '~/dev'
Related
See untildify for the inverse.
Convert an absolute path to tilde path: /Users/sindresorhus/dev => ~/dev
License: MIT License
Convert an absolute path to a tilde path:
/Users/sindresorhus/dev
โ~/dev
$ npm install tildify
import tildify from 'tildify';
tildify('/Users/sindresorhus/dev');
//=> '~/dev'
See untildify for the inverse.
Hey @sindresorhus
How about this? also would work in untildify
module.exports = function (str) {
return str.replace(home, '~');
};
=>
module.exports = function (str) {
str += path.sep;
str = str.replace(home + path.sep, '~' + path.sep);
str = str.slice(0, -1);
return str;
};
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) {
var fixture = path.resolve(home + 'foo', 'tildify');
t.assert(tildify(fixture) === fixture);
t.end();
});
Say my home dir is /Users/callum
and I've got a folder called /Some/project/Users/callum/foo
tildify changes this to /Some/project/~/foo
which is probably not what you want
Paths returned by VS Code APIs may have lowercase drive letters on Windows. As StartsWith
is case sensitive the logic fails. It's such short code I just rewrote it in Typescript and fixed my local copy.
if (normalizedPath[1] === ":") {
//force first char to uppercase
normalizedPath = normalizedPath.replace(normalizedPath[0], normalizedPath[0].toUpperCase());
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.