Utils.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { assert } from 'chai';
  2. import { Utils } from '../../../src/utils/Utils';
  3. import { JSFuck } from '../../../src/enums/JSFuck';
  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('decToHex (dec: number): string', () => {
  29. it('should creates a string with hexadecimal value from a given decimal number', () => {
  30. assert.equal(Utils.decToHex(0), '0');
  31. assert.equal(Utils.decToHex(10), 'a');
  32. assert.equal(Utils.decToHex(17), '11');
  33. assert.equal(Utils.decToHex(536870912), '20000000');
  34. });
  35. });
  36. describe('extractDomainFromUrl (url: string): string', () => {
  37. it('should extract domain from the given URL', () => {
  38. assert.equal(Utils.extractDomainFromUrl('http://google.ru'), 'google.ru');
  39. assert.equal(Utils.extractDomainFromUrl('http://www.google.ru'), 'www.google.ru');
  40. assert.equal(Utils.extractDomainFromUrl('https://www.google.ru:9000'), 'www.google.ru');
  41. assert.equal(Utils.extractDomainFromUrl('//google.ru/abc'), 'google.ru');
  42. assert.equal(Utils.extractDomainFromUrl('//localhost:9000'), 'localhost');
  43. });
  44. });
  45. describe('isInteger (number: number): boolean', () => {
  46. it('should return true if given number or string is integer', () => {
  47. assert.equal(Utils.isInteger(4), true);
  48. assert.equal(Utils.isInteger(<any>'4'), true);
  49. assert.equal(Utils.isInteger(<any>'a'), false);
  50. });
  51. });
  52. describe('mapGetFirstKeyOf(map: Map <any, any>, value: any): any', () => {
  53. it('should returns key of map item', () => {
  54. const map: Map <any, any> = new Map();
  55. map.set('number1', 1);
  56. map.set('number2', 2);
  57. map.set('number3', 2);
  58. map.set('string1', 'foo');
  59. map.set('string2', 'bar');
  60. map.set('object1', {item: 'value'});
  61. map.set('object2', {item: 'value'});
  62. map.set({key: 'object'}, [1, 2, 3]);
  63. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 1), 'number1');
  64. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 2), 'number2');
  65. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'foo'), 'string1');
  66. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'bar'), 'string2');
  67. assert.deepEqual(Utils.mapGetFirstKeyOf(map, {item: 'value'}), 'object1');
  68. assert.deepEqual(Utils.mapGetFirstKeyOf(map, [1, 2, 3]), {key: 'object'});
  69. assert.deepEqual(Utils.mapGetFirstKeyOf(map, 3), null);
  70. });
  71. });
  72. describe('stringToJSFuck (string: string): string', () => {
  73. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  74. it('should creates a JSFuck encoded string from a given string', () => {
  75. assert.equal(Utils.stringToJSFuck('string'), expected);
  76. });
  77. });
  78. describe('stringToUnicodeEscapeSequence (string: string): string', () => {
  79. let expected: string = '\\x73\\x74\\x72\\x69\\x6e\\x67';
  80. it('should return a unicode escape sequence based on a given string', () => {
  81. assert.equal(Utils.stringToUnicodeEscapeSequence('string'), expected);
  82. });
  83. });
  84. });