@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
44 lines (43 loc) • 1.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.nanoid = exports.customAlphabet = exports.customRandom = exports.random = exports.urlAlphabet = void 0;
exports.urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
const random = bytes => crypto.getRandomValues(new Uint8Array(bytes));
exports.random = random;
const customRandom = (alphabet, defaultSize, getRandom) => {
const mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1;
const step = -~((1.6 * mask * defaultSize) / alphabet.length);
return (size = defaultSize) => {
let id = '';
while (true) {
const bytes = getRandom(step);
let j = step;
while (j--) {
id += alphabet[bytes[j] & mask] || '';
if (id.length === size) {
return id;
}
}
}
};
};
exports.customRandom = customRandom;
const customAlphabet = (alphabet, size = 21) => (0, exports.customRandom)(alphabet, size, exports.random);
exports.customAlphabet = customAlphabet;
const nanoid = (size = 21) => crypto.getRandomValues(new Uint8Array(size)).reduce((id, byte) => {
byte &= 63;
if (byte < 36) {
id += byte.toString(36);
}
else if (byte < 62) {
id += (byte - 26).toString(36).toUpperCase();
}
else if (byte > 62) {
id += '-';
}
else {
id += '_';
}
return id;
}, '');
exports.nanoid = nanoid;
;