1
0
mirror of https://github.com/ascribe/onion.git synced 2025-02-14 21:10:27 +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 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';

View File

@ -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.')

View File

@ -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)

View File

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

View File

@ -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);
}
}
};