Utils.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Utils } from '../../src/Utils';
  2. import { JSFuck } from '../../src/enums/JSFuck';
  3. const assert: Chai.AssertStatic = require('chai').assert;
  4. describe('Utils', () => {
  5. describe('arrayContains (array: any[], searchElement: any): boolean', () => {
  6. it('should return boolean depends on condition if array is contains given value or not', () => {
  7. assert.equal(Utils.arrayContains(['1', '2', '3'], '2'), true);
  8. assert.equal(Utils.arrayContains([1, 2, 3], 2), true);
  9. assert.equal(Utils.arrayContains([1, 2, 3], 4), false);
  10. });
  11. });
  12. describe('arrayRotate <T> (array: T[], times: number): T[]', () => {
  13. let array: number[];
  14. beforeEach(() => {
  15. array = [1, 2, 3, 4, 5, 6];
  16. });
  17. it('should rotate (shift) array by a given value', () => {
  18. assert.deepEqual(Utils.arrayRotate(array, 2), [5, 6, 1, 2, 3, 4]);
  19. });
  20. it('should do nothing if value <= 0', () => {
  21. assert.deepEqual(Utils.arrayRotate(array, 0), [1, 2, 3, 4, 5, 6]);
  22. assert.deepEqual(Utils.arrayRotate(array, -1), [1, 2, 3, 4, 5, 6]);
  23. });
  24. it('should throw exception if array is empty', () => {
  25. assert.throws(() => Utils.arrayRotate([], 5), ReferenceError);
  26. });
  27. });
  28. describe('btoa (string: string): string', () => {
  29. it('should creates a base-64 encoded string from a given string', () => {
  30. assert.equal(Utils.btoa('string'), 'c3RyaW5n');
  31. });
  32. });
  33. describe('decToHex (dec: number): string', () => {
  34. it('should creates a string with hexadecimal value from a given decimal number', () => {
  35. assert.equal(Utils.decToHex(0), '0');
  36. assert.equal(Utils.decToHex(10), 'a');
  37. assert.equal(Utils.decToHex(17), '11');
  38. assert.equal(Utils.decToHex(536870912), '20000000');
  39. });
  40. });
  41. describe('extractDomainFromUrl (url: string): string', () => {
  42. it('should extract domain from the given URL', () => {
  43. assert.equal(Utils.extractDomainFromUrl('http://google.ru'), 'google.ru');
  44. assert.equal(Utils.extractDomainFromUrl('http://www.google.ru'), 'www.google.ru');
  45. assert.equal(Utils.extractDomainFromUrl('https://www.google.ru:9000'), 'www.google.ru');
  46. assert.equal(Utils.extractDomainFromUrl('//google.ru/abc'), 'google.ru');
  47. assert.equal(Utils.extractDomainFromUrl('//localhost:9000'), 'localhost');
  48. });
  49. });
  50. describe('getRandomVariableName (length: number = 6): string', () => {
  51. it('should return a string of given length with random variable name', () => {
  52. assert.match(Utils.getRandomVariableName(4), /^_0x(\w){4}$/);
  53. assert.match(Utils.getRandomVariableName(6), /^_0x(\w){4,6}$/);
  54. });
  55. });
  56. describe('isInteger (number: number): boolean', () => {
  57. it('should return true if given number or string is integer', () => {
  58. assert.equal(Utils.isInteger(4), true);
  59. assert.equal(Utils.isInteger(<any>'4'), true);
  60. assert.equal(Utils.isInteger(<any>'a'), false);
  61. });
  62. });
  63. describe('stringToJSFuck (string: string): string', () => {
  64. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  65. it('should creates a JSFuck encoded string from a given string', () => {
  66. assert.equal(Utils.stringToJSFuck('string'), expected);
  67. });
  68. });
  69. describe('stringToUnicode (string: string): string', () => {
  70. let expected: string = `'\\x73\\x74\\x72\\x69\\x6e\\x67'`;
  71. it('should return a unicode encoded string from a given string', () => {
  72. assert.equal(Utils.stringToUnicode('string'), expected);
  73. });
  74. });
  75. describe('hideString (str: string, length: number): [string, string]', () => {
  76. let original1: string = 'example.com',
  77. [str1, diff] = Utils.hideString(original1, 30);
  78. it('should return a string with the original string within', () => {
  79. assert.isTrue(str1.length > original1.length);
  80. assert.equal(str1.replace(new RegExp('[' + diff + ']', 'g'), ''), original1);
  81. });
  82. });
  83. });