Skip to content

Instantly share code, notes, and snippets.

@andrewsantarin
Last active July 15, 2018 15:51
Show Gist options
  • Save andrewsantarin/c865086ed214817cd4be85d78fc00f42 to your computer and use it in GitHub Desktop.
Save andrewsantarin/c865086ed214817cd4be85d78fc00f42 to your computer and use it in GitHub Desktop.
App like navigation?
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