@cloudcome/utils-core
Version:
cloudcome core utils
47 lines (46 loc) • 1.37 kB
JavaScript
import { r as randomNumber, n as numberConvert, S as STRING_DICT } from "./string2.mjs";
import { isString, isNumber } from "./type.mjs";
let lastTimestamp = 0;
let lastSafePadding = 0;
function uniqueBigInt(randomLength = 0) {
const now = Date.now();
if (now !== lastTimestamp) {
lastTimestamp = now;
lastSafePadding = 0;
}
let randomPart = "";
if (randomLength > 0) {
const randomMin = 10 ** (randomLength - 1);
const randomMax = 10 ** randomLength - 1;
randomPart = String(randomNumber(randomMin, randomMax));
}
return BigInt(randomPart + lastSafePadding++ + now);
}
const _randomChar = (dict) => {
const poolIndex = randomNumber(0, dict.length - 1);
return dict[poolIndex];
};
function uniqueString(minLength, dict) {
let finalLength = 0;
let finalDict = STRING_DICT;
if (isString(dict)) {
finalLength = minLength;
finalDict = dict;
} else if (isNumber(minLength)) {
finalLength = minLength;
} else if (isString(minLength)) {
finalDict = minLength;
}
let uniqueString2 = numberConvert(uniqueBigInt(), finalDict);
let insertLength = finalLength - uniqueString2.length;
if (insertLength <= 0) return uniqueString2;
while (insertLength--) {
uniqueString2 += _randomChar(finalDict);
}
return uniqueString2;
}
export {
uniqueBigInt,
uniqueString
};
//# sourceMappingURL=unique.mjs.map