Last active
July 15, 2018 15:51
-
-
Save andrewsantarin/c865086ed214817cd4be85d78fc00f42 to your computer and use it in GitHub Desktop.
App like navigation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import isEqual from 'lodash/isEqual'; | |
function extractPathSegments(pathname) { | |
return pathname | |
.split('/') | |
.filter(str => str) | |
.map(str => `/${str}`); | |
} | |
function lastLocationIsParent(lastLocationPathname, currentLocationPathname) { | |
const lastLocation = { | |
pathname: '/sign-in/tel', | |
}; | |
const currentLocation = { | |
pathname: '/sign-in/tel/verify', | |
}; | |
const lastLocationSegments = extractPathSegments(lastLocation.pathname); | |
const currentLocationSegments = extractPathSegments(currentLocation.pathname); | |
const currentLocationsParentSegments = currentLocationSegments.slice(0, -1); | |
const isLastLocationParent = isEqual( | |
lastLocationSegments, | |
currentLocationsParentSegments | |
); | |
return isLastLocationParent; | |
} | |
function lastLocationIsSibling(lastLocationPathname, currentLocationPathname) { | |
const isLastLocationParent = lastLocationIsParent( | |
lastLocationPathname, | |
currentLocationPathname | |
); | |
const lastLocationSegments = extractPathSegments(lastLocation.pathname); | |
const currentLocationSegments = extractPathSegments(currentLocation.pathname); | |
const areSegmentsEqualInLength = lastLocationSegments.length === currentLocationSegments.length; | |
const isLastLocationSibling = isLastLocationParent && areSegmentsEqualInLength; | |
return isLastLocationSibling; | |
} | |
const lastLocation = { | |
pathname: '/sign-in/tel', | |
}; | |
const currentLocation = { | |
pathname: '/sign-in/tel/verify', | |
}; | |
const isLastLocationParent = lastLocationIsParent( | |
lastLocation.pathname, | |
currentLocation.pathname | |
); | |
const isLastLocationSibling = lastLocationIsSibling( | |
lastLocation.pathname, | |
currentLocation.pathname | |
); | |
console.log(isLastLocationParent); | |
console.log(isLastLocationSibling); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment