UNPKG

1.42 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7
8// This weirdness, combined with the _configure function below, exists because
9// we don't want a public module to import 'batfish-internal/context' directly.
10// That will make any files that use it incapable of executing outside of
11// Batfish (e.g. they won't work in unit tests).
12var siteBasePath = '';
13var siteOrigin = void 0;
14
15// Crude heuristic but probably ok.
16function isAbsoluteUrl(url) {
17 return (/^https?:/.test(url)
18 );
19}
20
21function prefixUrl(url) {
22 if (isAbsoluteUrl(url)) {
23 return url;
24 }
25 if (siteBasePath && url.indexOf(siteBasePath) === 0) {
26 return url;
27 }
28 if (!/^\//.test(url)) url = '/' + url;
29 return siteBasePath + url;
30}
31
32function prefixUrlAbsolute(url) {
33 if (isAbsoluteUrl(url)) {
34 return url;
35 }
36 if (!siteOrigin) {
37 if (process.env.NODE_ENV !== 'production') {
38 throw new Error('The siteOrigin property is not specified in your Batfish configuration. Unable to prefix with absolute path.');
39 }
40 return url;
41 }
42 if (!/^\//.test(url)) url = '/' + url;
43 return siteOrigin + siteBasePath + url;
44}
45
46function isUrlPrefixed(url) {
47 return url.indexOf(siteBasePath) === 0;
48}
49
50prefixUrl._configure = function (a, b) {
51 siteBasePath = a || '';
52 siteOrigin = b;
53};
54
55exports.prefixUrl = prefixUrl;
56exports.prefixUrlAbsolute = prefixUrlAbsolute;
57exports.isUrlPrefixed = isUrlPrefixed;
\No newline at end of file