Quellcode durchsuchen

move username generator to utils

j8
isymchych vor 9 Jahren
Ursprung
Commit
c89c9e78ff
3 geänderte Dateien mit 49 neuen und 49 gelöschten Zeilen
  1. 3
    2
      modules/settings/Settings.js
  2. 40
    2
      modules/util/RandomUtil.js
  3. 6
    45
      modules/util/UsernameGenerator.js

+ 3
- 2
modules/settings/Settings.js Datei anzeigen

1
-var UsernameGenerator = require('../statistics/UsernameGenerator');
1
+var UsernameGenerator = require('../util/UsernameGenerator');
2
 
2
 
3
 var email = '';
3
 var email = '';
4
 var displayName = '';
4
 var displayName = '';
31
     }
31
     }
32
     if (!window.localStorage.callStatsUID) {
32
     if (!window.localStorage.callStatsUID) {
33
         window.localStorage.callStatsUID = UsernameGenerator.generateUsername();
33
         window.localStorage.callStatsUID = UsernameGenerator.generateUsername();
34
-        console.log('generated callstats uid', window.localStorage.callStatsUID);
34
+        console.log('generated callstats uid',
35
+            window.localStorage.callStatsUID);
35
     }
36
     }
36
     userId = window.localStorage.jitsiMeetId || '';
37
     userId = window.localStorage.jitsiMeetId || '';
37
     callStatsUID = window.localStorage.callStatsUID;
38
     callStatsUID = window.localStorage.callStatsUID;

+ 40
- 2
modules/util/RandomUtil.js Datei anzeigen

1
+/**
2
+ * @const
3
+ */
4
+var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
1
 
5
 
2
 /**
6
 /**
3
- * Generates random hex number within the range [min, max]
7
+ * Generates random int within the range [min, max]
8
+ * @param min the minimum value for the generated number
4
  * @param max the maximum value for the generated number
9
  * @param max the maximum value for the generated number
10
+ * @returns random int number
11
+ */
12
+function randomInt(min, max) {
13
+    return Math.floor(Math.random() * (max - min + 1)) + min;
14
+}
15
+
16
+/**
17
+ * Get random element from array or string.
18
+ * @param {Array|string} arr source
19
+ * @returns array element or string character
20
+ */
21
+function randomElement(arr) {
22
+    return arr[randomInt(0, arr.length -1)];
23
+}
24
+
25
+/**
26
+ * Generate random alphanumeric string.
27
+ * @param {number} length expected string length
28
+ * @returns {string} random string of specified length
29
+ */
30
+function randomAlphanumStr(length) {
31
+    var result = '';
32
+
33
+    for (var i = 0; i < length; i += 1) {
34
+        result += randomElement(ALPHANUM);
35
+    }
36
+
37
+    return result;
38
+}
39
+
40
+/**
41
+ * Generates random hex number within the range [min, max]
5
  * @param min the minimum value for the generated number
42
  * @param min the minimum value for the generated number
43
+ * @param max the maximum value for the generated number
6
  * @returns random hex number
44
  * @returns random hex number
7
  */
45
  */
8
 function rangeRandomHex(min, max)
46
 function rangeRandomHex(min, max)
9
 {
47
 {
10
-    return Math.floor(Math.random() * (max - min) + min).toString(16);
48
+    return randomInt(min, max).toString(16);
11
 }
49
 }
12
 
50
 
13
 /**
51
 /**

modules/statistics/UsernameGenerator.js → modules/util/UsernameGenerator.js Datei anzeigen

1
+var RandomUtil = require('./RandomUtil');
2
+
1
 /**
3
 /**
2
- * from faker.js - Copyright (c) 2014-2015 Matthew Bergman & Marak Squires, MIT License
4
+ * from faker.js - Copyright (c) 2014-2015 Matthew Bergman & Marak Squires
5
+ * MIT License
3
  * http://github.com/marak/faker.js/
6
  * http://github.com/marak/faker.js/
4
  *
7
  *
5
  * @const
8
  * @const
410
   "Zora", "Zula"
413
   "Zora", "Zula"
411
 ];
414
 ];
412
 
415
 
413
-/**
414
- * @const
415
- */
416
-var suffixChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
417
-
418
-
419
-/**
420
- * Returns a random integer between min (inclusive) and max (inclusive).
421
- */
422
-function getRandomInt(min, max) {
423
-  return Math.floor(Math.random() * (max - min + 1)) + min;
424
-}
425
-
426
-
427
-/**
428
- * Get random element from array or string.
429
- *
430
- * @param {Array|string} arr source
431
- *
432
- * @returns array element or string character
433
- */
434
-function getRandomElement(arr) {
435
-  return arr[getRandomInt(0, arr.length -1)];
436
-}
437
-
438
-/**
439
- * Generate random alphanumeric string.
440
- *
441
- * @param {number} length expected string length
442
- *
443
- * @returns {string} random string of specified length
444
- */
445
-function generateAlphanumStr(length) {
446
-  var result = '';
447
-
448
-  for (var i = 0; i < length; i += 1) {
449
-    result += getRandomElement(suffixChars);
450
-  }
451
-
452
-  return result;
453
-}
454
-
455
 /**
416
 /**
456
  * Generate random username.
417
  * Generate random username.
457
  * @returns {string} random username
418
  * @returns {string} random username
458
  */
419
  */
459
 function generateUsername () {
420
 function generateUsername () {
460
-  var name = getRandomElement(names);
461
-  var suffix = generateAlphanumStr(3);
421
+  var name = RandomUtil.randomElement(names);
422
+  var suffix = RandomUtil.randomAlphanumStr(3);
462
 
423
 
463
   return name + '-' +  suffix;
424
   return name + '-' +  suffix;
464
 }
425
 }

Laden…
Abbrechen
Speichern