react-router-relative-link-5
Version:
A wrapper around react-router's Link that allows relative paths. For react-router version 5
142 lines (117 loc) • 5.16 kB
JavaScript
import React from 'react';
import { withRouter, Link, NavLink } from 'react-router-dom';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
function isAbsolute(pathname) {
return pathname.charAt(0) === '/';
}
// About 1.5x faster than the two-arg version of Array#splice()
function spliceOne(list, index) {
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) {
list[i] = list[k];
}
list.pop();
}
// This implementation is based heavily on node's url.parse
function resolvePathname(to) {
var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var toParts = to && to.split('/') || [];
var fromParts = from && from.split('/') || [];
var isToAbs = to && isAbsolute(to);
var isFromAbs = from && isAbsolute(from);
var mustEndAbs = isToAbs || isFromAbs;
if (to && isAbsolute(to)) {
// to is absolute
fromParts = toParts;
} else if (toParts.length) {
// to is relative, drop the filename
fromParts.pop();
fromParts = fromParts.concat(toParts);
}
if (!fromParts.length) return '/';
var hasTrailingSlash = void 0;
if (fromParts.length) {
var last = fromParts[fromParts.length - 1];
hasTrailingSlash = last === '.' || last === '..' || last === '';
} else {
hasTrailingSlash = false;
}
var up = 0;
for (var i = fromParts.length; i >= 0; i--) {
var part = fromParts[i];
if (part === '.') {
spliceOne(fromParts, i);
} else if (part === '..') {
spliceOne(fromParts, i);
up++;
} else if (up) {
spliceOne(fromParts, i);
up--;
}
}
if (!mustEndAbs) for (; up--; up) {
fromParts.unshift('..');
}if (mustEndAbs && fromParts[0] !== '' && (!fromParts[0] || !isAbsolute(fromParts[0]))) fromParts.unshift('');
var result = fromParts.join('/');
if (hasTrailingSlash && result.substr(-1) !== '/') result += '/';
return result;
}
var ensureLeadingAndTrailingSlashes = function (path) {
return path.replace(/^\/?/, '/').replace(/\/?$/, '/');
};
var removeTrailingSlash = function (path) {
return path.replace(/\/$/, '') || '/';
};
var removeQuery = function (path) {
return path.split('?')[0];
};
var resolvePathnameNoTrailingSlash = function (path, currentPath) {
return removeTrailingSlash(resolvePathname("" + path, currentPath));
};
var extractCurrentPath = function (currentPath) { return (ensureLeadingAndTrailingSlashes(removeQuery(currentPath))); };
var CoreLink = function (_a) {
var BaseComponent = _a.BaseComponent, match = _a.match, relativeTo = _a.to, staticContext = _a.staticContext, others = __rest(_a, ["BaseComponent", "match", "to", "staticContext"]);
var currentPath = extractCurrentPath(match.url);
var to = typeof relativeTo == 'object'
? __assign(__assign({}, relativeTo), { pathname: resolvePathnameNoTrailingSlash(relativeTo.pathname, currentPath) })
// ??? relativto can be a function!!!!
: resolvePathnameNoTrailingSlash(relativeTo, currentPath);
return React.createElement(BaseComponent, __assign({ to: to }, others));
};
var RelativeLink = function (props) { return (React.createElement(CoreLink, __assign({ BaseComponent: Link }, props))); };
var RelativeLink$1 = withRouter(RelativeLink);
var RelativeNavLink = function (props) { return (React.createElement(CoreLink, __assign({ BaseComponent: NavLink }, props))); };
var RelativeNavLink$1 = withRouter(RelativeNavLink);
export { RelativeLink$1 as Link, RelativeNavLink$1 as NavLink };
//# sourceMappingURL=index.es.js.map