Last active
January 24, 2018 03:07
-
-
Save wolverineks/9f3eaf82446ec64f1b325ec5c9b8184a to your computer and use it in GitHub Desktop.
Using Array.prototype.map
This file contains 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 URI = require('uri-js') | |
const uri = 'custom://recovery?token=IamToken&nextVar=nextVar' | |
const doTheSideEffect = (token) => console.log(token) | |
doTheWholeEnchilada (uri) { | |
if (!uri) return | |
const parsedUri = URI.parse(uri) | |
const query = parsedUri.query | |
const splitArray = query.split('token=') | |
const nextString = splitArray[1] | |
const finalArray = nextString.split('&') | |
const final = finalArray[0] | |
doTheSideEffect(final) | |
} | |
doTheWholeEnchilada(uri) | |
// what happens if uri = 'custom://recovery?nextVar=nextVar' |
This file contains 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 URI = require('uri-js') | |
const {compose, curry, identity, isNil, not, nth, prop} = require('ramda') | |
const uri = 'custom://recovery?token=IamToken&nextVar=nextVar' | |
const doTheSideEffect = (token) => { console.log(token); return token } | |
const exists = compose(not, isNil) | |
const parse = URI.parse | |
const getQuery = prop('query') | |
const match = curry((regex, source) => source.match(regex)) | |
const token = /token=(\w{8})/ | |
const getMatch = match(token) | |
const getToken = nth(1) | |
const doThePureThing = (uri) => { | |
return [uri] | |
.filter(exists) | |
.map(parse) | |
.map(getQuery) | |
.filter(exists) | |
.map(getMatch) | |
.filter(exists) | |
.map(getToken) | |
} | |
doThePureThing(uri).map(doTheSideEffect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment