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

feat: datatype.hexadecimal signature change #1238

Merged
merged 20 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c5a100b
feat: `datatype.hexadecimal` signature change
import-brain Aug 7, 2022
9212922
chore: update hex func examples with new params
import-brain Aug 7, 2022
4626e43
chore: update `datatype.hexadecimal` func calls
import-brain Aug 7, 2022
bf26ea7
test: update existing hexadecimal snapshot tests
import-brain Aug 7, 2022
e910c4e
test: add seeded tests for new params
import-brain Aug 7, 2022
ff40053
test: add unseeded tests for new params
import-brain Aug 7, 2022
f7d47f1
Merge branch 'main' into feat/hex-signature-change
import-brain Aug 7, 2022
7894387
chore: rewrite func sig using deprecation workflow
import-brain Aug 8, 2022
f93d637
feat: add `case` option
import-brain Aug 8, 2022
ef0b385
test: update hexadecimal tests for new param
import-brain Aug 8, 2022
a030ff3
fix: only cast actual number to lowercase, not prefix
import-brain Aug 8, 2022
0b177fe
test: update seeded tests with new param
import-brain Aug 8, 2022
e88494a
fix: typo in example
import-brain Aug 8, 2022
addfc25
chore: apply code review suggestions from @ST-DDT
import-brain Aug 8, 2022
ec79eaf
Update src/modules/datatype/index.ts
import-brain Aug 8, 2022
19d4038
Merge branch 'main' into feat/hex-signature-change
ST-DDT Aug 8, 2022
b1a7054
Update src/modules/datatype/index.ts
import-brain Aug 9, 2022
8e1d267
Merge branch 'main' into feat/hex-signature-change
import-brain Aug 9, 2022
24ffdd9
chore: apply code review suggestion
import-brain Aug 9, 2022
3ad814f
Merge branch 'main' into feat/hex-signature-change
Shinigami92 Aug 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/modules/color/index.ts
Expand Up @@ -297,7 +297,9 @@ 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, prefix })
.slice(prefix.length);
import-brain marked this conversation as resolved.
Show resolved Hide resolved
color = formatHexColor(color, options);
return color;
}
Expand Down
3 changes: 1 addition & 2 deletions src/modules/database/index.ts
Expand Up @@ -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 }).toLowerCase();
}
}
17 changes: 12 additions & 5 deletions src/modules/datatype/index.ts
Expand Up @@ -187,13 +187,20 @@ 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 `''`.
*
* @example
* faker.datatype.hexadecimal() // '0xb'
* faker.datatype.hexadecimal(10) // '0xaE13F044fb'
* faker.datatype.hexadecimal() // 'b'
* faker.datatype.hexadecimal({ length: 10 }) // 'aE13F044fb'
* faker.datatype.hexadecimal({ prefix: '0x' }) // '0xE'
* faker.datatype.hexadecimal({ length: 10, prefix: '0x' }) // '0xz12a974fb1'
*/
hexadecimal(length = 1): string {
hexadecimal(options?: { length?: number; prefix?: string }): string {
const length = options?.length ?? 1;
const prefix = options?.prefix ?? '';

let wholeString = '';

for (let i = 0; i < length; i++) {
Expand Down Expand Up @@ -223,7 +230,7 @@ export class Datatype {
]);
}

return `0x${wholeString}`;
return `${prefix}${wholeString}`;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/modules/finance/index.ts
Expand Up @@ -321,7 +321,9 @@ 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' })
.toLowerCase();
import-brain marked this conversation as resolved.
Show resolved Hide resolved
return address;
}

Expand Down
24 changes: 18 additions & 6 deletions test/__snapshots__/datatype.spec.ts.snap
Expand Up @@ -70,9 +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`] = `"0x8"`;
exports[`datatype > 42 > hexadecimal > noArgs 1`] = `"8"`;

exports[`datatype > 42 > hexadecimal > with length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`;
exports[`datatype > 42 > hexadecimal > with length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`;

exports[`datatype > 42 > hexadecimal > with length and prefix 1`] = `"0x8BE4ABdd39321aD7d3fe"`;

exports[`datatype > 42 > hexadecimal > with prefix 1`] = `"0x8"`;

exports[`datatype > 42 > json 1`] = `"{\\"foo\\":79654,\\"bar\\":\\"2eiXX/J/*&\\",\\"bike\\":86617,\\"a\\":60111,\\"b\\":70807,\\"name\\":\\"\\\\\\"&{dnx4!1}\\",\\"prop\\":61748}"`;

Expand Down Expand Up @@ -180,9 +184,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`] = `"0xE"`;
exports[`datatype > 1211 > hexadecimal > noArgs 1`] = `"E"`;

exports[`datatype > 1211 > hexadecimal > with length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`;

exports[`datatype > 1211 > hexadecimal > with length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`;
exports[`datatype > 1211 > hexadecimal > with length and prefix 1`] = `"0xEaDB42F0e3f4A973fAB0"`;

exports[`datatype > 1211 > hexadecimal > with prefix 1`] = `"0xE"`;

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\\"}"`;

Expand Down Expand Up @@ -290,9 +298,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`] = `"0x5"`;
exports[`datatype > 1337 > hexadecimal > noArgs 1`] = `"5"`;

exports[`datatype > 1337 > hexadecimal > with length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`;

exports[`datatype > 1337 > hexadecimal > with length and prefix 1`] = `"0x5c346ba075bd57F5A62B"`;

exports[`datatype > 1337 > hexadecimal > with length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`;
exports[`datatype > 1337 > hexadecimal > with prefix 1`] = `"0x5"`;

exports[`datatype > 1337 > json 1`] = `"{\\"foo\\":56052,\\"bar\\":21258,\\"bike\\":54308,\\"a\\":3397,\\"b\\":23538,\\"name\\":\\"X9@{:e=+kD\\",\\"prop\\":62850}"`;

Expand Down
30 changes: 24 additions & 6 deletions test/datatype.spec.ts
Expand Up @@ -59,7 +59,10 @@ describe('datatype', () => {
t.itRepeated('boolean', 5);

t.describe('hexadecimal', (t) => {
t.it('noArgs').it('with length', 42);
t.it('noArgs')
.it('with length', { length: 42 })
.it('with prefix', { prefix: '0x' })
.it('with length and prefix', { length: 20, prefix: '0x' });
});

t.it('json');
Expand Down Expand Up @@ -323,14 +326,29 @@ describe('datatype', () => {
describe('hexadecimal', () => {
it('generates single hex character when no additional argument was provided', () => {
const hex = faker.datatype.hexadecimal();
expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i);
expect(hex.substring(2)).toHaveLength(1);
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);
});

it('generates a random hex string', () => {
const hex = faker.datatype.hexadecimal(5);
it('generates a hex string with a provided prefix', () => {
const hex = faker.datatype.hexadecimal({ prefix: '0x' });
expect(hex).toMatch(/^(0x)[0-9a-f]+$/i);
expect(hex).toHaveLength(3);
});

it('generates a hex string with a provided prefix and length', () => {
const hex = faker.datatype.hexadecimal({
prefix: '0x',
length: 7,
});
expect(hex).toMatch(/^(0x)[0-9a-f]+$/i);
expect(hex.substring(2)).toHaveLength(5);
expect(hex.substring(2)).toHaveLength(7);
});
});

Expand Down