Utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. export class Utils {
  2. /**
  3. * @type {RegExp}
  4. */
  5. private static hexRepetitiveZerosRegExp: RegExp = new RegExp('^(0{2,})+(?!$)', '');
  6. /**
  7. * @param array
  8. * @param searchElement
  9. * @returns {boolean}
  10. */
  11. public static arrayContains (array: any[], searchElement: any): boolean {
  12. return array.indexOf(searchElement) >= 0;
  13. }
  14. /**
  15. * @param array
  16. * @param times
  17. * @param reverse
  18. * @returns {T[]}
  19. */
  20. public static arrayRotate <T> (array: T[], times: number, reverse: boolean = false): T[] {
  21. if (times < 0) {
  22. return;
  23. }
  24. let newArray: T[] = array,
  25. temp: T;
  26. while (times--) {
  27. if (!reverse) {
  28. temp = newArray.pop();
  29. newArray.unshift(temp);
  30. } else {
  31. temp = newArray.shift();
  32. newArray.push(temp);
  33. }
  34. }
  35. return newArray;
  36. }
  37. /**
  38. * @param dec
  39. * @returns {string}
  40. */
  41. public static decToHex(dec: number): string {
  42. const decToHexSliceValue: number = -6,
  43. exponent: number = 6,
  44. radix: number = 16;
  45. return (dec + Math.pow(radix, exponent))
  46. .toString(radix)
  47. .substr(decToHexSliceValue)
  48. .replace(Utils.hexRepetitiveZerosRegExp, '');
  49. }
  50. /**
  51. * @param min
  52. * @param max
  53. * @returns {number}
  54. */
  55. public static getRandomInteger(min: number, max: number): number {
  56. return Math.floor(Math.random() * (max - min + 1)) + min;
  57. }
  58. /**
  59. * @param length
  60. * @returns any
  61. */
  62. public static getRandomVariableName (length: number = 6): string {
  63. const rangeMinInteger: number = 10000,
  64. rangeMaxInteger: number = 99999999,
  65. prefix: string = '_0x';
  66. return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger))).substr(0, length)}`;
  67. }
  68. /**
  69. * @param number
  70. * @returns {boolean}
  71. */
  72. public static isInteger (number: number): boolean {
  73. return number % 1 === 0;
  74. }
  75. /**
  76. * @param obj
  77. * @returns {T}
  78. */
  79. public static strEnumify <T extends {[prop: string]: ''|string}> (obj: T): T {
  80. return obj;
  81. }
  82. /**
  83. * @param string
  84. * @returns {string}
  85. */
  86. public static stringToUnicode (string: string): string {
  87. const radix: number = 16,
  88. unicodeSliceValue: number = -4;
  89. let regexp: RegExp = new RegExp('[a-zA-Z]');
  90. return `'${string.replace(/[\s\S]/g, (escape: string): string => {
  91. if (regexp.test(escape)) {
  92. return '\\x' + escape.charCodeAt(0).toString(radix);
  93. }
  94. return `\\u${('0000' + escape.charCodeAt(0).toString(radix)).slice(unicodeSliceValue)}`;
  95. })}'`;
  96. }
  97. }