Created
September 3, 2019 03:15
-
-
Save cvan/c95a52edc2a35b84a987249ec2a60014 to your computer and use it in GitHub Desktop.
get root domain from URL (JavaScript) for setting `Domain` in `Cookie` header
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
const getOrigin = str => str.replace(/.*\/\//, '').replace(/:.+/, '').replace(/\/.+/, ''); | |
const getOriginRoot = (str) => { | |
const originFull = getOrigin(str); | |
if (!originFull.includes('.')) { | |
return str; | |
} | |
const originFullChunks = originFull.split('.'); | |
const originRoot = originFullChunks.slice(originFullChunks.length - 2, originFullChunks.length).join('.'); | |
return originRoot; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment