From 6d57002788354985c6536396ef2994b00ac11429 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Fri, 14 Jan 2022 17:04:42 +0100 Subject: [PATCH] feat: migrate time (#74) --- src/index.ts | 3 ++- src/time.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/time.ts diff --git a/src/index.ts b/src/index.ts index ddfda1c9749..f8e087642ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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[]; @@ -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); diff --git a/src/time.ts b/src/time.ts new file mode 100644 index 00000000000..cf5190e60c6 --- /dev/null +++ b/src/time.ts @@ -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; + } +}