UNPKG

1.77 kBJavaScriptView Raw
1var _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; };
2
3import resolvePathname from 'resolve-pathname';
4import valueEqual from 'value-equal';
5import { parsePath } from './PathUtils';
6
7export var createLocation = function createLocation(path, state, key, currentLocation) {
8 var location = void 0;
9 if (typeof path === 'string') {
10 // Two-arg form: push(path, state)
11 location = parsePath(path);
12 location.state = state;
13 } else {
14 // One-arg form: push(location)
15 location = _extends({}, path);
16
17 if (location.pathname === undefined) location.pathname = '';
18
19 if (location.search) {
20 if (location.search.charAt(0) !== '?') location.search = '?' + location.search;
21 } else {
22 location.search = '';
23 }
24
25 if (location.hash) {
26 if (location.hash.charAt(0) !== '#') location.hash = '#' + location.hash;
27 } else {
28 location.hash = '';
29 }
30
31 if (state !== undefined && location.state === undefined) location.state = state;
32 }
33
34 location.key = key;
35
36 if (currentLocation) {
37 // Resolve incomplete/relative pathname relative to current location.
38 if (!location.pathname) {
39 location.pathname = currentLocation.pathname;
40 } else if (location.pathname.charAt(0) !== '/') {
41 location.pathname = resolvePathname(location.pathname, currentLocation.pathname);
42 }
43 }
44
45 return location;
46};
47
48export var locationsAreEqual = function locationsAreEqual(a, b) {
49 return a.pathname === b.pathname && a.search === b.search && a.hash === b.hash && a.key === b.key && valueEqual(a.state, b.state);
50};
\No newline at end of file