Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datatype): unintentional hex breaking change #1306

Merged
merged 7 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/modules/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
});
}
}
14 changes: 7 additions & 7 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -220,7 +220,7 @@ export class Datatype {
};
}

const { length = 1, prefix = '', case: letterCase = 'mixed' } = options;
import-brain marked this conversation as resolved.
Show resolved Hide resolved
const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options;

let wholeString = '';

Expand Down
1 change: 0 additions & 1 deletion src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ export class Finance {
ethereumAddress(): string {
const address = this.faker.datatype.hexadecimal({
length: 40,
prefix: '0x',
case: 'lower',
});
return address;
Expand Down
12 changes: 10 additions & 2 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
});
}

/**
Expand All @@ -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: '',
});
}
}
24 changes: 15 additions & 9 deletions test/__snapshots__/datatype.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;

Expand Down Expand Up @@ -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"`;

Expand Down Expand Up @@ -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"`;

Expand Down
20 changes: 10 additions & 10 deletions test/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down