return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + `/${jobState || ''}`, { params })
.pipe(
- map(res => {
- return this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ])
- }),
+ map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ], 'precise')),
map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)),
map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)),
catchError(err => this.restExtractor.handleError(err))
import { throwError as observableThrowError } from 'rxjs'
-import { Injectable } from '@angular/core'
+import { Inject, Injectable, LOCALE_ID } from '@angular/core'
import { Router } from '@angular/router'
-import { dateToHuman } from '@app/helpers'
-import { HttpStatusCode, ResultList } from '@shared/models'
+import { DateFormat, dateToHuman } from '@app/helpers'
import { logger } from '@root-helpers/logger'
+import { HttpStatusCode, ResultList } from '@shared/models'
@Injectable()
export class RestExtractor {
- constructor (private router: Router) { }
+ constructor (
+ @Inject(LOCALE_ID) private localeId: string,
+ private router: Router
+ ) { }
applyToResultListData <T, A, U> (
result: ResultList<T>,
}
}
- convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
- return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
+ convertResultListDateToHuman <T> (
+ result: ResultList<T>,
+ fieldsToConvert: string[] = [ 'createdAt' ],
+ format?: DateFormat
+ ): ResultList<T> {
+ return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert, format ])
}
- convertDateToHuman (target: any, fieldsToConvert: string[]) {
+ convertDateToHuman (target: any, fieldsToConvert: string[], format?: DateFormat) {
fieldsToConvert.forEach(field => {
- target[field] = dateToHuman(target[field])
+ target[field] = dateToHuman(this.localeId, new Date(target[field]), format)
})
return target
import { DatePipe } from '@angular/common'
-const datePipe = new DatePipe('en')
-function dateToHuman (date: string) {
- return datePipe.transform(date, 'medium')
+let datePipe: DatePipe
+let intl: Intl.DateTimeFormat
+
+type DateFormat = 'medium' | 'precise'
+
+function dateToHuman (localeId: string, date: Date, format: 'medium' | 'precise' = 'medium') {
+ if (!datePipe) {
+ datePipe = new DatePipe(localeId)
+ }
+
+ if (!intl) {
+ intl = new Intl.DateTimeFormat(localeId, {
+ hour: 'numeric',
+ minute: 'numeric',
+ second: 'numeric',
+ year: '2-digit',
+ month: 'numeric',
+ day: 'numeric',
+ fractionalSecondDigits: 3
+ })
+ }
+
+ if (format === 'medium') return datePipe.transform(date, format)
+ if (format === 'precise') return intl.format(date)
}
function durationToString (duration: number) {
}
export {
+ DateFormat,
+
durationToString,
dateToHuman
}