Skip to content

Commit

Permalink
refactor(number.hex): give more of a number vibe
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Aug 18, 2022
1 parent d9ee763 commit 160f690
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 54 deletions.
49 changes: 16 additions & 33 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class NumberModule {
* Returns a single random integer between zero and the given max value or the given range.
* The bounds are inclusive.
*
* @param options Maximum value or options object.
* @param options Maximum value or options object. Defaults to `{}`.
* @param options.min Lower bound for generated number. Defaults to `0`.
* @param options.max Upper bound for generated number. Defaults to `min + 99999`.
*
Expand Down Expand Up @@ -55,7 +55,7 @@ export class NumberModule {
/**
* Returns a single random floating-point number for the given precision or range and precision.
*
* @param options Precision or options object.
* @param options Precision or options object. Defaults to `{}`.
* @param options.min Lower bound for generated number. Defaults to `0`.
* @param options.max Upper bound for generated number. Defaults to `99999`.
* @param options.precision Number of digits after the decimal point. Negative numbers will be treated as `0`. Defaults to `2`.
Expand Down Expand Up @@ -100,43 +100,26 @@ export class NumberModule {
/**
* Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) number.
*
* @param length Length of the generated number. Defaults to `1`.
* @param options Maximum value or options object. Defaults to `{}`.
* @param options.min Lower bound for generated number. Defaults to `0`.
* @param options.max Upper bound for generated number. Defaults to `min + 16`.
*
* @example
* faker.datatype.hex() // 'b'
* faker.datatype.hex(10) // 'ae13f044fb'
* faker.datatype.hex(16) // '9'
* faker.datatype.hex({ min: 0, max: 65536 }) // 'af17'
*/
hex(length = 1): string {
let hexString = '';

for (let i = 0; i < length; i++) {
hexString += this.faker.helpers.arrayElement([
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'A',
'B',
'C',
'D',
'E',
'F',
]);
hex(options: number | { min?: number; max?: number } = {}): string {
if (typeof options === 'number') {
options = { max: options };
}

return hexString;
const { min = 0, max = min + 16 } = options;

return this.int({
max,
min,
}).toString(16);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/color.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports[`color > 42 > lch 1`] = `
]
`;

exports[`color > 42 > rgb 1`] = `"#8be4ab"`;
exports[`color > 42 > rgb 1`] = `"#5fe1dc"`;

exports[`color > 42 > space 1`] = `"Rec. 709"`;

Expand Down Expand Up @@ -114,7 +114,7 @@ exports[`color > 1211 > lch 1`] = `
]
`;

exports[`color > 1211 > rgb 1`] = `"#eadb42"`;
exports[`color > 1211 > rgb 1`] = `"#edb380"`;

exports[`color > 1211 > space 1`] = `"LMS"`;

Expand Down Expand Up @@ -173,6 +173,6 @@ exports[`color > 1337 > lch 1`] = `
]
`;

exports[`color > 1337 > rgb 1`] = `"#5c346b"`;
exports[`color > 1337 > rgb 1`] = `"#43140c"`;

exports[`color > 1337 > space 1`] = `"ProPhoto RGB Color Space"`;
6 changes: 3 additions & 3 deletions test/__snapshots__/database.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`database > 42 > column 1`] = `"token"`;

exports[`database > 42 > engine 1`] = `"MEMORY"`;

exports[`database > 42 > mongodbObjectId 1`] = `"8be4abdd39321ad7d3fe01ff"`;
exports[`database > 42 > mongodbObjectId 1`] = `"9"`;

exports[`database > 42 > type 1`] = `"smallint"`;

Expand All @@ -16,7 +16,7 @@ exports[`database > 1211 > column 1`] = `"createdAt"`;

exports[`database > 1211 > engine 1`] = `"ARCHIVE"`;

exports[`database > 1211 > mongodbObjectId 1`] = `"eadb42f0e3f4a973fab0aeef"`;
exports[`database > 1211 > mongodbObjectId 1`] = `"17"`;

exports[`database > 1211 > type 1`] = `"geometry"`;

Expand All @@ -26,6 +26,6 @@ exports[`database > 1337 > column 1`] = `"email"`;

exports[`database > 1337 > engine 1`] = `"MyISAM"`;

exports[`database > 1337 > mongodbObjectId 1`] = `"5c346ba075bd57f5a62b82d7"`;
exports[`database > 1337 > mongodbObjectId 1`] = `"6"`;

exports[`database > 1337 > type 1`] = `"time"`;
6 changes: 3 additions & 3 deletions test/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports[`finance > 42 > currencyName 1`] = `"Iraqi Dinar"`;

exports[`finance > 42 > currencySymbol 1`] = `"₱"`;

exports[`finance > 42 > ethereumAddress 1`] = `"0x8be4abdd39321ad7d3fe01ffce404f4d6db0906b"`;
exports[`finance > 42 > ethereumAddress 1`] = `"0x5cf2bc99272107d592ba00fbdf302f2949806048"`;

exports[`finance > 42 > iban > noArgs 1`] = `"GT30Y75110867098F1E3542612J4"`;

Expand Down Expand Up @@ -98,7 +98,7 @@ exports[`finance > 1211 > currencyName 1`] = `"SDR"`;

exports[`finance > 1211 > currencySymbol 1`] = `"₭"`;

exports[`finance > 1211 > ethereumAddress 1`] = `"0xeadb42f0e3f4a973fab0aeefce96dfcf49cd438d"`;
exports[`finance > 1211 > ethereumAddress 1`] = `"0xe7ec32f0a2a3c652bbd0caabde64dfdf379e3259"`;

exports[`finance > 1211 > iban > noArgs 1`] = `"TN0382001124170679299069"`;

Expand Down Expand Up @@ -162,7 +162,7 @@ exports[`finance > 1337 > currencyName 1`] = `"Fiji Dollar"`;

exports[`finance > 1337 > currencySymbol 1`] = `"$"`;

exports[`finance > 1337 > ethereumAddress 1`] = `"0x5c346ba075bd57f5a62b82d72af39cbbb07a98cb"`;
exports[`finance > 1337 > ethereumAddress 1`] = `"0x48234870538945f4b41c61a52bf27dccc0576698"`;

exports[`finance > 1337 > iban > noArgs 1`] = `"FO7710540350900318"`;

Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ exports[`helpers > 1211 > arrayElements > noArgs 1`] = `

exports[`helpers > 1211 > arrayElements > with array 1`] = `
[
"r",
"d",
"o",
"l",
"!",
"l",
"d",
"W",
"H",
"r",
"W",
"e",
"o",
"l",
Expand All @@ -137,8 +137,8 @@ exports[`helpers > 1211 > arrayElements > with array 1`] = `

exports[`helpers > 1211 > arrayElements > with array 2`] = `
[
"r",
"o",
"l",
" ",
"!",
]
`;
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/number.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ exports[`number > seed: 42 > float > should return a deterministic value for giv

exports[`number > seed: 42 > float() 1`] = `37453.64`;

exports[`number > seed: 42 > hexadecimal() 1`] = `"8"`;
exports[`number > seed: 42 > hex() 1`] = `"6"`;

exports[`number > seed: 42 > int() 1`] = `37454`;

Expand Down Expand Up @@ -172,7 +172,7 @@ exports[`number > seed: 1211 > float > should return a deterministic value for g

exports[`number > seed: 1211 > float() 1`] = `92851.09`;

exports[`number > seed: 1211 > hexadecimal() 1`] = `"E"`;
exports[`number > seed: 1211 > hex() 1`] = `"f"`;

exports[`number > seed: 1211 > int() 1`] = `92852`;

Expand Down Expand Up @@ -260,6 +260,6 @@ exports[`number > seed: 1337 > float > should return a deterministic value for g

exports[`number > seed: 1337 > float() 1`] = `26202.2`;

exports[`number > seed: 1337 > hexadecimal() 1`] = `"5"`;
exports[`number > seed: 1337 > hex() 1`] = `"4"`;

exports[`number > seed: 1337 > int() 1`] = `26202`;
18 changes: 14 additions & 4 deletions test/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,28 @@ describe('number', () => {
});
});

describe('hexadecimal', () => {
describe('hex', () => {
it('generates single hex character when no additional argument was provided', () => {
const hex = faker.number.hexadecimal();
const hex = faker.number.hex();
expect(hex).toBeTypeOf('string');
expect(hex).toHaveLength(1);
expect(hex).toSatisfy(validator.isHexadecimal);
});

it('generates a random hex string', () => {
const hex = faker.number.hexadecimal(5);
expect(hex).toHaveLength(5);
const hex = faker.number.hex(5);
expect(hex).toSatisfy(validator.isHexadecimal);
});

it('generates a random hex in a specific range', () => {
const min = 15;
const max = 255;
const hex = faker.number.hex({ min, max });

const hexNum = parseInt(hex, 16);
expect(hexNum).toBeLessThanOrEqual(max);
expect(hexNum).greaterThanOrEqual(min);
});
});

describe('bigInt', () => {
Expand Down

0 comments on commit 160f690

Please sign in to comment.