Rev 3032 | Rev 3692 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import htmlParse from 'html-react-parser';
const jsonToParams = (data = {}) => {
if (!data) {
return false;
}
const formBody = [];
for (const property in data) {
const encodedKey = encodeURIComponent(property);
// @ts-ignore
const encodedValue = encodeURIComponent(data[property]);
formBody.push(encodedKey + '=' + encodedValue);
}
const _formBody = formBody.join('&');
return _formBody;
};
const filterItems = (query = '', items = []) => {
if (!query) {
return items;
}
return items.filter((conversation) =>
conversation.name.toLowerCase().includes(query.toLowerCase())
);
};
const debounce = (func, timeout = 300) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
};
const camalize = function camalize(str) {
return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
};
const scrollToBottom = (element) => {
if (!element.current) {
return false;
}
element.scrollTop = element.scrollHeight * 9;
};
const formatObjectToArray = (object) => {
if (!object) return [];
return Object.entries(object).map(([key, value]) => ({
label: value,
value: key
}));
};
const isPromise = (obj) => {
return (
(!!obj && obj[Symbol.toStringTag] === 'Promise') || obj[Symbol.toStringTag] === 'AsyncFunction'
);
};
const parse = (str) => {
if (typeof str !== 'string') return '';
const stripText = str.replace(/<p>|<\/p>/g, '');
return htmlParse(stripText);
};
export {
jsonToParams,
debounce,
scrollToBottom,
filterItems,
camalize,
formatObjectToArray,
isPromise,
parse
};