mirror of
https://github.com/lasthead0/yandex2mqtt.git
synced 2025-08-07 08:40:29 +03:00
33 lines
687 B
JavaScript
33 lines
687 B
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Return a unique identifier with the given `len`.
|
|
*
|
|
* @param {Number} length
|
|
* @return {String}
|
|
* @api private
|
|
*/
|
|
module.exports.getUid = function(length) {
|
|
let uid = '';
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
const charsLength = chars.length;
|
|
|
|
for (let i = 0; i < length; ++i) {
|
|
uid += chars[getRandomInt(0, charsLength - 1)];
|
|
}
|
|
|
|
return uid;
|
|
};
|
|
|
|
/**
|
|
* Return a random int, used by `utils.getUid()`.
|
|
*
|
|
* @param {Number} min
|
|
* @param {Number} max
|
|
* @return {Number}
|
|
* @api private
|
|
*/
|
|
function getRandomInt(min, max) {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
}
|