From 366fcbf4f3a4ac978f817a56690df1aab5ad9230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 2 Jun 2015 13:42:17 +0200 Subject: [PATCH] refactor general utils --- js/components/ascribe_table/table_header.js | 1 - js/mixins/table_column_mixin.js | 4 +- js/utils/fetch_api_utils.js | 4 +- js/utils/general_utils.js | 125 +++++++++----------- js/utils/lang_utils.js | 13 +- 5 files changed, 70 insertions(+), 77 deletions(-) diff --git a/js/components/ascribe_table/table_header.js b/js/components/ascribe_table/table_header.js index 8176ad31..44594394 100644 --- a/js/components/ascribe_table/table_header.js +++ b/js/components/ascribe_table/table_header.js @@ -1,7 +1,6 @@ import React from 'react'; import TableColumnMixin from '../../mixins/table_column_mixin'; -import GeneralUtils from '../../utils/general_utils'; import TableHeaderItem from './table_header_item'; import TableColumnContentModel from '../../models/table_column_content_model'; diff --git a/js/mixins/table_column_mixin.js b/js/mixins/table_column_mixin.js index d74214e1..eac266b6 100644 --- a/js/mixins/table_column_mixin.js +++ b/js/mixins/table_column_mixin.js @@ -1,6 +1,6 @@ import React from 'react'; -import GeneralUtils from '../utils/general_utils'; +import { sumNumList } from '../utils/general_utils'; let TableColumnMixin = { /** @@ -11,7 +11,7 @@ let TableColumnMixin = { let bootstrapClasses = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-']; let listOfRowValues = list.map((column) => column.rowWidth ); - let numOfUsedColumns = GeneralUtils.sumNumList(listOfRowValues); + let numOfUsedColumns = sumNumList(listOfRowValues); if(numOfUsedColumns > numOfColumns) { throw new Error('This table has only ' + numOfColumns + ' columns to assign. You defined ' + numOfUsedColumns + '. Change this in the columnMap you\'re passing to the table.') diff --git a/js/utils/fetch_api_utils.js b/js/utils/fetch_api_utils.js index 47b52e66..ccee8ae5 100644 --- a/js/utils/fetch_api_utils.js +++ b/js/utils/fetch_api_utils.js @@ -1,4 +1,4 @@ -import GeneralUtils from './general_utils'; +import { sanitize } from './general_utils'; // TODO: Create Unittests that test all functions @@ -22,7 +22,7 @@ let FetchApiUtils = { */ argsToQueryParams(obj) { - obj = GeneralUtils.sanitize(obj); + obj = sanitize(obj); return Object .keys(obj) diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index 14118d94..96f56c1e 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -1,74 +1,63 @@ // TODO: Create Unittests that test all functions -let GeneralUtils = { - /** - * Removes undefined and null values from an key-value object. - */ - sanitize(obj) { - Object - .keys(obj) - .map((key) => { - // By matching null with a double equal, we can match undefined and null - // http://stackoverflow.com/a/15992131 - if(obj[key] == null || obj[key] === '') { - delete obj[key]; - } - }); - - return obj; - }, - - /** - * Returns the values of an object. - */ - valuesOfObject(obj) { - return Object - .keys(obj) - .map(key => obj[key]); - }, - - /** - * Sums up a list of numbers. Like a Epsilon-math-kinda-sum... - */ - sumNumList(l) { - let sum = 0; - l.forEach((num) => sum += parseFloat(num) || 0); - return sum; - }, - - /* - Taken from http://stackoverflow.com/a/4795914/1263876 - Behaves like C's format string function - - Does not throw errors though if a argument's type is not - matching its template's representative!!! - This essentially means you can use %d or %s for anything... - */ - formatText() { - var args = arguments, - string = args[0], - i = 1; - return string.replace(/%((%)|s|d)/g, function (m) { - // m is the matched format, e.g. %s, %d - var val = null; - if (m[2]) { - val = m[2]; - } else { - val = args[i]; - // A switch statement so that the formatter can be extended. Default is %s - switch (m) { - case '%d': - val = parseFloat(val); - if (isNaN(val)) { - val = 0; - } - break; - } - i++; +export function sanitize(obj) { + Object + .keys(obj) + .map((key) => { + // By matching null with a double equal, we can match undefined and null + // http://stackoverflow.com/a/15992131 + if(obj[key] == null || obj[key] === '') { + delete obj[key]; } - return val; }); - } + + return obj; }; -export default GeneralUtils; +/** + * Returns the values of an object. + */ +export function valuesOfObject(obj) { + return Object + .keys(obj) + .map(key => obj[key]); +}; + +/** + * Sums up a list of numbers. Like a Epsilon-math-kinda-sum... + */ +export function sumNumList(l) { + let sum = 0; + l.forEach((num) => sum += parseFloat(num) || 0); + return sum; +}; + +/* + Taken from http://stackoverflow.com/a/4795914/1263876 + Behaves like C's format string function +*/ +export function formatText() { + var args = arguments, + string = args[0], + i = 1; + return string.replace(/%((%)|s|d)/g, function (m) { + // m is the matched format, e.g. %s, %d + var val = null; + if (m[2]) { + val = m[2]; + } else { + val = args[i]; + // A switch statement so that the formatter can be extended. Default is %s + switch (m) { + case '%d': + val = parseFloat(val); + if (isNaN(val)) { + val = 0; + } + break; + } + i++; + } + return val; + }); +}; diff --git a/js/utils/lang_utils.js b/js/utils/lang_utils.js index 0eddcd30..8157caf9 100644 --- a/js/utils/lang_utils.js +++ b/js/utils/lang_utils.js @@ -1,19 +1,24 @@ import languages from '../constants/languages'; -import GeneralUtils from './general_utils'; +import { formatText } from './general_utils'; let getLangText = function(s, ...args) { let lang = navigator.language || navigator.userLanguage; try { if(lang in languages) { - return GeneralUtils.formatText(languages[lang][s], args); + return formatText(languages[lang][s], args); } else { // just use the english language - return GeneralUtils.formatText(languages['en-US'][s], args); + return formatText(languages['en-US'][s], args); } } catch(err) { - console.error(new Error('Language-string is not in constants file.')); + if(!(s in languages[lang])) { + console.error(new Error('Language-string is not in constants file for string: ' + s)); + } else { + console.error(err); + } + } };