Skip to content

Instantly share code, notes, and snippets.

@Otto-Vector
Created April 25, 2022 08:24
Show Gist options
  • Save Otto-Vector/0494d542543adde77b8930d18d209208 to your computer and use it in GitHub Desktop.
Save Otto-Vector/0494d542543adde77b8930d18d209208 to your computer and use it in GitHub Desktop.
queryString from Object
// нормализуем 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