Utils.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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('mapGetFirstKeyOf(map: Map <any, any>, value: any): any', () => {
  64. it('should returns key of map item', () => {
  65. const map: Map <any, any> = new Map();
  66. map.set('number1', 1);
  67. map.set('number2', 2);
  68. map.set('number3', 2);
  69. map.set('string1', 'foo');
  70. map.set('string2', 'bar');
  71. map.set('object1', {item: 'value'});
  72. map.set('object2', {item: 'value'});
  73. map.set({key: 'object'}, [1, 2, 3]);
  74. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 1), 'number1');
  75. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 2), 'number2');
  76. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'foo'), 'string1');
  77. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'bar'), 'string2');
  78. assert.deepEqual(Utils.mapGetFirstKeyOf(map, {item: 'value'}), 'object1');
  79. assert.deepEqual(Utils.mapGetFirstKeyOf(map, [1, 2, 3]), {key: 'object'});
  80. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 3), null);
  81. });
  82. });
  83. describe('stringToJSFuck (string: string): string', () => {
  84. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  85. it('should creates a JSFuck encoded string from a given string', () => {
  86. assert.equal(Utils.stringToJSFuck('string'), expected);
  87. });
  88. });
  89. describe('stringToUnicodeEscapeSequence (string: string): string', () => {
  90. let expected: string = '\\x73\\x74\\x72\\x69\\x6e\\x67';
  91. it('should return a unicode escape sequence based on a given string', () => {
  92. assert.equal(Utils.stringToUnicodeEscapeSequence('string'), expected);
  93. });
  94. });
  95. describe('hideString (str: string, length: number): [string, string]', () => {
  96. let original1: string = 'example.com',
  97. [str1, diff] = Utils.hideString(original1, 30);
  98. it('should return a string with the original string within', () => {
  99. assert.isTrue(str1.length > original1.length);
  100. assert.equal(str1.replace(new RegExp('[' + diff + ']', 'g'), ''), original1);
  101. });
  102. });
  103. });