Skip to content

Commit

Permalink
fix: fix time.recent method signature (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Mar 2, 2022
1 parent 357986b commit 60d3cc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import { Vehicle } from './vehicle';
import { Word } from './word';

// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never });
export type LiteralUnion<T extends U, U = string> =
| T
| (U & { zz_IGNORE_ME?: never });

export type UsableLocale = LiteralUnion<KnownLocale>;
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
Expand Down
18 changes: 13 additions & 5 deletions src/time.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import type { LiteralUnion } from './faker';

/**
* Module to generate time of dates in various formats.
*/
export class Time {
/**
* Returns recent time.
*
* @param format 'abbr' || 'wide' || 'unix' (default)
* @param format The format to use. Defaults to `'unix'`.
*
* - `'abbr'` Return a string with only the time. `Date.toLocaleTimeString`.
* - `'date'` Return a date instance.
* - `'wide'` Return a string with a long time. `Date.toTimeString()`.
* - `'unix'` Returns a unix timestamp.
*
* @example
* faker.time.recent() // 1643067231856
* faker.time.recent('abbr') // '12:34:07 AM'
* faker.time.recent('date') // 2022-03-01T20:35:47.402Z
* faker.time.recent('wide') // '00:34:11 GMT+0100 (Central European Standard Time)'
* faker.time.recent('unix') // 1643067231856
*/
recent(format: 'abbr' | 'wide' | 'unix' = 'unix'): string | number {
// TODO @Shinigami92 2022-01-11: This is not non-deterministic
// https://github.com/faker-js/faker/pull/74/files#r781579842
recent(
format: LiteralUnion<'abbr' | 'date' | 'wide' | 'unix'> = 'unix'
): string | number | Date {
// TODO ST-DDT 2022-03-01: Deprecate for removal - #557
let date: string | number | Date = new Date();

switch (format) {
Expand All @@ -26,7 +35,6 @@ export class Time {
date = date.toTimeString();
break;
case 'unix':
// TODO @Shinigami92 2022-01-10: add default case
date = date.getTime();
break;
}
Expand Down

0 comments on commit 60d3cc5

Please sign in to comment.