Mocker.js 1.4 KB

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