import { ParsedQuery } from 'query-string' ; import { transform } from 'lodash' ; /**
 * This method is intended for encoding *key* or *value* parts of query component. We need a custom
 * method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
 * encoded per http://tools.ietf.org/html/rfc3986:
 *    query         = *( pchar / "/" / "?" )
 *    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
 *    unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
 *    pct-encoded   = "%" HEXDIG HEXDIG
 *    sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
 *                     / "*" / "+" / "," / ";" / "="
 */ export function encodeUriQuery ( val , pctEncodeSpaces = false ) { return encodeURIComponent ( val ) . replace ( /%40/gi , '@' ) . replace ( /%3A/gi , ':' ) . replace ( /%24/g , '$' ) . replace ( /%2C/gi , ',' ) . replace ( /%3B/gi , ';' ) . replace ( /%20/g , pctEncodeSpaces ? '%20' : '+' ) ; } export const encodeQuery = ( query , encodeFunction = encodeUriQuery ) => transform ( query , ( result , value , key ) => { if ( key ) { const singleValue = Array . isArray ( value ) ? value . join ( ',' ) : value ; result [ key ] = encodeFunction ( singleValue === undefined || singleValue === null ? '' : singleValue , true ) ; } } ) ;