-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encapsulate query strings #3687
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
80f0b9b
export object from query-string
3b2053e
add a couple of tests to query-string.js
6cf8620
put tests inside a describe block
f1f1d8d
extract prefix and option constants
c18743e
add queryString.toString()
2b46bd8
add date ranges tests
c41788d
move Parameter.toUrlParam() and Parameters.toUrlParam() to use the qu…
48ec52d
use qs
c99264f
Merge branch 'master' into encapsulate-query-strings
442467c
bucket all query params under
d556cb4
set exported default name to match module name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
import qs from 'qs'; | ||
|
||
const parse = queryString => qs.parse(queryString, { allowDots: true, ignoreQueryPrefix: true }); | ||
const onlyParameters = ([k]) => k.startsWith('p_'); | ||
const removePrefix = ([k, v]) => [k.slice(2), v]; | ||
const prefix = 'p_'; | ||
const options = { allowDots: true, ignoreQueryPrefix: true }; | ||
|
||
const parse = queryString => qs.parse(queryString, options); | ||
const onlyParameters = ([k]) => k.startsWith(prefix); | ||
const removePrefix = ([k, v]) => [k.slice(prefix.length), v]; | ||
const addPrefix = ([k, v]) => [`${prefix}${k}`, v]; | ||
const toObject = (obj, [k, v]) => Object.assign(obj, { [k]: v }); | ||
|
||
export default () => Object.entries(parse(location.search)) | ||
const fromString = queryString => Object.entries(parse(queryString)) | ||
.filter(onlyParameters) | ||
.map(removePrefix) | ||
.reduce(toObject, {}); | ||
|
||
const fromUrl = () => fromString(location.search); | ||
|
||
const toString = obj => qs.stringify( | ||
Object.entries(obj) | ||
.map(addPrefix) | ||
.reduce(toObject, {}), options, | ||
); | ||
|
||
export default { | ||
fromString, | ||
fromUrl, | ||
toString, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import queryString from '@/services/query-string'; | ||
|
||
describe('fromString', () => { | ||
it('removes prefixes', () => { | ||
expect(queryString.fromString('p_a=b')) | ||
.toHaveProperty('a'); | ||
}); | ||
|
||
it('ignores non-parameters', () => { | ||
expect(queryString.fromString('p_a=b&utm=123')) | ||
.not.toHaveProperty('utm'); | ||
}); | ||
|
||
it('handles date ranges', () => { | ||
expect(queryString.fromString('p_created_at.start=2019-01-01&p_created_at.end=2019-02-01')).toEqual({ | ||
created_at: { | ||
start: '2019-01-01', | ||
end: '2019-02-01', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('toString', () => { | ||
it('adds prefixes', () => { | ||
expect(queryString.toString({ | ||
a: 'b', | ||
})).toEqual('p_a=b'); | ||
}); | ||
|
||
it('handles date ranges', () => { | ||
expect(queryString.toString({ | ||
created_at: { | ||
start: '2019-01-01', | ||
end: '2019-02-01', | ||
}, | ||
})).toEqual('p_created_at.start=2019-01-01&p_created_at.end=2019-02-01'); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to use
param.toUrlParams()
instead of mappingname => value
directly - it provides some layer of abstraction for a case when param will need some different serialization logic in future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm trying to achieve with this refactoring is moving out the serialization logic from
Parameter
/Parameters
and into the query string module. The approach here is that a query string is itself the serialization "view", thus the logic resides there and the model simply reveals its data.