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

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