UNPKG

1.3 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const encode_url_1 = __importDefault(require("./encode_url"));
6const cache_1 = __importDefault(require("./cache"));
7const cache = new cache_1.default();
8function 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 // If the last 2 elements of `out` is empty strings, replace them with `index.html`.
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}
30module.exports = relativeUrlHelper;
31//# sourceMappingURL=relative_url.js.map
\No newline at end of file