-
Notifications
You must be signed in to change notification settings - Fork 0
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
add SWRDataTable filters to query params #19
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -15,8 +15,78 @@ | |
* serializeQuery(nestedParams); | ||
* // Returns 'user[name]=John&user[age]=30' | ||
*/ | ||
|
||
export function serializeQuery(params, prefix) { | ||
const query = serializeParamsToQueryStrings(params, prefix); | ||
return [...[].concat(...query)].join("&"); | ||
} | ||
|
||
/** | ||
* This function takes an object containing parameters and converts it into a query string. | ||
* It follows the Rails pattern for nested query parameters. | ||
* @example | ||
* const params = { | ||
"columnFilters": { | ||
"is_general_thesis": [ | ||
"false" | ||
] | ||
}, | ||
"pageIndex": 0, | ||
"pageSize": 10 | ||
}; | ||
* serializeQueryObject(params); | ||
* // Returns { | ||
"columnFilters[is_general_thesis][]": "false", | ||
"pageIndex": "0", | ||
"pageSize": "10" | ||
* } | ||
*/ | ||
export function serializeQueryObject(params, prefix = undefined) { | ||
const query = serializeParamsToQueryStrings(params, prefix); | ||
return parseQueryStrings(query.filter((item) => item !== "")); | ||
} | ||
|
||
/** | ||
* This function takes a query string and converts it into an object. | ||
* It follows the Rails pattern for nested query parameters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔝 |
||
* @example | ||
* const nestedQueryString = 'user[name]=John&user[age]=30'; | ||
* deserializeQuery(nestedQueryString); | ||
* // Returns { user: { name: 'John', age: 30 } } | ||
*/ | ||
export function deserializeQuery(paramsString) { | ||
const arr = decodeURIComponent(paramsString.replace(/\+/g, "%20")).split("&"); | ||
const result = {}; | ||
|
||
arr.forEach((item) => { | ||
// eslint-disable-next-line prefer-const | ||
let [path, value] = item.split("="); | ||
|
||
// eslint-disable-next-line no-useless-escape | ||
const pathParts = path.split(/[\[\]]/).filter((p) => p); | ||
// eslint-disable-next-line array-callback-return, consistent-return | ||
pathParts.reduce((acc, key, index) => { | ||
if (index === pathParts.length - 1) { | ||
if (key in acc && Array.isArray(acc[key])) { | ||
acc[key].push(value); | ||
} else if (key in acc) { | ||
acc[key] = [acc[key], value]; | ||
} else { | ||
acc[key] = value; | ||
} | ||
} else { | ||
if (!(key in acc)) { | ||
// eslint-disable-next-line no-restricted-globals | ||
acc[key] = isNaN(Number(pathParts[index + 1])) ? {} : []; | ||
} | ||
return acc[key]; | ||
} | ||
}, result); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
function serializeParamsToQueryStrings(params, prefix = undefined) { | ||
const query = Object.keys(params).map((key) => { | ||
const value = params[key]; | ||
|
||
|
@@ -28,5 +98,26 @@ export function serializeQuery(params, prefix) { | |
return `${key}=${encodeURIComponent(value)}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be that the key also needs to be encoded? |
||
}); | ||
|
||
return [...[].concat(...query)].join("&"); | ||
return query; | ||
} | ||
|
||
function parseQueryStrings(queryStrings) { | ||
const result = {}; | ||
queryStrings.forEach((queryString) => { | ||
const firstEqualIndex = queryString.indexOf("="); | ||
const key = queryString.substring(0, firstEqualIndex); | ||
const value = queryString.substring(firstEqualIndex + 1); | ||
|
||
// Handle the case where the key already exists in the result object | ||
if (result[key]) { | ||
// If the key already exists and is not an array, convert it to an array | ||
if (!Array.isArray(result[key])) { | ||
result[key] = [result[key]]; | ||
} | ||
result[key].push(decodeURIComponent(value)); | ||
} else { | ||
result[key] = decodeURIComponent(value); | ||
} | ||
}); | ||
return result; | ||
} |
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.
Great doc 👍