Skip to content

Commit

Permalink
Merge branch 'next' into chore/renovate/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 31, 2024
2 parents b667755 + 72937de commit ed104f4
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 195 deletions.
394 changes: 201 additions & 193 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/definitions/animal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export type AnimalDefinition = LocaleEntry<{
rodent: string[];
snake: string[];
type: string[];
pet_name: string[];
}>;
2 changes: 2 additions & 0 deletions src/locales/en/animal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fish from './fish';
import horse from './horse';
import insect from './insect';
import lion from './lion';
import pet_name from './pet_name';
import rabbit from './rabbit';
import rodent from './rodent';
import snake from './snake';
Expand All @@ -31,6 +32,7 @@ const animal: AnimalDefinition = {
horse,
insect,
lion,
pet_name,
rabbit,
rodent,
snake,
Expand Down
44 changes: 44 additions & 0 deletions src/locales/en/animal/pet_name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default [
'Ace',
'Archie',
'Bailey',
'Bandit',
'Bella',
'Bentley',
'Bruno',
'Buddy',
'Charlie',
'Coco',
'Cookie',
'Cooper',
'Daisy',
'Dixie',
'Finn',
'Ginger',
'Gracie',
'Gus',
'Hank',
'Jack',
'Jax',
'Joey',
'Kobe',
'Leo',
'Lola',
'Louie',
'Lucy',
'Maggie',
'Max',
'Mia',
'Milo',
'Molly',
'Murphey',
'Nala',
'Nova',
'Ollie',
'Oreo',
'Rosie',
'Scout',
'Stella',
'Teddy',
'Tuffy',
];
14 changes: 14 additions & 0 deletions src/modules/animal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,18 @@ export class AnimalModule extends ModuleBase {
type(): string {
return this.faker.helpers.arrayElement(this.faker.definitions.animal.type);
}

/**
* Returns a random pet name.
*
* @example
* faker.animal.petName() // 'Coco'
*
* @since 9.2.0
*/
petName(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.animal.pet_name
);
}
}
91 changes: 91 additions & 0 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,95 @@ export class NumberModule extends SimpleModuleBase {

return min + offset;
}

/**
* Returns a roman numeral in String format.
* The bounds are inclusive.
*
* @param options Maximum value or options object.
* @param options.min Lower bound for generated roman numerals. Defaults to `1`.
* @param options.max Upper bound for generated roman numerals. Defaults to `3999`.
*
* @throws When `min` is greater than `max`.
* @throws When `min`, `max` is not a number.
* @throws When `min` is less than `1`.
* @throws When `max` is greater than `3999`.
*
* @example
* faker.number.romanNumeral() // "CMXCIII"
* faker.number.romanNumeral(5) // "III"
* faker.number.romanNumeral({ min: 10 }) // "XCIX"
* faker.number.romanNumeral({ max: 20 }) // "XVII"
* faker.number.romanNumeral({ min: 5, max: 10 }) // "VII"
*
* @since 9.2.0
*/
romanNumeral(
options:
| number
| {
/**
* Lower bound for generated number.
*
* @default 1
*/
min?: number;
/**
* Upper bound for generated number.
*
* @default 3999
*/
max?: number;
} = {}
): string {
const DEFAULT_MIN = 1;
const DEFAULT_MAX = 3999;

if (typeof options === 'number') {
options = {
max: options,
};
}

const { min = DEFAULT_MIN, max = DEFAULT_MAX } = options;

if (min < DEFAULT_MIN) {
throw new FakerError(
`Min value ${min} should be ${DEFAULT_MIN} or greater.`
);
}

if (max > DEFAULT_MAX) {
throw new FakerError(
`Max value ${max} should be ${DEFAULT_MAX} or less.`
);
}

let num = this.int({ min, max });

const lookup: Array<[string, number]> = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1],
];

let result = '';

for (const [k, v] of lookup) {
result += k.repeat(Math.floor(num / v));
num %= v;
}

