1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00

refactor general utils

This commit is contained in:
Tim Daubenschütz 2015-06-02 13:42:17 +02:00
parent 68be98c3f0
commit 366fcbf4f3
5 changed files with 70 additions and 77 deletions

View File

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import TableColumnMixin from '../../mixins/table_column_mixin'; import TableColumnMixin from '../../mixins/table_column_mixin';
import GeneralUtils from '../../utils/general_utils';
import TableHeaderItem from './table_header_item'; import TableHeaderItem from './table_header_item';
import TableColumnContentModel from '../../models/table_column_content_model'; import TableColumnContentModel from '../../models/table_column_content_model';

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import GeneralUtils from '../utils/general_utils'; import { sumNumList } from '../utils/general_utils';
let TableColumnMixin = { let TableColumnMixin = {
/** /**
@ -11,7 +11,7 @@ let TableColumnMixin = {
let bootstrapClasses = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-']; let bootstrapClasses = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-'];
let listOfRowValues = list.map((column) => column.rowWidth ); let listOfRowValues = list.map((column) => column.rowWidth );
let numOfUsedColumns = GeneralUtils.sumNumList(listOfRowValues); let numOfUsedColumns = sumNumList(listOfRowValues);
if(numOfUsedColumns > numOfColumns) { 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.') 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.')

View File

@ -1,4 +1,4 @@
import GeneralUtils from './general_utils'; import { sanitize } from './general_utils';
// TODO: Create Unittests that test all functions // TODO: Create Unittests that test all functions
@ -22,7 +22,7 @@ let FetchApiUtils = {
*/ */
argsToQueryParams(obj) { argsToQueryParams(obj) {
obj = GeneralUtils.sanitize(obj); obj = sanitize(obj);
return Object return Object
.keys(obj) .keys(obj)

View File

@ -1,10 +1,6 @@
// TODO: Create Unittests that test all functions // TODO: Create Unittests that test all functions
let GeneralUtils = { export function sanitize(obj) {
/**
* Removes undefined and null values from an key-value object.
*/
sanitize(obj) {
Object Object
.keys(obj) .keys(obj)
.map((key) => { .map((key) => {
@ -16,35 +12,31 @@ let GeneralUtils = {
}); });
return obj; return obj;
}, };
/** /**
* Returns the values of an object. * Returns the values of an object.
*/ */
valuesOfObject(obj) { export function valuesOfObject(obj) {
return Object return Object
.keys(obj) .keys(obj)
.map(key => obj[key]); .map(key => obj[key]);
}, };
/** /**
* Sums up a list of numbers. Like a Epsilon-math-kinda-sum... * Sums up a list of numbers. Like a Epsilon-math-kinda-sum...
*/ */
sumNumList(l) { export function sumNumList(l) {
let sum = 0; let sum = 0;
l.forEach((num) => sum += parseFloat(num) || 0); l.forEach((num) => sum += parseFloat(num) || 0);
return sum; return sum;
}, };
/* /*
Taken from http://stackoverflow.com/a/4795914/1263876 Taken from http://stackoverflow.com/a/4795914/1263876
Behaves like C's format string function 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() { export function formatText() {
var args = arguments, var args = arguments,
string = args[0], string = args[0],
i = 1; i = 1;
@ -68,7 +60,4 @@ let GeneralUtils = {
} }
return val; return val;
}); });
}
}; };
export default GeneralUtils;

View File

@ -1,19 +1,24 @@
import languages from '../constants/languages'; import languages from '../constants/languages';
import GeneralUtils from './general_utils'; import { formatText } from './general_utils';
let getLangText = function(s, ...args) { let getLangText = function(s, ...args) {
let lang = navigator.language || navigator.userLanguage; let lang = navigator.language || navigator.userLanguage;
try { try {
if(lang in languages) { if(lang in languages) {
return GeneralUtils.formatText(languages[lang][s], args); return formatText(languages[lang][s], args);
} else { } else {
// just use the english language // just use the english language
return GeneralUtils.formatText(languages['en-US'][s], args); return formatText(languages['en-US'][s], args);
} }
} catch(err) { } 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);
}
} }
}; };