diff options
| author | Shinigami92 <[email protected]> | 2022-01-10 21:34:27 +0100 |
|---|---|---|
| committer | Damien Retzinger <[email protected]> | 2022-01-14 18:37:49 -0500 |
| commit | e0d0b5cc45cfde3dd6a351650becf9ea83b99ae2 (patch) | |
| tree | 8014f68054db34065e1192ed61cd2e943ebe9bd5 /src/random.ts | |
| parent | b20f80bf7886e89045e369add9c5598d3586a225 (diff) | |
| download | faker-e0d0b5cc45cfde3dd6a351650becf9ea83b99ae2.tar.xz faker-e0d0b5cc45cfde3dd6a351650becf9ea83b99ae2.zip | |
feat: rewrite datatype to ts
Diffstat (limited to 'src/random.ts')
| -rw-r--r-- | src/random.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/random.ts b/src/random.ts new file mode 100644 index 00000000..8b4feb29 --- /dev/null +++ b/src/random.ts @@ -0,0 +1,24 @@ +import type { Faker } from '.'; + +export class Random { + constructor(private readonly faker: Faker, seed?: any[] | any) { + // Use a user provided seed if it is an array or number + if (Array.isArray(seed) && seed.length) { + this.faker.mersenne.seed_array(seed); + } else if (!isNaN(seed)) { + this.faker.mersenne.seed(seed); + } + } + + /** + * Takes an array and returns a random element of the array. + * + * @method faker.random.arrayElement + * @param array + */ + arrayElement(array) { + array = array || ['a', 'b', 'c']; + var r = this.faker.datatype.number({ max: array.length - 1 }); + return array[r]; + } +} |