return result;
}
}
6 changes: 6 additions & 0 deletions test/modules/__snapshots__/animal.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ exports[`animal > 42 > insect 1`] = `"Gouty oak gall"`;

exports[`animal > 42 > lion 1`] = `"Cape lion"`;

exports[`animal > 42 > petName 1`] = `"Ginger"`;

exports[`animal > 42 > rabbit 1`] = `"English Spot"`;

exports[`animal > 42 > rodent 1`] = `"Famatina chinchilla rat"`;
Expand Down Expand Up @@ -52,6 +54,8 @@ exports[`animal > 1211 > insect 1`] = `"Western paper wasp"`;

exports[`animal > 1211 > lion 1`] = `"West African Lion"`;

exports[`animal > 1211 > petName 1`] = `"Scout"`;

exports[`animal > 1211 > rabbit 1`] = `"Silver Marten"`;

exports[`animal > 1211 > rodent 1`] = `"Strong tuco-tuco"`;
Expand Down Expand Up @@ -82,6 +86,8 @@ exports[`animal > 1337 > insect 1`] = `"Erythrina gall wasp"`;

exports[`animal > 1337 > lion 1`] = `"Barbary Lion"`;

exports[`animal > 1337 > petName 1`] = `"Cooper"`;

exports[`animal > 1337 > rabbit 1`] = `"Cinnamon"`;

exports[`animal > 1337 > rodent 1`] = `"Crested porcupine"`;
Expand Down
42 changes: 42 additions & 0 deletions test/modules/__snapshots__/number.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ exports[`number > 42 > octal > with options 1`] = `"4"`;

exports[`number > 42 > octal > with value 1`] = `"0"`;

exports[`number > 42 > romanNumeral > noArgs 1`] = `"MCDXCVIII"`;

exports[`number > 42 > romanNumeral > with max as 3999 1`] = `"MCDXCVIII"`;

exports[`number > 42 > romanNumeral > with min and max 1`] = `"CCL"`;

exports[`number > 42 > romanNumeral > with min as 1 1`] = `"MCDXCVIII"`;

exports[`number > 42 > romanNumeral > with number value 1`] = `"CCCLXXV"`;

exports[`number > 42 > romanNumeral > with only max 1`] = `"LXII"`;

exports[`number > 42 > romanNumeral > with only min 1`] = `"MDI"`;

exports[`number > 1211 > bigInt > noArgs 1`] = `982966736876848n`;

exports[`number > 1211 > bigInt > with big options 1`] = `25442250580110979794946298n`;
Expand Down Expand Up @@ -100,6 +114,20 @@ exports[`number > 1211 > octal > with options 1`] = `"12"`;

exports[`number > 1211 > octal > with value 1`] = `"1"`;

exports[`number > 1211 > romanNumeral > noArgs 1`] = `"MMMDCCXIV"`;

exports[`number > 1211 > romanNumeral > with max as 3999 1`] = `"MMMDCCXIV"`;

exports[`number > 1211 > romanNumeral > with min and max 1`] = `"CDLXXIV"`;

exports[`number > 1211 > romanNumeral > with min as 1 1`] = `"MMMDCCXIV"`;

exports[`number > 1211 > romanNumeral > with number value 1`] = `"CMXXIX"`;

exports[`number > 1211 > romanNumeral > with only max 1`] = `"CLIV"`;

exports[`number > 1211 > romanNumeral > with only min 1`] = `"MMMDCCXIV"`;

exports[`number > 1337 > bigInt > noArgs 1`] = `212435297136194n`;

exports[`number > 1337 > bigInt > with big options 1`] = `27379244885156992800029992n`;
Expand Down Expand Up @@ -149,3 +177,17 @@ exports[`number > 1337 > octal > noArgs 1`] = `"2"`;
exports[`number > 1337 > octal > with options 1`] = `"2"`;

exports[`number > 1337 > octal > with value 1`] = `"0"`;

