UNPKG

1.18 kBJavaScriptView Raw
1var $, _, path;
2
3$ = {};
4
5$.type = require('../dist/type');
6
7$.root = require('../dist/root');
8
9$.home = require('../dist/home');
10
11_ = {};
12
13_.trimEnd = require('lodash/trimEnd');
14
15path = require('path');
16
17module.exports = function(string) {
18 var isIgnore;
19 if ('string' !== $.type(string)) {
20 return null;
21 }
22 // check isIgnore
23 if (string[0] === '!') {
24 isIgnore = true;
25 string = string.slice(1);
26 }
27 // replace . & ~
28 string = string.replace(/\.{2}/g, '__parent_directory__');
29 string = (function() {
30 switch (string[0]) {
31 case '.':
32 return string.replace(/\./, $.root());
33 case '~':
34 return string.replace(/~/, $.home());
35 default:
36 return string;
37 }
38 })();
39 string = string.replace(/__parent_directory__/g, '..');
40 // replace ../ to ./../ at start
41 if (string[0] === '.' && string[1] === '.') {
42 string = `${$.root()}/${string}`;
43 }
44 // normalize
45 string = path.normalize(string).replace(/\\/g, '/');
46 // absolute
47 if (!path.isAbsolute(string)) {
48 string = `${$.root()}/${string}`;
49 }
50 // ignore
51 if (isIgnore) {
52 string = `!${string}`;
53 }
54 // return
55 return _.trimEnd(string, '/');
56};