1 | var $, _, 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 |
|
15 | path = require('path');
|
16 |
|
17 | module.exports = function(string) {
|
18 | var isIgnore;
|
19 | if ('string' !== $.type(string)) {
|
20 | return null;
|
21 | }
|
22 |
|
23 | if (string[0] === '!') {
|
24 | isIgnore = true;
|
25 | string = string.slice(1);
|
26 | }
|
27 |
|
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 |
|
41 | if (string[0] === '.' && string[1] === '.') {
|
42 | string = `${$.root()}/${string}`;
|
43 | }
|
44 |
|
45 | string = path.normalize(string).replace(/\\/g, '/');
|
46 |
|
47 | if (!path.isAbsolute(string)) {
|
48 | string = `${$.root()}/${string}`;
|
49 | }
|
50 |
|
51 | if (isIgnore) {
|
52 | string = `!${string}`;
|
53 | }
|
54 |
|
55 | return _.trimEnd(string, '/');
|
56 | };
|