Utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. class Utils {
  3. static arrayRotate(array, times, reverse = false) {
  4. if (times < 0) {
  5. return;
  6. }
  7. let newArray = array, temp;
  8. while (times--) {
  9. if (!reverse) {
  10. temp = newArray.pop();
  11. newArray.unshift(temp);
  12. }
  13. else {
  14. temp = newArray.shift();
  15. newArray.push(temp);
  16. }
  17. }
  18. return newArray;
  19. }
  20. static decToHex(dec) {
  21. return (dec + Math.pow(16, 6)).toString(16).substr(-6);
  22. }
  23. static getRandomInteger(min, max) {
  24. return Math.floor(Math.random() * (max - min + 1)) + min;
  25. }
  26. static getRandomVariableName(length = 6) {
  27. const prefix = '_0x';
  28. return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(10000, 9999999))).substr(0, length)}`;
  29. }
  30. static stringToUnicode(string) {
  31. return `'${string.replace(/[\s\S]/g, (escape) => {
  32. return `\\u${('0000' + escape.charCodeAt(0).toString(16)).slice(-4)}`;
  33. })}'`;
  34. }
  35. }
  36. exports.Utils = Utils;