-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipes): added formatDuration pipes
Added two new pipes: `dfnsFormatDuration` and `dfnsFormatDurationPure` BREAKING CHANGE: This version requires date-fns >= v2.16.0 #351
- Loading branch information
1 parent
2b45dc2
commit 03af4a5
Showing
8 changed files
with
457 additions
and
149 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
projects/ngx-date-fns/src/lib/format-duration.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import { ChangeDetectorRef } from '@angular/core'; | ||
import { DateFnsConfigurationService } from './date-fns-configuration.service'; | ||
import { FormatDurationPipe } from './format-duration.pipe'; | ||
import { es } from 'date-fns/locale'; | ||
import { FormatDurationPurePipe } from './format-duration.pure.pipe'; | ||
|
||
describe('FormatDurationPipe', () => { | ||
let pipe: FormatDurationPipe; | ||
|
||
const createPipe = () => { | ||
const MyChangeDetector = class extends ChangeDetectorRef { | ||
markForCheck(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
detach(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
detectChanges(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
checkNoChanges(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
reattach(): void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
}; | ||
return new FormatDurationPipe( | ||
new DateFnsConfigurationService(), | ||
new MyChangeDetector() | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
pipe = createPipe(); | ||
}); | ||
|
||
it('should format full durations', () => { | ||
expect( | ||
pipe.transform({ | ||
years: 2, | ||
months: 9, | ||
weeks: 1, | ||
days: 7, | ||
hours: 5, | ||
minutes: 9, | ||
seconds: 30 | ||
}) | ||
).toBe('2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'); | ||
}); | ||
|
||
it('should format partial durations', () => { | ||
expect(pipe.transform({ months: 9, days: 2 })).toBe('9 months 2 days'); | ||
}); | ||
|
||
it('should customize the format', () => { | ||
expect( | ||
pipe.transform( | ||
{ | ||
years: 2, | ||
months: 9, | ||
weeks: 1, | ||
days: 7, | ||
hours: 5, | ||
minutes: 9, | ||
seconds: 30 | ||
}, | ||
{ format: ['months', 'weeks'] } | ||
) | ||
).toBe('9 months 1 week'); | ||
}); | ||
|
||
it('should customize the zeroes presence', () => { | ||
expect(pipe.transform({ years: 0, months: 9 }, { zero: true })).toBe( | ||
'0 years 9 months' | ||
); | ||
}); | ||
|
||
it('should customize the delimiter', () => { | ||
expect( | ||
pipe.transform({ years: 2, months: 9, weeks: 3 }, { delimiter: ', ' }) | ||
).toBe('2 years, 9 months, 3 weeks'); | ||
}); | ||
|
||
it('should customize the locale', () => { | ||
expect( | ||
pipe.transform({ years: 2, months: 9, weeks: 3 }, { locale: es }) | ||
).toBe('2 años 9 meses 3 semanas'); | ||
}); | ||
}); | ||
|
||
describe('FormatDurationPipe Pure', () => { | ||
const createPipe = () => | ||
new FormatDurationPurePipe(new DateFnsConfigurationService()); | ||
|
||
it('should format full durations', () => { | ||
expect( | ||
createPipe().transform({ | ||
years: 2, | ||
months: 9, | ||
weeks: 1, | ||
days: 7, | ||
hours: 5, | ||
minutes: 9, | ||
seconds: 30 | ||
}) | ||
).toBe('2 years 9 months 1 week 7 days 5 hours 9 minutes 30 seconds'); | ||
}); | ||
|
||
it('should format partial durations', () => { | ||
expect(createPipe().transform({ months: 9, days: 2 })).toBe( | ||
'9 months 2 days' | ||
); | ||
}); | ||
|
||
it('should customize the format', () => { | ||
expect( | ||
createPipe().transform( | ||
{ | ||
years: 2, | ||
months: 9, | ||
weeks: 1, | ||
days: 7, | ||
hours: 5, | ||
minutes: 9, | ||
seconds: 30 | ||
}, | ||
{ format: ['months', 'weeks'] } | ||
) | ||
).toBe('9 months 1 week'); | ||
}); | ||
|
||
it('should customize the zeroes presence', () => { | ||
expect( | ||
createPipe().transform({ years: 0, months: 9 }, { zero: true }) | ||
).toBe('0 years 9 months'); | ||
}); | ||
|
||
it('should customize the delimiter', () => { | ||
expect( | ||
createPipe().transform( | ||
{ years: 2, months: 9, weeks: 3 }, | ||
{ delimiter: ', ' } | ||
) | ||
).toBe('2 years, 9 months, 3 weeks'); | ||
}); | ||
|
||
it('should customize the locale', () => { | ||
expect( | ||
createPipe().transform({ years: 2, months: 9, weeks: 3 }, { locale: es }) | ||
).toBe('2 años 9 meses 3 semanas'); | ||
}); | ||
|
||
it('should customize the locale provided via config', () => { | ||
const conf = new DateFnsConfigurationService(); | ||
conf.setLocale(es); | ||
const pipe = new FormatDurationPurePipe(conf); | ||
expect(pipe.transform({ years: 2, months: 9, weeks: 3 })).toBe( | ||
'2 años 9 meses 3 semanas' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
Pipe, | ||
PipeTransform, | ||
ChangeDetectorRef, | ||
OnDestroy, | ||
NgModule | ||
} from '@angular/core'; | ||
import { | ||
DateFnsConfigurationService, | ||
calculateLocale | ||
} from './date-fns-configuration.service'; | ||
import { Subscription } from 'rxjs'; | ||
import { Locale } from 'date-fns'; | ||
import formatDuration from 'date-fns/formatDuration'; | ||
|
||
@Pipe({ name: 'dfnsFormatDuration', pure: false }) | ||
export class FormatDurationPipe implements PipeTransform, OnDestroy { | ||
private localeChanged$: Subscription; | ||
|
||
constructor( | ||
public config: DateFnsConfigurationService, | ||
public cd: ChangeDetectorRef | ||
) { | ||
this.localeChanged$ = this.config.localeChanged.subscribe(_ => | ||
this.cd.markForCheck() | ||
); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.localeChanged$.unsubscribe(); | ||
} | ||
|
||
transform( | ||
duration: Duration, | ||
options?: { | ||
format?: string[]; | ||
zero?: boolean; | ||
delimiter?: string; | ||
locale?: Locale; | ||
} | ||
): string { | ||
return formatDuration(duration, calculateLocale(options, this.config)); | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [FormatDurationPipe], | ||
exports: [FormatDurationPipe] | ||
}) | ||
export class FormatDurationPipeModule {} |
30 changes: 30 additions & 0 deletions
30
projects/ngx-date-fns/src/lib/format-duration.pure.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NgModule, Pipe, PipeTransform } from '@angular/core'; | ||
import { Locale } from 'date-fns'; | ||
import formatDuration from 'date-fns/formatDuration'; | ||
import { | ||
calculateLocale, | ||
DateFnsConfigurationService | ||
} from './date-fns-configuration.service'; | ||
|
||
@Pipe({ name: 'dfnsFormatDurationPure' }) | ||
export class FormatDurationPurePipe implements PipeTransform { | ||
constructor(public config: DateFnsConfigurationService) {} | ||
|
||
transform( | ||
duration: Duration, | ||
options?: { | ||
format?: string[]; | ||
zero?: boolean; | ||
delimiter?: string; | ||
locale?: Locale; | ||
} | ||
): string { | ||
return formatDuration(duration, calculateLocale(options, this.config)); | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [FormatDurationPurePipe], | ||
exports: [FormatDurationPurePipe] | ||
}) | ||
export class FormatDurationPurePipeModule {} |