Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix time.recent method signature #586

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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