koalacodes
Version:
Random Codes Generator
75 lines (71 loc) • 2.92 kB
JavaScript
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var _VOWELS, _CONSONANTS, _DEFAULT_LENGTH, _FIRST_LETTER;
const DEFAULTS = {
vowels: ["A", "E", "I", "O", "U"],
consonants: ["B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"],
defaultLength: 8,
firstLetter: "consonant"
};
class KoalaCodes {
/**
*
* @param {Options} options Different options for constructing the code generator
*/
constructor(options) {
__privateAdd(this, _VOWELS, void 0);
__privateAdd(this, _CONSONANTS, void 0);
__privateAdd(this, _DEFAULT_LENGTH, void 0);
__privateAdd(this, _FIRST_LETTER, void 0);
__privateSet(this, _VOWELS, options?.vowels ?? DEFAULTS.vowels);
__privateSet(this, _CONSONANTS, options?.consonants ?? DEFAULTS.consonants);
__privateSet(this, _DEFAULT_LENGTH, options?.defaultLength ?? DEFAULTS.defaultLength);
__privateSet(this, _FIRST_LETTER, options?.firstLetter ?? DEFAULTS.firstLetter);
}
generateCode(length = __privateGet(this, _DEFAULT_LENGTH)) {
const LENGTH = length > 0 ? length : __privateGet(this, _DEFAULT_LENGTH);
let code = "";
for (let index = 0; index < LENGTH; index++) {
if (index % 2 == 0) {
if (__privateGet(this, _FIRST_LETTER) == "consonant") {
code += __privateGet(this, _CONSONANTS)[Math.floor(Math.random() * __privateGet(this, _CONSONANTS).length)];
} else {
code += __privateGet(this, _VOWELS)[Math.floor(Math.random() * __privateGet(this, _VOWELS).length)];
}
} else {
if (__privateGet(this, _FIRST_LETTER) == "consonant") {
code += __privateGet(this, _VOWELS)[Math.floor(Math.random() * __privateGet(this, _VOWELS).length)];
} else {
code += __privateGet(this, _CONSONANTS)[Math.floor(Math.random() * __privateGet(this, _CONSONANTS).length)];
}
}
}
return code;
}
}
_VOWELS = new WeakMap();
_CONSONANTS = new WeakMap();
_DEFAULT_LENGTH = new WeakMap();
_FIRST_LETTER = new WeakMap();
var main = KoalaCodes;
const main$1 = /*@__PURE__*/getDefaultExportFromCjs(main);
export { main$1 as default };