This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree 3 files changed +62
-0
lines changed
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ export default getQueryStringValue
2
+
3
+ /**
4
+ * Original Source: https://stackoverflow.com/questions/901115/
5
+ *
6
+ * This method will return the query string value
7
+ * of the given parameter name
8
+ *
9
+ * @param {String } name - The parameter name in query string
10
+ * @param {String } url - The url with query string
11
+ * @return {String } - The query string value
12
+ */
13
+
14
+ function getQueryStringValue ( name , url ) {
15
+
16
+ name = name . replace ( / [ \[ \] ] / g, '\\$&' )
17
+ const regex = new RegExp ( `[?&]${ name } (=([^&#]*)|&|#|$)` )
18
+ const results = regex . exec ( url )
19
+ if ( ! results ) {
20
+ return null
21
+ }
22
+
23
+ if ( ! results [ 2 ] ) {
24
+ return ''
25
+ }
26
+
27
+ return decodeURIComponent ( results [ 2 ] . replace ( / \+ / g, ' ' ) )
28
+ }
Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ import isPrime from './is-prime'
56
56
import swapElements from './swapElements'
57
57
import reverse from './reverse'
58
58
import removeAccents from './remove-accents'
59
+ import getQueryStringValue from './get-query-string-value'
59
60
60
61
export {
61
62
isOdd ,
@@ -116,4 +117,5 @@ export {
116
117
swapElements ,
117
118
reverse ,
118
119
removeAccents ,
120
+ getQueryStringValue ,
119
121
}
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+ import { getQueryStringValue } from '../src'
3
+
4
+ const url = 'https://www.google.com/search?foo=bar&bar=&apple'
5
+
6
+ test ( 'returns query string value of the given parameter' , t => {
7
+ const name = 'foo'
8
+ const expected = 'bar'
9
+ const actual = getQueryStringValue ( name , url )
10
+ t . deepEqual ( actual , expected )
11
+ } )
12
+
13
+ test ( 'returns an empty string if parameter is present with empty value' , t => {
14
+ const name = 'bar'
15
+ const expected = ''
16
+ const actual = getQueryStringValue ( name , url )
17
+ t . deepEqual ( actual , expected )
18
+ } )
19
+
20
+ test ( 'returns an empty string if parameter is present with no value' , t => {
21
+ const name = 'apple'
22
+ const expected = ''
23
+ const actual = getQueryStringValue ( name , url )
24
+ t . deepEqual ( actual , expected )
25
+ } )
26
+
27
+ test ( 'returns null if parameter does not exist' , t => {
28
+ const name = 'pizza'
29
+ const expected = null
30
+ const actual = getQueryStringValue ( name , url )
31
+ t . deepEqual ( actual , expected )
32
+ } )
You can’t perform that action at this time.
0 commit comments