diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index d64b7750545..faa898d4c97 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -297,7 +297,10 @@ export class Color { let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { - color = this.faker.datatype.hexadecimal({ length: includeAlpha ? 8 : 6 }); + color = this.faker.datatype.hexadecimal({ + length: includeAlpha ? 8 : 6, + prefix: '', + }); color = formatHexColor(color, options); return color; } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 5887ab097e5..69e073159f0 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -69,6 +69,10 @@ export class Database { * faker.database.mongodbObjectId() // 'e175cac316a79afdd0ad3afb' */ mongodbObjectId(): string { - return this.faker.datatype.hexadecimal({ length: 24, case: 'lower' }); + return this.faker.datatype.hexadecimal({ + length: 24, + case: 'lower', + prefix: '', + }); } } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 671e8c6c53d..57399450870 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -190,17 +190,17 @@ export class Datatype { * * @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.prefix Prefix for the generated number. Defaults to `'0x'`. * @param options.case Case of the generated number. Defaults to `'mixed'`. * * @example - * faker.datatype.hexadecimal() // 'B' - * faker.datatype.hexadecimal({ length: 10 }) // 'aE13d044cB' + * faker.datatype.hexadecimal() // '0xB' + * faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB' * 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: '#' }) // '#f12a974eB1' + * faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB' + * faker.datatype.hexadecimal({ prefix: '', case: 'lower' }) // 'd' * faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1' */ hexadecimal( @@ -220,7 +220,7 @@ export class Datatype { }; } - const { length = 1, prefix = '', case: letterCase = 'mixed' } = options; + const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options; let wholeString = ''; diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index f1b089e320c..c6f799764a5 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -323,7 +323,6 @@ export class Finance { ethereumAddress(): string { const address = this.faker.datatype.hexadecimal({ length: 40, - prefix: '0x', case: 'lower', }); return address; diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 215962731d0..be8d02f172e 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -92,7 +92,11 @@ export class Git { * faker.git.commitSha() // '2c6e3880fd94ddb7ef72d34e683cdc0c47bec6e6' */ commitSha(): string { - return this.faker.datatype.hexadecimal({ length: 40, case: 'lower' }); + return this.faker.datatype.hexadecimal({ + length: 40, + case: 'lower', + prefix: '', + }); } /** @@ -102,6 +106,10 @@ export class Git { * faker.git.shortSha() // '6155732' */ shortSha(): string { - return this.faker.datatype.hexadecimal({ length: 7, case: 'lower' }); + return this.faker.datatype.hexadecimal({ + length: 7, + case: 'lower', + prefix: '', + }); } } diff --git a/test/__snapshots__/datatype.spec.ts.snap b/test/__snapshots__/datatype.spec.ts.snap index 97f2d4c64e1..7980ec5c2d3 100644 --- a/test/__snapshots__/datatype.spec.ts.snap +++ b/test/__snapshots__/datatype.spec.ts.snap @@ -70,11 +70,13 @@ exports[`datatype > 42 > float > with min and max 1`] = `-0.43`; exports[`datatype > 42 > float > with min, max and precision 1`] = `-0.4261`; -exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"8"`; +exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"0x8"`; -exports[`datatype > 42 > hexadecimal > with casing 1`] = `"8"`; +exports[`datatype > 42 > hexadecimal > with casing 1`] = `"0x8"`; -exports[`datatype > 42 > hexadecimal > with length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; +exports[`datatype > 42 > hexadecimal > with length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; + +exports[`datatype > 42 > hexadecimal > with length and casing 1`] = `"0x8be4abdd39321ad7d3fe"`; exports[`datatype > 42 > hexadecimal > with length, prefix, and casing 1`] = `"0x8be4abdd39321ad7d3fe"`; @@ -186,11 +188,13 @@ exports[`datatype > 1211 > float > with min and max 1`] = `61.07`; exports[`datatype > 1211 > float > with min, max and precision 1`] = `61.0658`; -exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"E"`; +exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"0xE"`; + +exports[`datatype > 1211 > hexadecimal > with casing 1`] = `"0xe"`; -exports[`datatype > 1211 > hexadecimal > with casing 1`] = `"e"`; +exports[`datatype > 1211 > hexadecimal > with length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; -exports[`datatype > 1211 > hexadecimal > with length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; +exports[`datatype > 1211 > hexadecimal > with length and casing 1`] = `"0xeadb42f0e3f4a973fab0"`; exports[`datatype > 1211 > hexadecimal > with length, prefix, and casing 1`] = `"0xeadb42f0e3f4a973fab0"`; @@ -302,11 +306,13 @@ exports[`datatype > 1337 > float > with min and max 1`] = `-12.92`; exports[`datatype > 1337 > float > with min, max and precision 1`] = `-12.9153`; -exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"5"`; +exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"0x5"`; + +exports[`datatype > 1337 > hexadecimal > with casing 1`] = `"0x5"`; -exports[`datatype > 1337 > hexadecimal > with casing 1`] = `"5"`; +exports[`datatype > 1337 > hexadecimal > with length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; -exports[`datatype > 1337 > hexadecimal > with length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; +exports[`datatype > 1337 > hexadecimal > with length and casing 1`] = `"0x5c346ba075bd57f5a62b"`; exports[`datatype > 1337 > hexadecimal > with length, prefix, and casing 1`] = `"0x5c346ba075bd57f5a62b"`; diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts index a57139fdd0c..25847e07415 100644 --- a/test/datatype.spec.ts +++ b/test/datatype.spec.ts @@ -331,14 +331,8 @@ describe('datatype', () => { describe('hexadecimal', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.datatype.hexadecimal(); - expect(hex).toMatch(/^[0-9a-f]{1}$/i); - expect(hex).toHaveLength(1); - }); - - it('generates a random hex string with a provided length', () => { - const hex = faker.datatype.hexadecimal({ length: 5 }); - expect(hex).toMatch(/^[0-9a-f]+$/i); - expect(hex).toHaveLength(5); + expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i); + expect(hex.substring(2)).toHaveLength(1); }); it('generates a hex string with a provided prefix', () => { @@ -347,10 +341,16 @@ describe('datatype', () => { expect(hex).toHaveLength(3); }); + it('generates a random hex string with a provided length', () => { + const hex = faker.datatype.hexadecimal({ length: 5 }); + expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); + expect(hex.substring(2)).toHaveLength(5); + }); + it('generates a hex string with a provided casing', () => { const hex = faker.datatype.hexadecimal({ case: 'lower' }); - expect(hex).toMatch(/^[0-9a-f]+$/i); - expect(hex).toHaveLength(1); + expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); + expect(hex.substring(2)).toHaveLength(1); }); it('generates a hex string with a provided prefix, length, and casing', () => {