From 8689585d7473485ae99627dedb22b3f6b370771e Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Mon, 13 Jun 2016 15:30:20 +0200 Subject: [PATCH] Use js-utility-belt's url utils --- .../ascribe_buttons/s3_download_button.js | 4 +- js/utils/requests.js | 4 +- js/utils/url.js | 70 +------------------ 3 files changed, 6 insertions(+), 72 deletions(-) diff --git a/js/components/ascribe_buttons/s3_download_button.js b/js/components/ascribe_buttons/s3_download_button.js index aec9db0b..71e2b6cc 100644 --- a/js/components/ascribe_buttons/s3_download_button.js +++ b/js/components/ascribe_buttons/s3_download_button.js @@ -9,7 +9,7 @@ import S3Fetcher from '../../fetchers/s3_fetcher'; import AppConstants from '../../constants/application_constants'; import { getLangText } from '../../utils/lang'; -import { queryParamsToArgs } from '../../utils/url'; +import { parseQueryParamStr } from '../../utils/url'; const { string } = React.PropTypes; @@ -52,7 +52,7 @@ const S3DownloadButton = React.createClass({ // The signed url, however can expire, which is why we need to set up a timer to // renew it when it expires. - const expires = parseInt(queryParamsToArgs(downloadUrl.split('?')[1]).expires, 10); + const expires = parseInt(parseQueryParamStr(downloadUrl).expires, 10); const now = new Date().getTime() / 1000; // Amazon uses seconds as their signature unix timestamp while `setTimeout` uses diff --git a/js/utils/requests.js b/js/utils/requests.js index fc88df3e..b16038c4 100644 --- a/js/utils/requests.js +++ b/js/utils/requests.js @@ -6,7 +6,7 @@ import AppConstants from '../constants/application_constants'; import { getCookie } from '../utils/fetch_api'; import { omitFromObject } from '../utils/general'; -import { argsToQueryParams } from '../utils/url'; +import { stringifyAsQueryParam } from '../utils/url'; class Requests { @@ -108,7 +108,7 @@ class Requests { }); if (attachParamsToQuery && params && Object.keys(params).length > 0) { - newUrl += argsToQueryParams(params); + newUrl += stringifyAsQueryParam(params); } return newUrl; diff --git a/js/utils/url.js b/js/utils/url.js index 881ff5b0..87225e29 100644 --- a/js/utils/url.js +++ b/js/utils/url.js @@ -1,71 +1,5 @@ -'use strict'; - -import camelCase from 'camelcase'; -import decamelize from 'decamelize'; -import queryString from 'query-string'; - -import { sanitize } from './general'; - -// TODO: Create Unittests that test all functions - -/** - * Takes a key-value dictionary of this form: - * - * { - * 'page': 1, - * 'pageSize': 10 - * } - * - * and converts it to a query-parameter, which you can append to your URL. - * The return looks like this: - * - * ?page=1&page_size=10 - * - * CamelCase gets converted to snake_case! - * - * @param {object} obj Query params dictionary - * @return {string} Query params string - */ -export function argsToQueryParams(obj) { - const sanitizedObj = sanitize(obj); - const queryParamObj = {}; - - Object - .keys(sanitizedObj) - .forEach((key) => { - queryParamObj[decamelize(key)] = sanitizedObj[key]; - }); - - return '?' + queryString.stringify(queryParamObj); -} - -/** - * Get the current url's query params as an key-val dictionary. - * snake_case gets converted to CamelCase! - * @return {object} Query params dictionary - */ -export function getCurrentQueryParams() { - return queryParamsToArgs(window.location.search.substring(1)); -} - -/** - * Convert the given query param string into a key-val dictionary. - * snake_case gets converted to CamelCase! - * @param {string} queryParamString Query params string - * @return {object} Query params dictionary - */ -export function queryParamsToArgs(queryParamString) { - const queryParamObj = queryString.parse(queryParamString); - const camelCaseParamObj = {}; - - Object - .keys(queryParamObj) - .forEach((key) => { - camelCaseParamObj[camelCase(key)] = queryParamObj[key]; - }); - - return camelCaseParamObj; -} +// Re-export related utilities from js-utility-belt for easier access +export { getCurrentQueryParams, stringifyAsQueryParam, parseQueryParamStr } from 'js-utility-belt/es6/url'; /** * Takes a string and a boolean and generates a string query parameter for