Skip to content

Commit

Permalink
feat: migrate time (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Jan 14, 2022
1 parent 7bb491c commit 6d57002
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Helpers } from './helpers';
import { Mersenne } from './mersenne';
import { Name } from './name';
import { Random } from './random';
import { Time } from './time';

export interface FakerOptions {
locales?: string[];
Expand Down Expand Up @@ -185,7 +186,7 @@ export class Faker {
readonly name: Name = new Name(this);
readonly phone = new (require('./phone_number'))(this);
readonly system = new (require('./system'))(this);
readonly time = new (require('./time'))(this);
readonly time: Time = new Time();
readonly vehicle = new (require('./vehicle'))(this);
readonly word = new (require('./word'))(this);

Expand Down
28 changes: 28 additions & 0 deletions src/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export class Time {
/**
* recent
*
* @method faker.time.recent
* @param outputType 'abbr' || 'wide' || 'unix' (default choice)
*/
recent(outputType: '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
let date: string | number | Date = new Date();

switch (outputType) {
case 'abbr':
date = date.toLocaleTimeString();
break;
case 'wide':
date = date.toTimeString();
break;
case 'unix':
// TODO @Shinigami92 2022-01-10: add default case
date = date.getTime();
break;
}

return date;
}
}

0 comments on commit 6d57002

Please sign in to comment.