From 7befc357552deb6bbe3055cd09dca21dc468e36b Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 7 Aug 2022 18:30:35 -0400 Subject: [PATCH] feat: `datatype.hexadecimal` signature change --- src/modules/datatype/index.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 347013681b5..f084bd799ee 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -1,5 +1,6 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; +import { deprecated } from '../../internal/deprecated'; /** * Module to generate various primitive values and data types. @@ -187,13 +188,32 @@ export class Datatype { /** * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number. * - * @param length Length of the generated number. Defaults to `1`. + * @param length Deprecated, do not use. + * @param options The optional options object. + * @param options.length Length of the generated number. Defaults to `1`. + * @param options.prefix Prefix for the generated number. Defaults to `''`. * * @example * faker.datatype.hexadecimal() // '0xb' * faker.datatype.hexadecimal(10) // '0xaE13F044fb' */ - hexadecimal(length = 1): string { + hexadecimal( + length?: number, + options?: { length?: number; prefix?: string } + ): string { + if (length) { + deprecated({ + deprecated: 'faker.datatype.hexadecimal(length)', + proposed: 'faker.datatype.hexadecimal({ length })', + since: '7.3', + until: '8.0', + }); + } else { + length = options?.length ?? 1; + } + + const prefix = options?.prefix ?? ''; + let wholeString = ''; for (let i = 0; i < length; i++) { @@ -223,7 +243,7 @@ export class Datatype { ]); } - return `0x${wholeString}`; + return `${prefix}${wholeString}`; } /**