jiti-meet/react/features/base/util/randomUtil.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

/* @flow */
2015-11-04 14:39:56 +00:00
/**
2016-12-09 01:20:37 +00:00
* Alphanumeric characters.
2015-11-04 14:39:56 +00:00
* @const
*/
2016-12-09 01:20:37 +00:00
const ALPHANUM
= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2015-11-03 19:21:25 +00:00
2015-11-06 00:51:23 +00:00
/**
2016-12-09 01:20:37 +00:00
* Hexadecimal digit characters.
2015-11-06 00:51:23 +00:00
* @const
*/
2016-12-09 01:20:37 +00:00
const HEX_DIGITS = '0123456789abcdef';
2015-11-06 00:51:23 +00:00
2015-11-03 19:21:25 +00:00
/**
2016-12-09 01:20:37 +00:00
* Generate a string with random alphanumeric characters with a specific length.
*
* @param {number} length - The length of the string to return.
* @returns {string} A string of random alphanumeric characters with the
* specified length.
2015-11-04 14:39:56 +00:00
*/
export function randomAlphanumString(length: number) {
2016-12-09 01:20:37 +00:00
return _randomString(length, ALPHANUM);
2015-11-04 14:39:56 +00:00
}
/**
2016-12-09 01:20:37 +00:00
* Get random element of array or string.
*
* @param {Array|string} arr - Source.
* @returns {Array|string} Array element or string character.
2015-11-04 14:39:56 +00:00
*/
2017-03-07 21:09:04 +00:00
export function randomElement(arr: Array<*> | string) {
2016-12-09 01:20:37 +00:00
return arr[randomInt(0, arr.length - 1)];
2015-11-04 14:39:56 +00:00
}
/**
2016-12-09 01:20:37 +00:00
* Returns a random hex digit.
*
* @returns {Array|string}
2015-11-04 14:39:56 +00:00
*/
2016-12-09 01:20:37 +00:00
export function randomHexDigit() {
return randomElement(HEX_DIGITS);
}
2015-11-04 14:39:56 +00:00
2016-12-09 01:20:37 +00:00
/**
* Generates a string of random hexadecimal digits with a specific length.
*
* @param {number} length - The length of the string to return.
* @returns {string} A string of random hexadecimal digits with the specified
* length.
*/
export function randomHexString(length: number) {
2016-12-09 01:20:37 +00:00
return _randomString(length, HEX_DIGITS);
}
2015-11-04 14:39:56 +00:00
2016-12-09 01:20:37 +00:00
/**
* Generates random int within the range [min, max].
*
* @param {number} min - The minimum value for the generated number.
* @param {number} max - The maximum value for the generated number.
* @returns {number} Random int number.
*/
export function randomInt(min: number, max: number) {
2016-12-09 01:20:37 +00:00
return Math.floor(Math.random() * (max - min + 1)) + min;
2015-11-04 14:39:56 +00:00
}
2015-11-03 19:21:25 +00:00
/**
2016-12-09 01:20:37 +00:00
* Generates a string of random characters with a specific length.
*
* @param {number} length - The length of the string to return.
* @param {string} characters - The characters from which the returned string is
* to be constructed.
* @private
2016-12-09 01:20:37 +00:00
* @returns {string} A string of random characters with the specified length.
2015-11-03 19:21:25 +00:00
*/
2016-12-09 01:20:37 +00:00
function _randomString(length, characters) {
let result = '';
2015-11-03 19:21:25 +00:00
2016-12-09 01:20:37 +00:00
for (let i = 0; i < length; ++i) {
result += randomElement(characters);
}
return result;
}