RoutesMaker/Public/ecma/Managers/RandomKeysManager.ecma.js

93 lines
2.2 KiB
JavaScript

"use strict";
import {Utils} from "../Utils/Utils.ecma.js";
/**
* @class
* @constructor
* @param {?(Object.<string, any|null>|Array.<any|null>)} [inputs = null]
* @returns {void}
* @access public
* @static
*/
export const RandomKeysManager = (function(){
/**
* @constructs RandomKeysManager
* @param {?(Object.<string, any|null>|Array.<any|null>)} [inputs = null]
* @returns {void}
* @access private
* @static
*/
const RandomKeysManager = function(inputs = null){
/** @type {Array.<string>} */
const keys = [],
/** @type {string|Array.<string>} */
alphabet = Utils.get_value("alphabet", inputs, "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"),
/** @type {number} */
length = Utils.get_value("length", inputs, 11);
/**
* @returns {string}
* @access public
*/
this.get = () => {
/** @type {string} */
let key;
/** @type {number} */
const l = alphabet.length;
do{
key = "";
while((key += alphabet[Math.random() * l >> 0]).length < length);
}while(
keys.includes(key) ||
/^[^a-z_]/.test(key) ||
document.querySelector("." + key + ",#" + key + ",[name=" + key + "]")
);
keys.includes(key);
return key;
};
/**
* @param {...string} set
* @returns {void}
* @access public
*/
this.remove = (...set) => {
set.forEach(key => {
/** @type {number} */
const i = keys.indexOf(key);
i != -1 && keys.splice(i, 1);
});
};
/**
* @param {...string} set
* @returns {void}
* @access public
*/
this.add = (...set) => {
set.forEach(key => {
keys.includes(key) || keys.push(key);
});
};
/**
* @returns {void}
* @access public
*/
this.empty = () => {
keys.splice(0, keys.length);
};
};
return RandomKeysManager;
})();