exports[`number > 1337 > romanNumeral > noArgs 1`] = `"MXLVIII"`;

exports[`number > 1337 > romanNumeral > with max as 3999 1`] = `"MXLVIII"`;

exports[`number > 1337 > romanNumeral > with min and max 1`] = `"CCV"`;

exports[`number > 1337 > romanNumeral > with min as 1 1`] = `"MXLVIII"`;

exports[`number > 1337 > romanNumeral > with number value 1`] = `"CCLXIII"`;

exports[`number > 1337 > romanNumeral > with only max 1`] = `"XLIV"`;

exports[`number > 1337 > romanNumeral > with only min 1`] = `"MLI"`;
10 changes: 9 additions & 1 deletion test/modules/animal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('animal', () => {
'rabbit',
'rodent',
'snake',
'type'
'type',
'petName'
);
});

Expand Down Expand Up @@ -133,6 +134,13 @@ describe('animal', () => {
expect(faker.definitions.animal.type).toContain(actual);
});
});

describe('petName()', () => {
it('should return random value from pet name array', () => {
const actual = faker.animal.petName();
expect(faker.definitions.animal.pet_name).toContain(actual);
});
});
}
);
});
74 changes: 73 additions & 1 deletion test/modules/number.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import validator from 'validator';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { FakerError, SimpleFaker, faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { MERSENNE_MAX_VALUE } from '../utils/mersenne-test-utils';
Expand Down Expand Up @@ -47,6 +47,16 @@ describe('number', () => {
max: 32465761264574654845432354n,
});
});

t.describe('romanNumeral', (t) => {
t.it('noArgs')
.it('with number value', 1000)
.it('with only min', { min: 5 })
.it('with only max', { max: 165 })
.it('with min as 1', { min: 1 })
.it('with max as 3999', { max: 3999 })
.it('with min and max', { min: 100, max: 502 });
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
Expand Down Expand Up @@ -625,6 +635,68 @@ describe('number', () => {
);
});
});

describe('romanNumeral', () => {
it('should generate a Roman numeral within default range', () => {
const roman = faker.number.romanNumeral();
expect(roman).toBeTypeOf('string');
expect(roman).toMatch(/^[IVXLCDM]+$/);
});

it('should generate a Roman numeral with max value of 1000', () => {
const roman = faker.number.romanNumeral(1000);
expect(roman).toMatch(/^[IVXLCDM]+$/);
});

it.each(
Object.entries({
I: 1,
IV: 4,
IX: 9,
X: 10,
XXVII: 27,
XC: 90,
XCIX: 99,
CCLXIII: 263,
DXXXVI: 536,
DCCXIX: 719,
MDCCCLI: 1851,
MDCCCXCII: 1892,
MMCLXXXIII: 2183,
MMCMXLIII: 2943,
MMMDCCLXVI: 3766,
MMMDCCLXXIV: 3774,
MMMCMXCIX: 3999,
})
)(
'should generate a Roman numeral %s for value %d',
(expected: string, value: number) => {
const mock = vi.spyOn(faker.number, 'int');
mock.mockReturnValue(value);
const actual = faker.number.romanNumeral();
mock.mockRestore();
expect(actual).toBe(expected);
}
);

it('should throw when min value is less than 1', () => {
expect(() => {
faker.number.romanNumeral({ min: 0 });
}).toThrow(new FakerError('Min value 0 should be 1 or greater.'));
});

it('should throw when max value is greater than 3999', () => {
expect(() => {
faker.number.romanNumeral({ max: 4000 });
}).toThrow(new FakerError('Max value 4000 should be 3999 or less.'));
});

it('should throw when max value is less than min value', () => {
expect(() => {
faker.number.romanNumeral({ min: 500, max: 100 });
}).toThrow(new FakerError('Max 100 should be greater than min 500.'));
});
});
});

describe('value range tests', () => {
Expand Down

0 comments on commit ed104f4

Please sign in to comment.