Browse Source

feat(ts) migrate modules\util\RandomUtil to TS

master
Naman Jain 4 months ago
parent
commit
5189885a9d
No account linked to committer's email address
2 changed files with 7 additions and 7 deletions
  1. 0
    0
      modules/util/MathUtil.spec.ts
  2. 7
    7
      modules/util/RandomUtil.ts

modules/util/MathUtil.spec.js → modules/util/MathUtil.spec.ts View File


modules/util/RandomUtil.js → modules/util/RandomUtil.ts View File

16
  * @param max the maximum value for the generated number
16
  * @param max the maximum value for the generated number
17
  * @returns random int number
17
  * @returns random int number
18
  */
18
  */
19
-function randomInt(min, max) {
19
+export function randomInt(min: number, max: number): number {
20
     return Math.floor(Math.random() * (max - min + 1)) + min;
20
     return Math.floor(Math.random() * (max - min + 1)) + min;
21
 }
21
 }
22
 
22
 
25
  * @param {Array|string} arr source
25
  * @param {Array|string} arr source
26
  * @returns array element or string character
26
  * @returns array element or string character
27
  */
27
  */
28
-function randomElement(arr) {
28
+export function randomElement<T>(arr: T[] | string): T | string {
29
     return arr[randomInt(0, arr.length - 1)];
29
     return arr[randomInt(0, arr.length - 1)];
30
 }
30
 }
31
 
31
 
34
  * @param {number} length expected string length
34
  * @param {number} length expected string length
35
  * @returns {string} random string of specified length
35
  * @returns {string} random string of specified length
36
  */
36
  */
37
-function randomAlphanumStr(length) {
37
+export function randomAlphanumStr(length: number): string {
38
     let result = '';
38
     let result = '';
39
 
39
 
40
     for (let i = 0; i < length; i += 1) {
40
     for (let i = 0; i < length; i += 1) {
52
      * Returns a random hex digit.
52
      * Returns a random hex digit.
53
      * @returns {*}
53
      * @returns {*}
54
      */
54
      */
55
-    randomHexDigit() {
56
-        return randomElement(HEX_DIGITS);
55
+    randomHexDigit(): string {
56
+        return randomElement(HEX_DIGITS) as string;
57
     },
57
     },
58
 
58
 
59
     /**
59
     /**
60
      * Returns a random string of hex digits with length 'len'.
60
      * Returns a random string of hex digits with length 'len'.
61
      * @param len the length.
61
      * @param len the length.
62
      */
62
      */
63
-    randomHexString(len) {
63
+    randomHexString(len: number): string {
64
         let ret = '';
64
         let ret = '';
65
 
65
 
66
         while (len--) { // eslint-disable-line no-param-reassign
66
         while (len--) { // eslint-disable-line no-param-reassign
74
     randomInt
74
     randomInt
75
 };
75
 };
76
 
76
 
77
-module.exports = RandomUtil;
77
+export default RandomUtil;

Loading…
Cancel
Save