This commit is contained in:
Ahmed Bouhuolia
2020-03-16 00:06:15 +02:00
parent 56701951b7
commit 73711384f6
7925 changed files with 18478 additions and 959 deletions

79
client/src/utils.js Normal file
View File

@@ -0,0 +1,79 @@
import moment from 'moment';
export function removeEmptyFromObject(obj) {
obj = Object.assign({}, obj);
var keys = Object.keys(obj);
keys.forEach(function(key) {
const value = obj[key];
if (value === '' || value === null || value === undefined ) {
delete obj[key];
}
});
return obj;
};
export const optionsMapToArray = (optionsMap, service = '') => {
return Object.keys(optionsMap).map((optionKey) => {
const optionValue = optionsMap[optionKey];
return {
key: service ? `${service}_${optionKey}` : `${optionKey}`,
value: optionValue,
};
})
};
export const optionsArrayToMap = (optionsArray) => {
return optionsArray.reduce((map, option) => {
map[option.key] = option.value;
return map;
}, {});
};
export function numberComma(number){
number = typeof number === 'number' ? String(number) : number;
const parts = number.split('.');
const integer = parts[0] || '0';
const decimal = parts[1];
const postfix = decimal ? `.${decimal}` : '';
return `${integer.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')}${postfix}`;
}
export const momentFormatter = (format) => {
return {
formatDate: date => moment(date).format(format),
parseDate: str => moment(str, format).toDate(),
placeholder: `${format} (moment)`,
};
}
/** Event handler that exposes the target element's value as a boolean. */
export const handleBooleanChange = (handler) => {
return (event) => handler(event.target.checked);
};
/** Event handler that exposes the target element's value as a string. */
export const handleStringChange = (handler) => {
return (event) => handler(event.target.value);
};
/** Event handler that exposes the target element's value as a number. */
export const handleNumberChange = (handler) => {
return handleStringChange(value => handler(+value));
};
export const objectKeysTransform = (obj, transform) => {
return Object.keys(obj).reduce((acc, key) => {
const computedKey = transform(key);
acc[computedKey] = obj[key];
return acc;
}, {});
};
export const compose = (...funcs) =>
funcs.reduce((a, b) => (...args) => a(b(...args)), arg => arg);