Skip to content

Commit

Permalink
handle all possible types for To
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnhoffer committed Dec 21, 2021
1 parent 2bf3314 commit 821d40c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/history/__tests__/hash-root-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ describe('a hash history with no slash', () => {
return Reflect.get(target, prop, _);
}
});
})
})
});
2 changes: 1 addition & 1 deletion packages/history/__tests__/hash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,5 @@ export const testHashHistory = (root, createHistory) => {
describe('a hash history', () => {
testHashHistory('#/', () => {
return createHashHistory();
})
})
});
17 changes: 8 additions & 9 deletions packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,20 +591,19 @@ export function createHashHistory(

let { hashRoot = '/' } = options;

function parsePathInput(pathname) {
return parsePath(pathname.replace(hashRoot, '/'));
}

function parsePathOutput(pathname) {
return parsePath(pathname.replace('/', hashRoot));
function prefixPathname([base, root]: string[], partial: Partial<Path>) {
const pathname = (partial.pathname || base).replace(base, root);
return { pathname, ...partial };
}
const pathFromGlobal = prefixPathname.bind(null, [hashRoot, '/']);
const pathToGlobal = prefixPathname.bind(null, ['/', hashRoot]);

function getIndexAndLocation(): [number, Location] {
let {
pathname = '/',
search = '',
hash = ''
} = parsePathInput(window.location.hash.substr(1));
} = pathFromGlobal(parsePath(window.location.hash.substr(1)))
let state = globalHistory.state || {};
return [
state.idx,
Expand Down Expand Up @@ -708,7 +707,7 @@ export function createHashHistory(
pathname: location.pathname,
hash: '',
search: '',
...(typeof to === 'string' ? parsePathOutput(to) : to),
...(typeof to === 'string' ? parsePath(to) : to),
state,
key: createKey()
});
Expand All @@ -724,7 +723,7 @@ export function createHashHistory(
key: nextLocation.key,
idx: index
},
createHref(nextLocation)
createHref(pathToGlobal(nextLocation))
];
}

Expand Down

0 comments on commit 821d40c

Please sign in to comment.