Utils.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @returns {Chance.Chance}
  55. */
  56. public static getRandomGenerator (): Chance.Chance {
  57. return Utils.randomGenerator;
  58. }
  59. /**
  60. * @param length
  61. * @returns any
  62. */
  63. public static getRandomVariableName (length: number = 6): string {
  64. const rangeMinInteger: number = 10000,
  65. rangeMaxInteger: number = 99999999,
  66. prefix: string = '_0x';
  67. return `${prefix}${(
  68. Utils.decToHex(
  69. Utils.getRandomGenerator().integer({
  70. min: rangeMinInteger,
  71. max: rangeMaxInteger
  72. })
  73. )
  74. ).substr(0, length)}`;
  75. }
  76. /**
  77. * @param length
  78. * @param charSet
  79. * @returns string
  80. */
  81. public static hideString(str: string, length: number): [string, string] {
  82. // from http://stackoverflow.com/a/3561711
  83. const escapeRegExp = (s: string) =>
  84. s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  85. const randomMerge = function (s1: string, s2: string): string {
  86. let i1 = -1, i2 = -1, result = '';
  87. while (i1 < s1.length || i2 < s2.length) {
  88. if (Math.random() < 0.5 && i2 < s2.length) {
  89. result += s2.charAt(++i2);
  90. } else {
  91. result += s1.charAt(++i1);
  92. }
  93. }
  94. return result;
  95. }
  96. // here we need a custom pool parameter because the default on from Change.string
  97. // can return chars that break the RegExp
  98. const customPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  99. let randomString = Utils.randomGenerator.string({length: length, pool: customPool});
  100. let randomStringDiff = randomString.replace(
  101. new RegExp('[' + escapeRegExp(str) + ']', 'g'),
  102. '');
  103. let randomStringDiffArray = randomStringDiff.split('');
  104. Utils.randomGenerator.shuffle(randomStringDiffArray);
  105. randomStringDiff = randomStringDiffArray.join('');
  106. return [randomMerge(str, randomStringDiff), randomStringDiff];
  107. }
  108. /**
  109. * @param number
  110. * @returns {boolean}
  111. */
  112. public static isInteger (number: number): boolean {
  113. return number % 1 === 0;
  114. }
  115. /**
  116. * @param obj
  117. * @returns {T}
  118. */
  119. public static strEnumify <T extends {[prop: string]: ''|string}> (obj: T): T {
  120. return obj;
  121. }
  122. /**
  123. * @param string
  124. * @returns {string}
  125. */
  126. public static stringToJSFuck (string: string): string {
  127. return Array
  128. .from(string)
  129. .map((character: string): string => {
  130. return JSFuck[character] || character;
  131. })
  132. .join(' + ');
  133. }
  134. /**
  135. * @param string
  136. * @returns {string}
  137. */
  138. public static stringToUnicode (string: string): string {
  139. const radix: number = 16;
  140. let prefix: string,
  141. regexp: RegExp = new RegExp('[\x00-\x7F]'),
  142. template: string;
  143. return `'${string.replace(/[\s\S]/g, (escape: string): string => {
  144. if (regexp.test(escape)) {
  145. prefix = '\\x';
  146. template = '0'.repeat(2);
  147. } else {
  148. prefix = '\\u';
  149. template = '0'.repeat(4);
  150. }
  151. return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
  152. })}'`;
  153. }
  154. }