From 1f149bb086078f75ff2c8737b93a2939797e33e3 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 19 Sep 2019 11:51:51 -0700 Subject: [PATCH] refactor encodeNoneDefaultQueryString --- awx/ui_next/src/util/qs.js | 47 ++++---------------------------------- 1 file changed, 4 insertions(+), 43 deletions(-) diff --git a/awx/ui_next/src/util/qs.js b/awx/ui_next/src/util/qs.js index 60d68127c2..e4b8b3cf75 100644 --- a/awx/ui_next/src/util/qs.js +++ b/awx/ui_next/src/util/qs.js @@ -52,7 +52,7 @@ function stringToObject(config, qs) { params[key] = mergeParam(params[key], value); }); return params; -}; +} export { stringToObject as _stringToObject }; /** @@ -120,28 +120,10 @@ function encodeValue(key, value) { export const encodeNonDefaultQueryString = (config, params) => { if (!params) return ''; - const namespacedParams = namespaceParams(config.namespace, params); - const namespacedDefaults = namespaceParams( - config.namespace, - config.defaultParams + const paramsWithoutDefaults = removeParams({}, params, config.defaultParams); + return encodeQueryString( + namespaceParams(config.namespace, paramsWithoutDefaults) ); - const namespacedDefaultKeys = Object.keys(namespacedDefaults); - const namespacedParamsWithoutDefaultsKeys = Object.keys( - namespacedParams - ).filter( - key => - namespacedDefaultKeys.indexOf(key) === -1 || - !paramValueIsEqual(namespacedParams[key], namespacedDefaults[key]) - ); - - return namespacedParamsWithoutDefaultsKeys - .sort() - .filter(key => namespacedParams[key] !== null) - .map(key => { - return [key, namespacedParams[key]]; - }) - .map(([key, value]) => encodeValue(key, value)) - .join('&'); }; /** @@ -161,27 +143,6 @@ const namespaceParams = (namespace, params) => { return namespaced; }; -/** - * helper function to check the value of a param is equal to another - * @param {string or number or array} param value one - * @param {string or number or array} params value two - * @return {boolean} true if values are equal - */ -const paramValueIsEqual = (one, two) => { - let isEqual = false; - - if (Array.isArray(one) && Array.isArray(two)) { - isEqual = one.filter(val => two.indexOf(val) > -1).length === one.length; - } else if ( - (typeof one === 'string' && typeof two === 'string') || - (typeof one === 'number' && typeof two === 'number') - ) { - isEqual = one === two; - } - - return isEqual; -}; - /** * Removes params from the search string and returns the updated list of params * @param {object} qs config object (used for getting defaults, current query params etc.)