Created
April 25, 2022 08:24
-
-
Save Otto-Vector/0494d542543adde77b8930d18d209208 to your computer and use it in GitHub Desktop.
queryString from Object
This file contains hidden or 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
// нормализуем query Запрос для работы из объекта | |
export const qsNormalize = (params: {}, {nullIsRequired = true, decodeisRequired = true}): string => { | |
let urlObj = new URLSearchParams(Object.fromEntries( Object | |
.entries( params ) | |
// // чистим пустые значения | |
.filter( n => n[1] !== '' ) | |
.filter( n => n[1] !== undefined ) | |
.map(([a,b])=> [a, (b===null && nullIsRequired) ? 'null': b]) //переводим null в строку | |
.filter( n => n[1] !== null ) | |
)).toString() | |
return decodeisRequired ? decodeURIComponent(urlObj) : urlObj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment