-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
refactor: replace qs lib with native URLSearchParams #736
Open
skranee
wants to merge
2
commits into
dev
Choose a base branch
from
refactor/replace-qs-with-urlsearchparams
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Flatten a nested object into a flat structure with keys representing the path to each value. | ||
* @param {object} obj - The nested object to flatten. | ||
* @param {string} [parentKey=""] - The base key for the current nesting level (used recursively). | ||
* @param {Record<string, any>} [result={}] - The object to store the flattened result (used recursively). | ||
* @returns {Record<string, any>} - A flat object where keys represent the path to the values in the original object. | ||
* | ||
* @example | ||
* const data = { | ||
* name: "John", | ||
* age: 42, | ||
* children: { | ||
* first: { | ||
* name: "Kelvin", | ||
* age: 12, | ||
* }, | ||
* second: { | ||
* name: "Henry", | ||
* age: 15, | ||
* }, | ||
* }, | ||
* }; | ||
* | ||
* const flatData = flattenData(data); | ||
* console.log(flatData); | ||
* // { | ||
* // "name": "John", | ||
* // "age": 42, | ||
* // "children[first][name]": "Kelvin", | ||
* // "children[first][age]": 12, | ||
* // "children[second][name]": "Henry", | ||
* // "children[second][age]": 15, | ||
* // } | ||
*/ | ||
const flattenData = (obj: Record<string, any>, parentKey: string = "", result: Record<string, any> = {}): Record<string, any> => { | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
const newKey = parentKey ? `${parentKey}[${key}]` : key; | ||
if (typeof obj[key] === "object" && obj[key] !== null && !Array.isArray(obj[key])) { | ||
flattenData(obj[key], newKey, result); | ||
} else { | ||
result[newKey] = obj[key]; | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
|
||
export default flattenData; |
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.
Can you explain why this is needed? Preferably with examples.
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.
Seems overengineered. I'd prefer to use native URLSearchParams (or alternative) functionality, instead of a custom function.
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.
This is needed because native URLSearchParams can not work with nested objects, it can only process flat ones. So if the 'data' object is going to have some depth, the params would look like that


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.
In qs lib it's processed by default up to object's depth = 5, but while using native URLSearchParams you should either be sure that the object is always flat or flatten it on your own to avoid results as are shown on the screenshot.
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.
Also, if I'm not mistaken, a native flat() method is only available for arrays in JS. If we're working with object, we have to flatten the object using your own functions (that's also what I've got while searching the answer on stack overflow and through AI requests).
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 get your point, but does the BTC, DASH or DOGE APIs use nested params? Perhaps we don't this at all.
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.
IDK about that honestly, will do a research later