78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
exports.__esModule = true;
|
|
exports.locationsAreEqual = exports.createLocation = undefined;
|
|
|
|
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
|
|
var _resolvePathname = require('resolve-pathname');
|
|
|
|
var _resolvePathname2 = _interopRequireDefault(_resolvePathname);
|
|
|
|
var _valueEqual = require('value-equal');
|
|
|
|
var _valueEqual2 = _interopRequireDefault(_valueEqual);
|
|
|
|
var _PathUtils = require('./PathUtils');
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
var createLocation = exports.createLocation = function createLocation(path, state, key, currentLocation) {
|
|
var location = void 0;
|
|
if (typeof path === 'string') {
|
|
// Two-arg form: push(path, state)
|
|
location = (0, _PathUtils.parsePath)(path);
|
|
location.state = state;
|
|
} else {
|
|
// One-arg form: push(location)
|
|
location = _extends({}, path);
|
|
|
|
if (location.pathname === undefined) location.pathname = '';
|
|
|
|
if (location.search) {
|
|
if (location.search.charAt(0) !== '?') location.search = '?' + location.search;
|
|
} else {
|
|
location.search = '';
|
|
}
|
|
|
|
if (location.hash) {
|
|
if (location.hash.charAt(0) !== '#') location.hash = '#' + location.hash;
|
|
} else {
|
|
location.hash = '';
|
|
}
|
|
|
|
if (state !== undefined && location.state === undefined) location.state = state;
|
|
}
|
|
|
|
try {
|
|
location.pathname = decodeURI(location.pathname);
|
|
} catch (e) {
|
|
if (e instanceof URIError) {
|
|
throw new URIError('Pathname "' + location.pathname + '" could not be decoded. ' + 'This is likely caused by an invalid percent-encoding.');
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
if (key) location.key = key;
|
|
|
|
if (currentLocation) {
|
|
// Resolve incomplete/relative pathname relative to current location.
|
|
if (!location.pathname) {
|
|
location.pathname = currentLocation.pathname;
|
|
} else if (location.pathname.charAt(0) !== '/') {
|
|
location.pathname = (0, _resolvePathname2.default)(location.pathname, currentLocation.pathname);
|
|
}
|
|
} else {
|
|
// When there is no prior location and pathname is empty, set it to /
|
|
if (!location.pathname) {
|
|
location.pathname = '/';
|
|
}
|
|
}
|
|
|
|
return location;
|
|
};
|
|
|
|
var locationsAreEqual = exports.locationsAreEqual = function locationsAreEqual(a, b) {
|
|
return a.pathname === b.pathname && a.search === b.search && a.hash === b.hash && a.key === b.key && (0, _valueEqual2.default)(a.state, b.state);
|
|
}; |