1 |
|
2 | let BASE;
|
3 |
|
4 | export default {
|
5 |
|
6 | get base () {
|
7 | if (!BASE) BASE = window.document.querySelector('base');
|
8 | if (BASE) return BASE.href;
|
9 | return window.location.origin + (window.location.pathname ? window.location.pathname : '/');
|
10 | },
|
11 |
|
12 | async setup (option) {
|
13 | option = option || {};
|
14 |
|
15 | if (option.base) {
|
16 | BASE = window.document.querySelector('base');
|
17 |
|
18 | if (!BASE) {
|
19 | BASE = window.document.createElement('base');
|
20 | window.document.head.insertBefore(BASE, window.document.head.firstElementChild);
|
21 | }
|
22 |
|
23 | BASE.href = option.base;
|
24 | }
|
25 |
|
26 | },
|
27 |
|
28 | extension (data) {
|
29 | const position = data.lastIndexOf('.');
|
30 | return position > 0 ? data.slice(position + 1) : '';
|
31 | },
|
32 |
|
33 | clean (data) {
|
34 | const hash = window.location.hash;
|
35 | const search = window.location.search;
|
36 | const origin = window.location.origin;
|
37 | const protocol = window.location.protocol + '//';
|
38 |
|
39 | if (data.slice(0, origin.length) === origin) {
|
40 | data = data.slice(origin.length);
|
41 | }
|
42 |
|
43 | if (data.slice(0, protocol.length) === protocol) {
|
44 | data = data.slice(protocol.length);
|
45 | }
|
46 |
|
47 | if (data.slice(-hash.length) === hash) {
|
48 | data = data.slice(0, -hash.length);
|
49 | }
|
50 |
|
51 | if (data.slice(-search.length) === search) {
|
52 | data = data.slice(0, -search.length);
|
53 | }
|
54 |
|
55 | return data || '/';
|
56 | },
|
57 |
|
58 | normalize (data) {
|
59 | const parser = window.document.createElement('a');
|
60 |
|
61 | data = this.clean(data);
|
62 | data = data.replace(/\/+/g, '/');
|
63 |
|
64 | parser.href = data;
|
65 |
|
66 | data = parser.pathname;
|
67 | data = data ? data : '/';
|
68 |
|
69 | if (data !== '/' && data.slice(-1) === '/') {
|
70 | data = data.slice(0, -1);
|
71 | }
|
72 |
|
73 | return data;
|
74 | },
|
75 |
|
76 | join () {
|
77 |
|
78 | if (!arguments.length) {
|
79 | throw new Error('Oxe.path.join - argument required');
|
80 | }
|
81 |
|
82 | const result = [];
|
83 |
|
84 | for (let i = 0, l = arguments.length; i < l; i++) {
|
85 | result.push(arguments[i]);
|
86 | }
|
87 |
|
88 | return this.normalize(result.join('/'));
|
89 | }
|
90 |
|
91 | }
|