diff options
| author | Eric Cheng <[email protected]> | 2022-08-10 03:31:53 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-10 07:31:53 +0000 |
| commit | 8cb6027087fbc3fd038c4063b78f283d9aa48959 (patch) | |
| tree | b4847e577e22f21ac7a92121028c6563b04a2101 /src | |
| parent | d4cfe979193c283be17016d6a1b03e745bf1511a (diff) | |
| download | faker-8cb6027087fbc3fd038c4063b78f283d9aa48959.tar.xz faker-8cb6027087fbc3fd038c4063b78f283d9aa48959.zip | |
feat: `datatype.hexadecimal` signature change (#1238)
Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/color/index.ts | 2 | ||||
| -rw-r--r-- | src/modules/database/index.ts | 3 | ||||
| -rw-r--r-- | src/modules/datatype/index.ts | 44 | ||||
| -rw-r--r-- | src/modules/finance/index.ts | 6 |
4 files changed, 46 insertions, 9 deletions
diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 8b5fdba2..d64b7750 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -297,7 +297,7 @@ export class Color { let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { - color = this.faker.datatype.hexadecimal(includeAlpha ? 8 : 6).slice(2); + color = this.faker.datatype.hexadecimal({ length: includeAlpha ? 8 : 6 }); color = formatHexColor(color, options); return color; } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 073d3f8a..5887ab09 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -69,7 +69,6 @@ export class Database { * faker.database.mongodbObjectId() // 'e175cac316a79afdd0ad3afb' */ mongodbObjectId(): string { - // strip the "0x" from the hexadecimal output - return this.faker.datatype.hexadecimal(24).replace('0x', '').toLowerCase(); + return this.faker.datatype.hexadecimal({ length: 24, case: 'lower' }); } } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 34701368..671e8c6c 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,40 @@ export class Datatype { /** * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number. * - * @param length Length of the generated number. Defaults to `1`. + * @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 `''`. + * @param options.case Case of the generated number. Defaults to `'mixed'`. * * @example - * faker.datatype.hexadecimal() // '0xb' - * faker.datatype.hexadecimal(10) // '0xaE13F044fb' + * faker.datatype.hexadecimal() // 'B' + * faker.datatype.hexadecimal({ length: 10 }) // 'aE13d044cB' + * faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE' + * faker.datatype.hexadecimal({ case: 'lower' }) // 'f' + * faker.datatype.hexadecimal({ length: 10, prefix: '0x' }) // '0xf12a974eB1' + * faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // 'E3F38014FB' + * faker.datatype.hexadecimal({ prefix: '0x', case: 'lower' }) // '0xd' + * faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1' */ - hexadecimal(length = 1): string { + hexadecimal( + options: + | { length?: number; prefix?: string; case?: 'lower' | 'upper' | 'mixed' } + | number = {} + ): string { + if (typeof options === 'number') { + deprecated({ + deprecated: 'faker.datatype.hexadecimal(length)', + proposed: 'faker.datatype.hexadecimal({ length })', + since: '7.5', + until: '8.0', + }); + options = { + length: options, + }; + } + + const { length = 1, prefix = '', case: letterCase = 'mixed' } = options; + let wholeString = ''; for (let i = 0; i < length; i++) { @@ -223,7 +251,13 @@ export class Datatype { ]); } - return `0x${wholeString}`; + if (letterCase === 'upper') { + wholeString = wholeString.toUpperCase(); + } else if (letterCase === 'lower') { + wholeString = wholeString.toLowerCase(); + } + + return `${prefix}${wholeString}`; } /** diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 7085ea36..f1b089e3 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -321,7 +321,11 @@ export class Finance { * faker.finance.ethereumAddress() // '0xf03dfeecbafc5147241cc4c4ca20b3c9dfd04c4a' */ ethereumAddress(): string { - const address = this.faker.datatype.hexadecimal(40).toLowerCase(); + const address = this.faker.datatype.hexadecimal({ + length: 40, + prefix: '0x', + case: 'lower', + }); return address; } |
