awx/ui_next/src/util/qs.js
/**
 * Convert url query string to query param object
 * @param {object} qs config object (used for getting defaults, current query params etc.)
 * @param {string} url query string
 * @return {object} query param object
 */
Convert url query string to query param object
@param {object} qs config object (used for getting defaults, current query params etc.)
@param {string} url query string
@return {object} query param object
Convert url query string to query param object @param { object } qs config object ( used for getting defaults current query params etc . ) @param { string } url query string @return { object } query param object
function parseQueryString(config, queryString) {
  if (!queryString) return config.defaultParams;
  const namespacedIntegerFields = config.integerFields.map(f =>
    config.namespace ? `${config.namespace}.${f}` : f
  );
  const keyValuePairs = queryString
    .replace(/^\?/, '')
    .split('&')
    .map(s => s.split('='))
    .map(([key, value]) => {
      if (namespacedIntegerFields.includes(key)) {
        return [decodeURIComponent(key), parseInt(value, 10)];
      }
      return [decodeURIComponent(key), decodeURIComponent(value)];
    });
  const keyValueObject = toObject(keyValuePairs);
  const dedupedKeyValuePairs = Object.keys(keyValueObject).map(key => {
    const values = keyValuePairs.filter(([k]) => k === key).map(([, v]) => v);
    if (values.length === 1) {
      return [key, values[0]];
    }
    return [key, values];
  });
  const parsed = Object.assign(
    ...dedupedKeyValuePairs.map(([k, v]) => ({
      [k]: v,
    }))
  );
  const namespacedParams = {};
  Object.keys(parsed).forEach(field => {
    if (namespaceMatches(config.namespace, field)) {
      let fieldname = field;
      if (config.namespace) {
        fieldname = field.substr(config.namespace.length + 1);
      }
      namespacedParams[fieldname] = parsed[field];
    }
  });
  const namespacedDefaults = namespaceParams(
    config.namespace,
    config.defaultParams
  );
  Object.keys(namespacedDefaults)
    .filter(key => Object.keys(parsed).indexOf(key) === -1)
    .forEach(field => {
      if (namespaceMatches(config.namespace, field)) {
        let fieldname = field;
        if (config.namespace) {
          fieldname = field.substr(config.namespace.length + 1);
        }
        namespacedParams[fieldname] = namespacedDefaults[field];
      }
    });
  return namespacedParams;
}
function parseQueryString ( config , queryString ) { if ( ! queryString ) return config . defaultParams ; const namespacedIntegerFields = config . integerFields . map ( f => config . namespace ? ` ${ config . namespace } ${ f } ` : f ) ; const keyValuePairs = queryString . replace ( / ^\? / , '' ) . split ( '&' ) . map ( s => s . split ( '=' ) ) . map ( ( [ key , value ] ) => { if ( namespacedIntegerFields . includes ( key ) ) { return [ decodeURIComponent ( key ) , parseInt ( value , 10 ) ] ; } return [ decodeURIComponent ( key ) , decodeURIComponent ( value ) ] ; } ) ; const keyValueObject = toObject ( keyValuePairs ) ; const dedupedKeyValuePairs = Object . keys ( keyValueObject ) . map ( key => { const values = keyValuePairs . filter ( ( [ k ] ) => k === key ) . map ( ( [ , v ] ) => v ) ; if ( values . length === 1 ) { return [ key , values [ 0 ] ] ; } return [ key , values ] ; } ) ; const parsed = Object . assign ( ... dedupedKeyValuePairs . map ( ( [ k , v ] ) => ( { [ k ] : v , } ) ) ) ; const namespacedParams = { } ; Object . keys ( parsed ) . forEach ( field => { if ( namespaceMatches ( config . namespace , field ) ) { let fieldname = field ; if ( config . namespace ) { fieldname = field . substr ( config . namespace . length + 1 ) ; } namespacedParams [ fieldname ] = parsed [ field ] ; } } ) ; const namespacedDefaults = namespaceParams ( config . namespace , config . defaultParams ) ; Object . keys ( namespacedDefaults ) . filter ( key => Object . keys ( parsed ) . indexOf ( key ) === - 1 ) . forEach ( field => { if ( namespaceMatches ( config . namespace , field ) ) { let fieldname = field ; if ( config . namespace ) { fieldname = field . substr ( config . namespace . length + 1 ) ; } namespacedParams [ fieldname ] = namespacedDefaults [ field ] ; } } ) ; return namespacedParams ; }
Convert url query string to query param object
 @param {object} qs config object (used for getting defaults, current query params etc.)
Convert url query string to query param object @param { object } qs config object ( used for getting defaults current query params etc . )
// needs to return array for duplicate keys // ie [[k1, v1], [k1, v2], [k2, v3]] // -> [[k1, [v1, v2]], [k2, v3]]