All files / src/app/core/utils url-form-params.ts

100% Statements 15/15
100% Branches 14/14
100% Functions 10/10
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40132x           132x 160x   10x 7x         132x         9x   12x 9x       132x 15x     17x   15x 15x   15x      
import { HttpParams } from '@angular/common/http';
 
export interface URLFormParams {
  [facet: string]: string[];
}
 
export function formParamsToString(object: URLFormParams, separator = ','): string {
  return object
    ? Object.entries(object)
        .filter(([, value]) => Array.isArray(value) && value.length)
        .map(([key, val]) => `${key}=${val.map(encodeURIComponent).join(separator)}`)
        .join('&')
    : '';
}
 
export function appendFormParamsToHttpParams(
  object: URLFormParams,
  params: HttpParams = new HttpParams(),
  separator = ','
): HttpParams {
  return object
    ? Object.entries(object)
        .filter(([, value]) => Array.isArray(value) && value.length)
        .reduce((p, [key, val]) => p.set(key, val.join(separator)), params)
    : params;
}
 
export function stringToFormParams(object: string, separator = ','): URLFormParams {
  return object
    ? object
        .split('&')
        .filter(val => val?.includes('='))
        .map(val => {
          const [key, values] = val.split('=');
          return { key: decodeURIComponent(key), value: values.split(separator).map(decodeURIComponent) };
        })
        .reduce((acc, val) => ({ ...acc, [val.key]: val.value }), {})
    : {};
}