-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (28 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export const compose = (...fns) => param =>
fns.reduceRight((acc, curr) => curr(acc), param)
export const queryExtractor = (url) =>
url.split('?').pop()
// make array of query param strings
export const queriesExtractor = (url) =>
url.split('?').pop().split('&')
// transform 1 query string into js object
export const queryTransformer = queryString =>
queryString.split('=').reduce((prev, curr) => ({ [prev]: decodeURI(curr) }))
// parse array of query strings or 1 query string, into normalized js object
export const queryParser = queryString => {
return Array.isArray(queryString)
? queryString.reduce(
(acc, curr) => ({
...acc,
...queryTransformer(curr)
}),
{}
)
: queryTransformer(queryString)
}
export const getQueryParams = compose(queryParser, queriesExtractor)
// getQueryParams() { ...data from window.location.search }
// getQueryParams('/person?name=michael%20spohn') {name: 'michael spohn'}
// queryParser('name=michael%20spohn') {name: 'michael spohn'}
// queryExtractor() 'name=michael&age=26'
// queriesExtractor() ['name=michael', 'age=26']