Skip to content

Commit 821d40c

Browse files
committed
handle all possible types for To
1 parent 2bf3314 commit 821d40c

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

packages/history/__tests__/hash-root-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ describe('a hash history with no slash', () => {
1818
return Reflect.get(target, prop, _);
1919
}
2020
});
21-
})
21+
})
2222
});

packages/history/__tests__/hash-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,5 @@ export const testHashHistory = (root, createHistory) => {
149149
describe('a hash history', () => {
150150
testHashHistory('#/', () => {
151151
return createHashHistory();
152-
})
152+
})
153153
});

packages/history/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,20 +591,19 @@ export function createHashHistory(
591591

592592
let { hashRoot = '/' } = options;
593593

594-
function parsePathInput(pathname) {
595-
return parsePath(pathname.replace(hashRoot, '/'));
596-
}
597-
598-
function parsePathOutput(pathname) {
599-
return parsePath(pathname.replace('/', hashRoot));
594+
function prefixPathname([base, root]: string[], partial: Partial<Path>) {
595+
const pathname = (partial.pathname || base).replace(base, root);
596+
return { pathname, ...partial };
600597
}
598+
const pathFromGlobal = prefixPathname.bind(null, [hashRoot, '/']);
599+
const pathToGlobal = prefixPathname.bind(null, ['/', hashRoot]);
601600

602601
function getIndexAndLocation(): [number, Location] {
603602
let {
604603
pathname = '/',
605604
search = '',
606605
hash = ''
607-
} = parsePathInput(window.location.hash.substr(1));
606+
} = pathFromGlobal(parsePath(window.location.hash.substr(1)))
608607
let state = globalHistory.state || {};
609608
return [
610609
state.idx,
@@ -708,7 +707,7 @@ export function createHashHistory(
708707
pathname: location.pathname,
709708
hash: '',
710709
search: '',
711-
...(typeof to === 'string' ? parsePathOutput(to) : to),
710+
...(typeof to === 'string' ? parsePath(to) : to),
712711
state,
713712
key: createKey()
714713
});
@@ -724,7 +723,7 @@ export function createHashHistory(
724723
key: nextLocation.key,
725724
idx: index
726725
},
727-
createHref(nextLocation)
726+
createHref(pathToGlobal(nextLocation))
728727
];
729728
}
730729

0 commit comments

Comments
 (0)