Created
June 13, 2023 18:48
-
-
Save mka142/64a5bf17b6541763c4f19a189a569f43 to your computer and use it in GitHub Desktop.
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 PropTypes from "prop-types"; | |
/** | |
* Find relative path from to to pathname | |
* @param {String} to | |
* @param {String} pathname | |
* @returns {String} rlative path | |
*/ | |
export const relative = (to, pathname) => { | |
let to_list = to.split("/"); | |
let to_obj = Object.fromEntries(to.split("/").entries()); | |
let pathname_list = Object.fromEntries(pathname.split("/").entries()); | |
let key; | |
for (key = 0; key <= to_list.length; key++) { | |
const match = to_obj[key] == pathname_list[key]; | |
if (!match) { | |
break; | |
} | |
} | |
return to_list.slice(key, to_list.length).join("/"); | |
}; | |
relative.propTypes = { | |
to: PropTypes.string.isRequired, | |
pathname: PropTypes.string.isRequired, | |
}; | |
export default { relative }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment