Created
June 24, 2023 07:38
-
-
Save cloverink/345be82c1200128cc3ba5efbb414f748 to your computer and use it in GitHub Desktop.
Unique key gennerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dayjs from 'src/common/day'; | |
const k = 'abcdefghijklmnopqustuvwxyz0123456789'; | |
const generateBit = (word: number[], index: number) => | |
word[0] + index >= k.length | |
? k[(word[0] + index) % k.length] | |
: k[word[0] + index]; | |
const generateKey = (n: number, word: number[] = []) => { | |
const left = Math.trunc(n / k.length); | |
const right = n % k.length; | |
word.push(right); | |
if (left < k.length) { | |
if (left > 0) { | |
word.push(left); | |
} | |
const sec = generateBit(word, 1); | |
const tri = generateBit(word, 2); | |
const correct = word.reverse(); | |
return `${tri}${correct.map((c) => k[c]).join('')}${sec}`; | |
} | |
return generateKey(left, word); | |
}; | |
export const getSurveyKey = (): string => { | |
const dt = dayjs.utc().unix(); | |
const rnd = Math.floor(Math.random() * 999); | |
const n = Number(`${dt}${rnd}`); | |
return generateKey(n); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment