Skip to content

Commit

Permalink
Extracting shared logic into reusable pathToLocation function and upd…
Browse files Browse the repository at this point in the history
…ating module config
  • Loading branch information
santino committed Jul 19, 2020
1 parent 1bd835d commit 8c1a8b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"main": "cjs/index.js",
"module": "esm/index.js",
"unpkg": "umd/react-concurrent-router.production.min.js",
"sideEffects": false,
"scripts": {
"build": "rimraf dist && npm run build:cjs&& npm run build:esm && npm run build:umd && npm run build:pick",
"build:cjs": "cross-env BABEL_ENV=cjs rollup -c",
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const bundles = {
dir: 'dist/cjs',
entryFileNames: '[name].js',
format: 'cjs',
exports: 'default',
esModule: false
},
external: configBase.external,
Expand Down
19 changes: 11 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const sortAndStringifyRequestParams = params => {

// for ... in loop is faster than Object.keys + map
for (const param in params) {
/* istanbul ignore next */
// istanbul ignore next
if (!Object.prototype.hasOwnProperty.call(params, param)) continue

optimisedParamsArray.push({
Expand Down Expand Up @@ -134,13 +134,19 @@ export const routesToMap = routes => {
return routesMap
}

/**
* Transforms input to location object when string; returns non-parsed input otherwise.
*/
const pathToLocation = location =>
typeof location === 'string' ? parsePath(location) : location

/**
* Check match between two different locations. Check for equal pathname by default.
* Will also compare search and hash properties for exact match.
*/
export const locationsMatch = (left, right, exact = false) => {
const leftLocation = typeof left === 'string' ? parsePath(left) : left
const rightLocation = typeof right === 'string' ? parsePath(right) : right
const leftLocation = pathToLocation(left)
const rightLocation = pathToLocation(right)

// pathname equality is always required, even for non-exact matches
if (leftLocation.pathname !== rightLocation.pathname) return false
Expand Down Expand Up @@ -191,10 +197,7 @@ export const matchRegexRoute = (referencePath, pathname) => {
* Will return matched route; wildrcard route (404) if no matches or ultimately null if no wildcard set.
*/
export const matchRoutes = (routes, requestedMatch) => {
const locationToMatch =
typeof requestedMatch === 'string'
? parsePath(requestedMatch)
: requestedMatch
const locationToMatch = pathToLocation(requestedMatch)
const { pathname } = locationToMatch
const params = { ...paramsStringToObject(locationToMatch.search) }

Expand Down Expand Up @@ -256,7 +259,7 @@ const prepareAssistPrefetchMatch = (

// for ... in loop is faster than Object.keys + map
for (const property in prefetch) {
/* istanbul ignore next */
// istanbul ignore next
if (!Object.prototype.hasOwnProperty.call(prefetch, property)) {
continue
}
Expand Down

0 comments on commit 8c1a8b9

Please sign in to comment.