Utils.spec.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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('getRandomVariableName (length: number = 6): string', () => {
  42. it('should return a string of given length with random variable name', () => {
  43. assert.match(Utils.getRandomVariableName(4), /^_0x(\w){4}$/);
  44. assert.match(Utils.getRandomVariableName(6), /^_0x(\w){4,6}$/);
  45. });
  46. });
  47. describe('isInteger (number: number): boolean', () => {
  48. it('should return true if given number or string is integer', () => {
  49. assert.equal(Utils.isInteger(4), true);
  50. assert.equal(Utils.isInteger(<any>'4'), true);
  51. assert.equal(Utils.isInteger(<any>'a'), false);
  52. });
  53. });
  54. describe('stringToJSFuck (string: string): string', () => {
  55. let expected: string = `${JSFuck.s} + ${JSFuck.t} + ${JSFuck.r} + ${JSFuck.i} + ${JSFuck.n} + ${JSFuck.g}`;
  56. it('should creates a JSFuck encoded string from a given string', () => {
  57. assert.equal(Utils.stringToJSFuck('string'), expected);
  58. });
  59. });
  60. describe('stringToUnicode (string: string): string', () => {
  61. let expected: string = `'\\x73\\x74\\x72\\x69\\x6e\\x67'`;
  62. it('should return a unicode encoded string from a given string', () => {
  63. assert.equal(Utils.stringToUnicode('string'), expected);
  64. });
  65. });
  66. describe('hideString (str: string, length: number): [string, string]', () => {
  67. let original1: string = 'example.com',
  68. [str1, diff] = Utils.hideString(original1, 30);
  69. it('should return a string with the original string within', () => {
  70. assert.isTrue(str1.length > original1.length);
  71. assert.equal(str1.replace(new RegExp('[' + diff + ']', 'g'), ''), original1);
  72. });
  73. });
  74. });