Utils.js 1.4 KB

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