import moment from "moment"; import { RegionWiseBranchesType } from "../types/regionWiseBranch"; const regionOrder = ["south", "west", "east", "north", "central"]; const util = { /** * @return true if given object is empty */ isEmptyObject: (objectName: object) => { return Object.keys(objectName).length === 0 }, /** * @return true if given array is empty */ isEmptyArray: (arrayName: Array) => { return arrayName?.length === 0 }, /** * Remove duplicate items from array * @param arr array of items with duplicate * @returns Duplicate removed array */ removeDuplicate: (arr: Array) => { return arr.filter((obj, pos, arr) => { return arr.map(mapObj => mapObj).indexOf(obj) === pos; }); }, /** * Remove duplicate items from array * @param arr array of items with duplicate * @param prop key name to identify the duplicate * @returns Duplicate removed array */ removeDuplicateWithKey: (arr: Array, prop: string) => { return arr.filter((obj, pos, arr) => { return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos; }); }, /** * get sorted region as [s,w,e,n,c] * @param obj region as key and array of branches * @returns sorted region object */ regionSort: (obj:RegionWiseBranchesType) => { const sortedObject = Object.fromEntries( Object.entries(obj) .sort(([key1], [key2]) => regionOrder.indexOf(key1) - regionOrder.indexOf(key2)) ) return sortedObject }, getLatLng : (location:string) => { if(!location){ return [0, 0] } else { const splitLocation = location?.split(','); const lati = parseFloat(splitLocation[1]?.replace(')', '')); const long = parseFloat(splitLocation[0]?.replace('(', '')); return [long,lati] } }, /** Format date with Moment * @param date: string * @param format: string * @returns Date string by given format */ formatDate: (date: string, format?: string) => { return date ? moment(date).format(format || "DD-MMM HH:mm" ) : "-"; }, percentageCalculator: (total: number, value: number) => { let percent = 0 const traveled = total - value if (traveled < total && traveled > 0) { percent = (traveled / total) * 100 } else if (traveled < 1) { percent = 1 } else { percent = 100 } return percent }, roundedBy50: (value: number) => { const res = Math.round(value/50) * 50 return res }, getLastNDaysArray: (n:number) => { const dates = []; const today = new Date(); for (let i = 0; i < n; i++) { const date = new Date(today); date.setDate(today.getDate() - i); const day = String(date.getDate()).padStart(2, "0"); const month = String(date.getMonth() + 1).padStart(2, "0"); const year = String(date.getFullYear()).slice(-2); const formattedDate = `${day}-${month}-${year}`; dates.push(formattedDate); } return dates; }, formatDateDDMMYY: (dateStr:string) => { const date = new Date(dateStr); const day = String(date.getDate()).padStart(2, "0"); const month = String(date.getMonth() + 1).padStart(2, "0"); const year = String(date.getFullYear()).slice(-2); return `${day}-${month}-${year}`; }, debounce void>(func: T, delay = 50) { let timeoutId: ReturnType | null; return function (this: ThisParameterType, ...args: Parameters) { if (timeoutId) { clearTimeout(timeoutId); } timeoutId = setTimeout(() => { func.apply(this, args); }, delay); }; } } export default util