Utils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import * as _ from 'lodash';
  2. import { JSFuck } from '../enums/JSFuck';
  3. export class Utils {
  4. /**
  5. * @type {string}
  6. */
  7. public static readonly hexadecimalPrefix: string = '0x';
  8. /**
  9. * @param array
  10. * @param times
  11. * @returns {T[]}
  12. */
  13. public static arrayRotate <T> (array: T[], times: number): T[] {
  14. if (!array.length) {
  15. throw new ReferenceError(`Cannot rotate empty array.`);
  16. }
  17. if (times <= 0) {
  18. return array;
  19. }
  20. const newArray: T[] = array;
  21. let temp: T | undefined;
  22. while (times--) {
  23. temp = newArray.pop()!;
  24. newArray.unshift(temp);
  25. }
  26. return newArray;
  27. }
  28. /**
  29. * @param dec
  30. * @returns {string}
  31. */
  32. public static decToHex (dec: number): string {
  33. const radix: number = 16;
  34. return Number(dec).toString(radix);
  35. }
  36. /**
  37. * @param url
  38. * @returns {string}
  39. */
  40. public static extractDomainFromUrl (url: string): string {
  41. let domain: string;
  42. if (url.indexOf('://') > -1 || url.indexOf('//') === 0) {
  43. domain = url.split('/')[2];
  44. } else {
  45. domain = url.split('/')[0];
  46. }
  47. domain = domain.split(':')[0];
  48. return domain;
  49. }
  50. /**
  51. * @param number
  52. * @returns {boolean}
  53. */
  54. public static isCeilNumber (number: number): boolean {
  55. return number % 1 === 0;
  56. }
  57. /**
  58. * @param map
  59. * @param value
  60. * @returns {T | null}
  61. */
  62. public static mapGetFirstKeyOf <T, U> (map: Map <T, U>, value: U): T | null {
  63. for (const [key, storageValue] of map) {
  64. if (_.isEqual(value, storageValue)) {
  65. return key;
  66. }
  67. }
  68. return null;
  69. }
  70. /**
  71. * @param obj
  72. * @returns {T}
  73. */
  74. public static strEnumify <T extends {[prop: string]: ''|string}> (obj: T): T {
  75. return obj;
  76. }
  77. /**
  78. * @param string
  79. * @param times
  80. * @returns {string}
  81. */
  82. public static stringRotate (string: string, times: number): string {
  83. return Utils.arrayRotate(Array.from(string), times).join('');
  84. }
  85. /**
  86. * @param string
  87. * @returns {string}
  88. */
  89. public static stringToJSFuck (string: string): string {
  90. return Array
  91. .from(string)
  92. .map((character: string): string => {
  93. return JSFuck[character] || character;
  94. })
  95. .join(' + ');
  96. }
  97. /**
  98. * @param string
  99. * @returns {string}
  100. */
  101. public static stringToUnicodeEscapeSequence (string: string): string {
  102. const radix: number = 16;
  103. const regexp: RegExp = new RegExp('[\x00-\x7F]');
  104. let prefix: string,
  105. template: string;
  106. return `${string.replace(/[\s\S]/g, (escape: string): string => {
  107. if (regexp.test(escape)) {
  108. prefix = '\\x';
  109. template = '0'.repeat(2);
  110. } else {
  111. prefix = '\\u';
  112. template = '0'.repeat(4);
  113. }
  114. return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
  115. })}`;
  116. }
  117. }