Utils.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { Chance } from 'chance';
  2. import { JSFuck } from './enums/JSFuck';
  3. export class Utils {
  4. /**
  5. * @type {Chance.Chance}
  6. */
  7. private static randomGenerator: Chance.Chance = new Chance();
  8. /**
  9. * @param array
  10. * @param searchElement
  11. * @returns {boolean}
  12. */
  13. public static arrayContains (array: any[], searchElement: any): boolean {
  14. return array.indexOf(searchElement) >= 0;
  15. }
  16. /**
  17. * @param array
  18. * @param times
  19. * @returns {T[]}
  20. */
  21. public static arrayRotate <T> (array: T[], times: number): T[] {
  22. if (!array.length) {
  23. throw new ReferenceError(`Cannot rotate empty array.`);
  24. }
  25. if (times <= 0) {
  26. return array;
  27. }
  28. let newArray: T[] = array,
  29. temp: T | undefined;
  30. while (times--) {
  31. temp = newArray.pop()!;
  32. newArray.unshift(temp);
  33. }
  34. return newArray;
  35. }
  36. /**
  37. * @param string
  38. * @param encode
  39. */
  40. public static btoa (string: string, encode: boolean = true): string {
  41. return new Buffer(
  42. encode ? encodeURI(string) : string
  43. ).toString('base64');
  44. }
  45. /**
  46. * @param dec
  47. * @returns {string}
  48. */
  49. public static decToHex (dec: number): string {
  50. const radix: number = 16;
  51. return Number(dec).toString(radix);
  52. }
  53. /**
  54. * @param url
  55. * @returns {string}
  56. */
  57. public static extractDomainFromUrl (url: string): string {
  58. let domain: string;
  59. if (url.indexOf('://') > -1 || url.indexOf('//') === 0) {
  60. domain = url.split('/')[2];
  61. } else {
  62. domain = url.split('/')[0];
  63. }
  64. domain = domain.split(':')[0];
  65. return domain;
  66. }
  67. /**
  68. * @returns {Chance.Chance}
  69. */
  70. public static getRandomGenerator (): Chance.Chance {
  71. return Utils.randomGenerator;
  72. }
  73. /**
  74. * @param length
  75. * @returns {string}
  76. */
  77. public static getRandomVariableName (length: number = 6): string {
  78. const rangeMinInteger: number = 10000,
  79. rangeMaxInteger: number = 99999999,
  80. prefix: string = '_0x';
  81. return `${prefix}${(
  82. Utils.decToHex(
  83. Utils.getRandomGenerator().integer({
  84. min: rangeMinInteger,
  85. max: rangeMaxInteger
  86. })
  87. )
  88. ).substr(0, length)}`;
  89. }
  90. /**
  91. * @param str
  92. * @param length
  93. * @returns {string[]}
  94. */
  95. public static hideString(str: string, length: number): [string, string] {
  96. const escapeRegExp = (s: string) =>
  97. s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  98. const randomMerge = function (s1: string, s2: string): string {
  99. let i1 = -1, i2 = -1, result = '';
  100. while (i1 < s1.length || i2 < s2.length) {
  101. if (Math.random() < 0.5 && i2 < s2.length) {
  102. result += s2.charAt(++i2);
  103. } else {
  104. result += s1.charAt(++i1);
  105. }
  106. }
  107. return result;
  108. };
  109. const customPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  110. let randomString = Utils.randomGenerator.string({length: length, pool: customPool}),
  111. randomStringDiff = randomString.replace(
  112. new RegExp('[' + escapeRegExp(str) + ']', 'g'),
  113. ''),
  114. randomStringDiffArray = randomStringDiff.split('');
  115. Utils.randomGenerator.shuffle(randomStringDiffArray);
  116. randomStringDiff = randomStringDiffArray.join('');
  117. return [randomMerge(str, randomStringDiff), randomStringDiff];
  118. }
  119. /**
  120. * @param number
  121. * @returns {boolean}
  122. */
  123. public static isInteger (number: number): boolean {
  124. return number % 1 === 0;
  125. }
  126. /**
  127. * @param obj
  128. * @returns {T}
  129. */
  130. public static strEnumify <T extends {[prop: string]: ''|string}> (obj: T): T {
  131. return obj;
  132. }
  133. /**
  134. * @param string
  135. * @returns {string}
  136. */
  137. public static stringToJSFuck (string: string): string {
  138. return Array
  139. .from(string)
  140. .map((character: string): string => {
  141. return JSFuck[character] || character;
  142. })
  143. .join(' + ');
  144. }
  145. /**
  146. * @param string
  147. * @returns {string}
  148. */
  149. public static stringToUnicode (string: string): string {
  150. const radix: number = 16;
  151. let prefix: string,
  152. regexp: RegExp = new RegExp('[\x00-\x7F]'),
  153. template: string;
  154. return `'${string.replace(/[\s\S]/g, (escape: string): string => {
  155. if (regexp.test(escape)) {
  156. prefix = '\\x';
  157. template = '0'.repeat(2);
  158. } else {
  159. prefix = '\\u';
  160. template = '0'.repeat(4);
  161. }
  162. return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
  163. })}'`;
  164. }
  165. }