import * as moment from 'moment'; /* * groups an array of object into an object of arrays, * with as a key an identifier that is shared between * the array's objects that should be grouped */ export function groupBy(array: Object[], identifier: string | ((item: Object) => any)) { return array.reduce((groupedValues, currentValue) => { const id = typeof identifier === 'string' ? currentValue[identifier] : identifier(currentValue); if (!groupedValues[id]) { groupedValues[id] = [currentValue]; } else { groupedValues[id].push(currentValue); } return groupedValues; }, {}); } /* * returns the last n items of the given array, * or all of them if the desired count is greater than the items in the array */ export function lastItems(array: any[], count: number) { return array.length > count ? array.slice(array.length - count) : array; } /* /* turns Date into string of format 'HH:mm:ss' */ export function dateToDaytime(date: Date) { return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`; } /** * TODO: FIND A (non-optimal) solution for correct dateparsing in safari * @param string datetime string */ export function parseDate(string: string): Date { return moment(string).toDate(); } /** * turns a UTC datestring into a Date * @param string string representing UTC date */ export function stringToUTCDate(string: string): Date { const date = new Date(string); return new Date( Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), ) ); }