1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const encode_url_1 = __importDefault(require("./encode_url"));
|
6 | const cache_1 = __importDefault(require("./cache"));
|
7 | const cache = new cache_1.default();
|
8 | function relativeUrlHelper(from = '', to = '') {
|
9 | return cache.apply(`${from}-${to}`, () => {
|
10 | const fromParts = from.split('/');
|
11 | const toParts = to.split('/');
|
12 | const length = Math.min(fromParts.length, toParts.length);
|
13 | let i = 0;
|
14 | for (; i < length; i++) {
|
15 | if (fromParts[i] !== toParts[i])
|
16 | break;
|
17 | }
|
18 | let out = toParts.slice(i);
|
19 | for (let j = fromParts.length - i - 1; j > 0; j--) {
|
20 | out.unshift('..');
|
21 | }
|
22 | const outLength = out.length;
|
23 |
|
24 | if (outLength > 1 && !out[outLength - 1] && !out[outLength - 2]) {
|
25 | out = out.slice(0, outLength - 2).concat('index.html');
|
26 | }
|
27 | return (0, encode_url_1.default)(out.join('/').replace(/\/{2,}/g, '/'));
|
28 | });
|
29 | }
|
30 | module.exports = relativeUrlHelper;
|
31 |
|
\ | No newline at end of file |