basic.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // This file is used to store basic functions that are used in other utils
  2. // Should have no dependency on other utils
  3. import * as process from 'process'
  4. export const IsMac = process.platform === 'darwin'
  5. export const IsLinux = process.platform === 'linux'
  6. export const IsWindows = process.platform === 'win32'
  7. export const IsCI = process.env.CI === 'true'
  8. export const modKey = IsMac ? 'Meta' : 'Control'
  9. export function randomString(length: number) {
  10. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  11. let result = '';
  12. const charactersLength = characters.length;
  13. for (let i = 0; i < length; i++) {
  14. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  15. }
  16. return result;
  17. }
  18. export function randomLowerString(length: number) {
  19. const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
  20. let result = '';
  21. const charactersLength = characters.length;
  22. for (let i = 0; i < length; i++) {
  23. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  24. }
  25. return result;
  26. }
  27. export function randomInt(min: number, max: number): number {
  28. return Math.floor(Math.random() * (max - min + 1) + min)
  29. }
  30. export function randomBoolean(): boolean {
  31. return Math.random() < 0.5;
  32. }