@bigin/ns-firebase
Version:
32 lines (31 loc) • 854 B
JavaScript
export const nextPushId = (function () {
const PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
let lastPushTime = 0;
const lastRandChars = [];
return function (now) {
const duplicateTime = now === lastPushTime;
lastPushTime = now;
let i;
const timeStampChars = new Array(8);
for (i = 7; i >= 0; i--) {
timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
now = Math.floor(now / 64);
}
let id = timeStampChars.join('');
if (!duplicateTime) {
for (i = 0; i < 12; i++) {
lastRandChars[i] = Math.floor(Math.random() * 64);
}
} else {
for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
lastRandChars[i] = 0;
}
lastRandChars[i]++;
}
for (i = 0; i < 12; i++) {
id += PUSH_CHARS.charAt(lastRandChars[i]);
}
return id;
};
})();
//# sourceMappingURL=NextPushId.js.map