util.ts 807 B

1234567891011121314151617181920212223242526272829
  1. export const formatTime = (date: Date) => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return (
  9. [year, month, day].map(formatNumber).join('/') +
  10. ' ' +
  11. [hour, minute, second].map(formatNumber).join(':')
  12. )
  13. }
  14. const formatNumber = (n: number) => {
  15. const s = n.toString()
  16. return s[1] ? s : '0' + s
  17. }
  18. export function groupBy<T>(items: Iterable<T>, callbackFn: (element: T, index: number) => any): Record<string, T[]> {
  19. const obj = Object.create(null);
  20. let k = 0;
  21. for (const value of items) {
  22. const key = callbackFn(value, k++);
  23. if (key in obj) { obj[key].push(value); } else { obj[key] = [value]; }
  24. }
  25. return obj;
  26. }