Skip to content

Commit 8cb6027

Browse files
ejchengShinigami92
andauthored
feat: datatype.hexadecimal signature change (#1238)
Co-authored-by: Shinigami <[email protected]>
1 parent d4cfe97 commit 8cb6027

File tree

6 files changed

+107
-22
lines changed

6 files changed

+107
-22
lines changed

src/modules/color/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class Color {
297297
let color: string | number[];
298298
let cssFunction: CSSFunction = 'rgb';
299299
if (format === 'hex') {
300-
color = this.faker.datatype.hexadecimal(includeAlpha ? 8 : 6).slice(2);
300+
color = this.faker.datatype.hexadecimal({ length: includeAlpha ? 8 : 6 });
301301
color = formatHexColor(color, options);
302302
return color;
303303
}

src/modules/database/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export class Database {
6969
* faker.database.mongodbObjectId() // 'e175cac316a79afdd0ad3afb'
7070
*/
7171
mongodbObjectId(): string {
72-
// strip the "0x" from the hexadecimal output
73-
return this.faker.datatype.hexadecimal(24).replace('0x', '').toLowerCase();
72+
return this.faker.datatype.hexadecimal({ length: 24, case: 'lower' });
7473
}
7574
}

src/modules/datatype/index.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Faker } from '../..';
22
import { FakerError } from '../../errors/faker-error';
3+
import { deprecated } from '../../internal/deprecated';
34

45
/**
56
* Module to generate various primitive values and data types.
@@ -187,13 +188,40 @@ export class Datatype {
187188
/**
188189
* Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number.
189190
*
190-
* @param length Length of the generated number. Defaults to `1`.
191+
* @param options The optional options object.
192+
* @param options.length Length of the generated number. Defaults to `1`.
193+
* @param options.prefix Prefix for the generated number. Defaults to `''`.
194+
* @param options.case Case of the generated number. Defaults to `'mixed'`.
191195
*
192196
* @example
193-
* faker.datatype.hexadecimal() // '0xb'
194-
* faker.datatype.hexadecimal(10) // '0xaE13F044fb'
197+
* faker.datatype.hexadecimal() // 'B'
198+
* faker.datatype.hexadecimal({ length: 10 }) // 'aE13d044cB'
199+
* faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE'
200+
* faker.datatype.hexadecimal({ case: 'lower' }) // 'f'
201+
* faker.datatype.hexadecimal({ length: 10, prefix: '0x' }) // '0xf12a974eB1'
202+
* faker.datatype.hexadecimal({ length: 10, case: 'upper' }) // 'E3F38014FB'
203+
* faker.datatype.hexadecimal({ prefix: '0x', case: 'lower' }) // '0xd'
204+
* faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1'
195205
*/
196-
hexadecimal(length = 1): string {
206+
hexadecimal(
207+
options:
208+
| { length?: number; prefix?: string; case?: 'lower' | 'upper' | 'mixed' }
209+
| number = {}
210+
): string {
211+
if (typeof options === 'number') {
212+
deprecated({
213+
deprecated: 'faker.datatype.hexadecimal(length)',
214+
proposed: 'faker.datatype.hexadecimal({ length })',
215+
since: '7.5',
216+
until: '8.0',
217+
});
218+
options = {
219+
length: options,
220+
};
221+
}
222+
223+
const { length = 1, prefix = '', case: letterCase = 'mixed' } = options;
224+
197225
let wholeString = '';
198226

199227
for (let i = 0; i < length; i++) {
@@ -223,7 +251,13 @@ export class Datatype {
223251
]);
224252
}
225253

226-
return `0x${wholeString}`;
254+
if (letterCase === 'upper') {
255+
wholeString = wholeString.toUpperCase();
256+
} else if (letterCase === 'lower') {
257+
wholeString = wholeString.toLowerCase();
258+
}
259+
260+
return `${prefix}${wholeString}`;
227261
}
228262

229263
/**

src/modules/finance/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,11 @@ export class Finance {
321321
* faker.finance.ethereumAddress() // '0xf03dfeecbafc5147241cc4c4ca20b3c9dfd04c4a'
322322
*/
323323
ethereumAddress(): string {
324-
const address = this.faker.datatype.hexadecimal(40).toLowerCase();
324+
const address = this.faker.datatype.hexadecimal({
325+
length: 40,
326+
prefix: '0x',
327+
case: 'lower',
328+
});
325329
return address;
326330
}
327331

test/__snapshots__/datatype.spec.ts.snap

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ exports[`datatype > 42 > float > with min and max 1`] = `-0.43`;
7070

7171
exports[`datatype > 42 > float > with min, max and precision 1`] = `-0.4261`;
7272

73-
exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"0x8"`;
73+
exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"8"`;
7474

75-
exports[`datatype > 42 > hexadecimal > with length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`;
75+
exports[`datatype > 42 > hexadecimal > with casing 1`] = `"8"`;
76+
77+
exports[`datatype > 42 > hexadecimal > with length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`;
78+
79+
exports[`datatype > 42 > hexadecimal > with length, prefix, and casing 1`] = `"0x8be4abdd39321ad7d3fe"`;
80+
81+
exports[`datatype > 42 > hexadecimal > with prefix 1`] = `"0x8"`;
7682

7783
exports[`datatype > 42 > json 1`] = `"{\\"foo\\":79654,\\"bar\\":\\"2eiXX/J/*&\\",\\"bike\\":86617,\\"a\\":60111,\\"b\\":70807,\\"name\\":\\"\\\\\\"&{dnx4!1}\\",\\"prop\\":61748}"`;
7884
@@ -180,9 +186,15 @@ exports[`datatype > 1211 > float > with min and max 1`] = `61.07`;
180186
181187
exports[`datatype > 1211 > float > with min, max and precision 1`] = `61.0658`;
182188
183-
exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"0xE"`;
189+
exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"E"`;
190+
191+
exports[`datatype > 1211 > hexadecimal > with casing 1`] = `"e"`;
192+
193+
exports[`datatype > 1211 > hexadecimal > with length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`;
184194
185-
exports[`datatype > 1211 > hexadecimal > with length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`;
195+
exports[`datatype > 1211 > hexadecimal > with length, prefix, and casing 1`] = `"0xeadb42f0e3f4a973fab0"`;
196+
197+
exports[`datatype > 1211 > hexadecimal > with prefix 1`] = `"0xE"`;
186198
187199
exports[`datatype > 1211 > json 1`] = `"{\\"foo\\":\\"Kti5-}$_/\`\\",\\"bar\\":76408,\\"bike\\":35403,\\"a\\":69406,\\"b\\":\\"l\\\\\\"h^]dnwI<\\",\\"name\\":\\"|p|5KWu3/C\\",\\"prop\\":\\"|Jh!E=x\\\\\\"RH\\"}"`;
188200
@@ -290,9 +302,15 @@ exports[`datatype > 1337 > float > with min and max 1`] = `-12.92`;
290302
291303
exports[`datatype > 1337 > float > with min, max and precision 1`] = `-12.9153`;
292304
293-
exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"0x5"`;
305+
exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"5"`;
306+
307+
exports[`datatype > 1337 > hexadecimal > with casing 1`] = `"5"`;
308+
309+
exports[`datatype > 1337 > hexadecimal > with length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`;
310+
311+
exports[`datatype > 1337 > hexadecimal > with length, prefix, and casing 1`] = `"0x5c346ba075bd57f5a62b"`;
294312
295-
exports[`datatype > 1337 > hexadecimal > with length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`;
313+
exports[`datatype > 1337 > hexadecimal > with prefix 1`] = `"0x5"`;
296314
297315
exports[`datatype > 1337 > json 1`] = `"{\\"foo\\":56052,\\"bar\\":21258,\\"bike\\":54308,\\"a\\":3397,\\"b\\":23538,\\"name\\":\\"X9@{:e=+kD\\",\\"prop\\":62850}"`;
298316

test/datatype.spec.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ describe('datatype', () => {
5959
t.itRepeated('boolean', 5);
6060

6161
t.describe('hexadecimal', (t) => {
62-
t.it('noArgs').it('with length', 42);
62+
t.it('noArgs')
63+
.it('with length', { length: 42 })
64+
.it('with prefix', { prefix: '0x' })
65+
.it('with casing', { case: 'lower' })
66+
.it('with length, prefix, and casing', {
67+
length: 20,
68+
prefix: '0x',
69+
case: 'lower',
70+
});
6371
});
6472

6573
t.it('json');
@@ -323,14 +331,36 @@ describe('datatype', () => {
323331
describe('hexadecimal', () => {
324332
it('generates single hex character when no additional argument was provided', () => {
325333
const hex = faker.datatype.hexadecimal();
326-
expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i);
327-
expect(hex.substring(2)).toHaveLength(1);
334+
expect(hex).toMatch(/^[0-9a-f]{1}$/i);
335+
expect(hex).toHaveLength(1);
336+
});
337+
338+
it('generates a random hex string with a provided length', () => {
339+
const hex = faker.datatype.hexadecimal({ length: 5 });
340+
expect(hex).toMatch(/^[0-9a-f]+$/i);
341+
expect(hex).toHaveLength(5);
328342
});
329343

330-
it('generates a random hex string', () => {
331-
const hex = faker.datatype.hexadecimal(5);
332-
expect(hex).toMatch(/^(0x)[0-9a-f]+$/i);
333-
expect(hex.substring(2)).toHaveLength(5);
344+
it('generates a hex string with a provided prefix', () => {
345+
const hex = faker.datatype.hexadecimal({ prefix: '0x' });
346+
expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
347+
expect(hex).toHaveLength(3);
348+
});
349+
350+
it('generates a hex string with a provided casing', () => {
351+
const hex = faker.datatype.hexadecimal({ case: 'lower' });
352+
expect(hex).toMatch(/^[0-9a-f]+$/i);
353+
expect(hex).toHaveLength(1);
354+
});
355+
356+
it('generates a hex string with a provided prefix, length, and casing', () => {
357+
const hex = faker.datatype.hexadecimal({
358+
prefix: '0x',
359+
length: 7,
360+
case: 'upper',
361+
});
362+
expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
363+
expect(hex.substring(2)).toHaveLength(7);
334364
});
335365
});
336366

0 commit comments

Comments
 (0)