Skip to content

Commit

Permalink
Fix prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pladreyt committed Jan 3, 2023
1 parent 0fa53ee commit 28d4c8a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
28 changes: 14 additions & 14 deletions src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ export class StringModule {
*
* @param options The optional options object.
* @param options.length The number or range of characters to generate after the prefix. Defaults to `1`.
* @param options.prefix Prefix for the generated number. Defaults to `'0x'`.
* @param options.prefix Prefix for the generated number. Defaults to `'0b'`.
*
* @example
* faker.string.binary() // '0x1'
* faker.string.binary({ length: 10 }) // '0x1101011011'
* faker.string.binary({ length: { min: 5, max: 10 } }) // '0x11101011'
* faker.string.binary({ prefix: '0x' }) // '0x1'
* faker.string.binary() // '0b1'
* faker.string.binary({ length: 10 }) // '0b1101011011'
* faker.string.binary({ length: { min: 5, max: 10 } }) // '0b11101011'
* faker.string.binary({ prefix: '0b' }) // '0b1'
* faker.string.binary({ length: 10, prefix: '#' }) // '#1101011011'
* faker.string.binary({ prefix: '' }) // '1'
* faker.string.binary({ length: 10, prefix: '0x' }) // '0x1101011011'
* faker.string.binary({ length: 10, prefix: '0b' }) // '0b1101011011'
*
* @since 8.0.0
*/
Expand All @@ -265,7 +265,7 @@ export class StringModule {
prefix?: string;
} = {}
): string {
const { prefix = '0x' } = options;
const { prefix = '0b' } = options;
const length = this.faker.helpers.rangeToNumber(options.length ?? 1);
if (length <= 0) {
return prefix;
Expand All @@ -285,16 +285,16 @@ export class StringModule {
*
* @param options The optional options object.
* @param options.length The number or range of characters to generate after the prefix. Defaults to `1`.
* @param options.prefix Prefix for the generated number. Defaults to `'0x'`.
* @param options.prefix Prefix for the generated number. Defaults to `'0o'`.
*
* @example
* faker.string.octal() // '0x3'
* faker.string.octal({ length: 10 }) // '0x1526216210'
* faker.string.octal({ length: { min: 5, max: 10 } }) // '0x15263214'
* faker.string.octal({ prefix: '0x' }) // '0x7'
* faker.string.octal() // '0o3'
* faker.string.octal({ length: 10 }) // '0o1526216210'
* faker.string.octal({ length: { min: 5, max: 10 } }) // '0o15263214'
* faker.string.octal({ prefix: '0o' }) // '0o7'
* faker.string.octal({ length: 10, prefix: '#' }) // '#1542153414'
* faker.string.octal({ prefix: '' }) // '2'
* faker.string.octal({ length: 10, prefix: '0x' }) // '0x1526216210'
* faker.string.octal({ length: 10, prefix: '0o' }) // '0o1526216210'
*
* @since 8.0.0
*/
Expand All @@ -304,7 +304,7 @@ export class StringModule {
prefix?: string;
} = {}
): string {
const { prefix = '0x' } = options;
const { prefix = '0o' } = options;
const length = this.faker.helpers.rangeToNumber(options.length ?? 1);
if (length <= 0) {
return prefix;
Expand Down
36 changes: 18 additions & 18 deletions test/__snapshots__/string.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ exports[`string > 42 > alphanumeric > with length range 1`] = `"NWbJMBB9r963sR"`

exports[`string > 42 > alphanumeric > with length, casing and exclude 1`] = `"cvy4kvh"`;

exports[`string > 42 > binary > noArgs 1`] = `"0x0"`;
exports[`string > 42 > binary > noArgs 1`] = `"0b0"`;

exports[`string > 42 > binary > with custom prefix 1`] = `"bin_0"`;

exports[`string > 42 > binary > with length 1`] = `"0x011011"`;
exports[`string > 42 > binary > with length 1`] = `"0b011011"`;

exports[`string > 42 > binary > with length and empty prefix 1`] = `"0110111"`;

exports[`string > 42 > binary > with length range 1`] = `"0x11011110000001"`;
exports[`string > 42 > binary > with length range 1`] = `"0b11011110000001"`;

exports[`string > 42 > hexadecimal > noArgs 1`] = `"0x8"`;

Expand Down Expand Up @@ -100,15 +100,15 @@ exports[`string > 42 > numeric > with length range 1`] = `"79177551410048"`;

exports[`string > 42 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6890887"`;

exports[`string > 42 > octal > noArgs 1`] = `"0x2"`;
exports[`string > 42 > octal > noArgs 1`] = `"0o2"`;

exports[`string > 42 > octal > with custom prefix 1`] = `"oct_2"`;

exports[`string > 42 > octal > with length 1`] = `"0x267156"`;
exports[`string > 42 > octal > with length 1`] = `"0o267156"`;

exports[`string > 42 > octal > with length and empty prefix 1`] = `"2671564"`;

exports[`string > 42 > octal > with length range 1`] = `"0x67156441310036"`;
exports[`string > 42 > octal > with length range 1`] = `"0o67156441310036"`;

exports[`string > 42 > sample > noArgs 1`] = `"Cky2eiXX/J"`;

Expand Down Expand Up @@ -200,15 +200,15 @@ exports[`string > 1211 > alphanumeric > with length range 1`] = `"sTMd8Z2F9GdLql
exports[`string > 1211 > alphanumeric > with length, casing and exclude 1`] = `"yexv53z"`;
exports[`string > 1211 > binary > noArgs 1`] = `"0x1"`;
exports[`string > 1211 > binary > noArgs 1`] = `"0b1"`;
exports[`string > 1211 > binary > with custom prefix 1`] = `"bin_1"`;
exports[`string > 1211 > binary > with length 1`] = `"0x101100"`;
exports[`string > 1211 > binary > with length 1`] = `"0b101100"`;
exports[`string > 1211 > binary > with length and empty prefix 1`] = `"1011001"`;
exports[`string > 1211 > binary > with length range 1`] = `"0x01100101010100011101"`;
exports[`string > 1211 > binary > with length range 1`] = `"0b01100101010100011101"`;
exports[`string > 1211 > hexadecimal > noArgs 1`] = `"0xE"`;
Expand Down Expand Up @@ -248,15 +248,15 @@ exports[`string > 1211 > numeric > with length range 1`] = `"4872190616274316780
exports[`string > 1211 > numeric > with length, allowLeadingZeros and exclude 1`] = `"9798609"`;
exports[`string > 1211 > octal > noArgs 1`] = `"0x7"`;
exports[`string > 1211 > octal > noArgs 1`] = `"0o7"`;
exports[`string > 1211 > octal > with custom prefix 1`] = `"oct_7"`;
exports[`string > 1211 > octal > with length 1`] = `"0x737611"`;
exports[`string > 1211 > octal > with length 1`] = `"0o737611"`;
exports[`string > 1211 > octal > with length and empty prefix 1`] = `"7376117"`;
exports[`string > 1211 > octal > with length range 1`] = `"0x37611705151632155606"`;
exports[`string > 1211 > octal > with length range 1`] = `"0o37611705151632155606"`;
exports[`string > 1211 > sample > noArgs 1`] = `"wKti5-}$_/"`;
Expand Down Expand Up @@ -348,15 +348,15 @@ exports[`string > 1337 > alphanumeric > with length range 1`] = `"y9dhxs2jewAg"`
exports[`string > 1337 > alphanumeric > with length, casing and exclude 1`] = `"ag45age"`;
exports[`string > 1337 > binary > noArgs 1`] = `"0x0"`;
exports[`string > 1337 > binary > noArgs 1`] = `"0b0"`;
exports[`string > 1337 > binary > with custom prefix 1`] = `"bin_0"`;
exports[`string > 1337 > binary > with length 1`] = `"0x010001"`;
exports[`string > 1337 > binary > with length 1`] = `"0b010001"`;
exports[`string > 1337 > binary > with length and empty prefix 1`] = `"0100010"`;
exports[`string > 1337 > binary > with length range 1`] = `"0x100010000110"`;
exports[`string > 1337 > binary > with length range 1`] = `"0b100010000110"`;
exports[`string > 1337 > hexadecimal > noArgs 1`] = `"0x5"`;
Expand Down Expand Up @@ -396,15 +396,15 @@ exports[`string > 1337 > numeric > with length range 1`] = `"512254032552"`;
exports[`string > 1337 > numeric > with length, allowLeadingZeros and exclude 1`] = `"6706677"`;
exports[`string > 1337 > octal > noArgs 1`] = `"0x2"`;
exports[`string > 1337 > octal > noArgs 1`] = `"0o2"`;
exports[`string > 1337 > octal > with custom prefix 1`] = `"oct_2"`;
exports[`string > 1337 > octal > with length 1`] = `"0x241124"`;
exports[`string > 1337 > octal > with length 1`] = `"0o241124"`;
exports[`string > 1337 > octal > with length and empty prefix 1`] = `"2411243"`;
exports[`string > 1337 > octal > with length range 1`] = `"0x411243021442"`;
exports[`string > 1337 > octal > with length range 1`] = `"0o411243021442"`;
exports[`string > 1337 > sample > noArgs 1`] = `"9U/4:SK$>6"`;
Expand Down
8 changes: 4 additions & 4 deletions test/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ describe('string', () => {
describe(`binary`, () => {
it('generates a single binary character when no additional argument was provided', () => {
const binary = faker.string.binary();
expect(binary).toMatch(/^0x[01]$/i);
expect(binary).toMatch(/^0b[01]$/i);
expect(binary).toHaveLength(3);
});

Expand All @@ -387,7 +387,7 @@ describe('string', () => {
(length) => {
const binary = faker.string.binary({ length });

expect(binary).toBe('0x');
expect(binary).toBe('0b');
}
);

Expand All @@ -408,7 +408,7 @@ describe('string', () => {
describe(`octal`, () => {
it('generates single octal character when no additional argument was provided', () => {
const octal = faker.string.octal();
expect(octal).toMatch(/^0x[0-7]$/i);
expect(octal).toMatch(/^0o[0-7]$/i);
expect(octal).toHaveLength(3);
});

Expand All @@ -426,7 +426,7 @@ describe('string', () => {
(length) => {
const octal = faker.string.octal({ length });

expect(octal).toBe('0x');
expect(octal).toBe('0o');
}
);

Expand Down

0 comments on commit 28d4c8a

Please sign in to comment.