Mocker.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. function getRandomInt(max) {
  2. return Math.floor(Math.random() * Math.floor(max));
  3. }
  4. const testSymbol = Symbol("test");
  5. export class Mocker {
  6. constructor() {
  7. this.pick = (...args) => {
  8. return args[getRandomInt(args.length)];
  9. };
  10. }
  11. get string() {
  12. return Math.random().toString(36).substring(7);
  13. }
  14. get number() {
  15. return Math.random() * 100;
  16. }
  17. get bigint() {
  18. return BigInt(Math.floor(Math.random() * 10000));
  19. }
  20. get boolean() {
  21. return Math.random() < 0.5;
  22. }
  23. get date() {
  24. return new Date(Math.floor(Date.now() * Math.random()));
  25. }
  26. get symbol() {
  27. return testSymbol;
  28. }
  29. get null() {
  30. return null;
  31. }
  32. get undefined() {
  33. return undefined;
  34. }
  35. get stringOptional() {
  36. return this.pick(this.string, this.undefined);
  37. }
  38. get stringNullable() {
  39. return this.pick(this.string, this.null);
  40. }
  41. get numberOptional() {
  42. return this.pick(this.number, this.undefined);
  43. }
  44. get numberNullable() {
  45. return this.pick(this.number, this.null);
  46. }
  47. get booleanOptional() {
  48. return this.pick(this.boolean, this.undefined);
  49. }
  50. get booleanNullable() {
  51. return this.pick(this.boolean, this.null);
  52. }
  53. }