Skip to content

Commit

Permalink
feat: use random.numeric
Browse files Browse the repository at this point in the history
Co-authored-by: ST-DDT <[email protected]>
  • Loading branch information
Shinigami92 and ST-DDT committed Apr 19, 2022
1 parent 59bbc70 commit 9d6eec5
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,15 @@ export class Datatype {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
}

// TODO @Shinigami92 2022-04-07: Use faker.random.numeric() from https://github.com/faker-js/faker/pull/797
const generateRandomBigInt = (length: number) =>
BigInt(
Array.from(
{ length },
() => '0123456789'.split('')[this.number({ min: 0, max: 9 })]
).join('')
);

const delta = max - min;

const offset =
generateRandomBigInt(delta.toString(10).length) % (delta + BigInt(1));
BigInt(
this.faker.random.numeric(delta.toString(10).length, {
allowLeadingZeros: true,
})
) %
(delta + BigInt(1));

return min + offset;
}
Expand Down

0 comments on commit 9d6eec5

Please sign in to comment.