Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Created April 10, 2024 07:28
Show Gist options
  • Select an option

  • Save ivanhoe011/41492b0fdf15f74be0bdf30fc8bf0bd0 to your computer and use it in GitHub Desktop.

Select an option

Save ivanhoe011/41492b0fdf15f74be0bdf30fc8bf0bd0 to your computer and use it in GitHub Desktop.
Get all filters from the url's query (following jsonapi.org filters format)
/**
* Get all filters w/ regexp, because UrlSearchParams sux
*
* @param string url
* @return Object
*/
function extractFilters(url) {
return url.matchAll(
/[&?]filter\[([^\]]+)\]=([^&]+)/g
)
.reduce((acc, match) => ({
...acc,
[match[1]]: match[2]
}),
{}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment