export const shorten = (address: string, fromEnd = 6, fromStart = 2): string => { const n = address.length; return address.substring(0, fromStart) + '...' + address.substring(n - fromEnd); }; type CallbackFunction = () => void; export function on( obj: T | null, ...args: Parameters | [string, CallbackFunction | null, ...any] ): void { if (obj && obj.addEventListener) { obj.addEventListener(...(args as Parameters)); } } export function off( obj: T | null, ...args: Parameters | [string, CallbackFunction | null, ...any] ): void { if (obj && obj.removeEventListener) { obj.removeEventListener(...(args as Parameters)); } } export const formatMoney = (value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ','); export const encodeQueryData = (data) => { const ret: string[] = []; for (const item in data) { if (data[item] === undefined) continue; if (Array.isArray(data[item])) { data[item].map((arrayItem) => { if (arrayItem) { ret.push(encodeURIComponent(item) + '=' + encodeURIComponent(arrayItem)); } }); continue; } ret.push(encodeURIComponent(item) + '=' + encodeURIComponent(data[item])); } return ret.join('&'); }; export const groupParamsByKey = (params) => [...params.entries()].reduce((acc, tuple) => { // getting the key and value from each tuple const [key, val] = tuple; if (acc.hasOwnProperty(key)) { // if the current key is already an array, we'll add the value to it if (Array.isArray(acc[key])) { acc[key] = [...acc[key], val]; } else { // if it's not an array, but contains a value, we'll convert it into an array // and add the current value to it acc[key] = [acc[key], val]; } } else { // plain assignment if no special case is present acc[key] = val; } return acc; }, {}); // envs export const isDevEnv = process.env.NODE_ENV === 'development'; export const isTestEnv = process.env.NODE_ENV === 'test'; export const isProdEnv = process.env.NODE_ENV === 'production';