最近写个uni-app前端,由于要兼容小程序环境,标准几个npm里的库都不能用了。看了下网上的uuid js库,再加上short-uuid的源码。一并输入GPT敲打了几个来回后,得到这样的代码:
uuid.ts
enum UUIDFormat {
CookieBase90,
FlickrBase58,
UUID25Base36
}
const constants = {
[UUIDFormat.CookieBase90]:
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~",
[UUIDFormat.FlickrBase58]: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
[UUIDFormat.UUID25Base36]: '0123456789abcdefghijklmnopqrstuvwxyz'
}
/**
* Calculate length for the shortened ID
* @param {number} alphabetLength
* @returns {number}
*/
const getShortIdLength = (alphabetLength: number): number =>
Math.ceil(Math.log2(Math.pow(2, 128)) / Math.log2(alphabetLength))
/**
* Convert a hex string to a custom base string
* @param {string} hex
* @param {string} alphabet
* &