Greatly improve id conversion performance
This commit is contained in:
parent
76aa8eeefb
commit
7cb576a535
1 changed files with 4 additions and 12 deletions
|
@ -9,27 +9,19 @@ const chars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||||
//FIXME: Make this idempotent
|
//FIXME: Make this idempotent
|
||||||
export function convertId(id: string, target: IdType): string {
|
export function convertId(id: string, target: IdType): string {
|
||||||
if (target == IdType.IceshrimpId) {
|
if (target == IdType.IceshrimpId) {
|
||||||
let input = BigInt(id);
|
return BigInt(id).toString(36);
|
||||||
let result = '';
|
|
||||||
|
|
||||||
while (input !== 0n) {
|
|
||||||
result = chars.at(Number(input % 36n)) + result;
|
|
||||||
input /= 36n;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
else if (target == IdType.MastodonId) {
|
else if (target == IdType.MastodonId) {
|
||||||
let result = 0n;
|
let result = 0n;
|
||||||
const iter = id.toLowerCase().split('').reverse();
|
const iter = id.toLowerCase();
|
||||||
|
|
||||||
for (let i = 0; i < iter.length; i++){
|
for (let i = 0; i < iter.length; i++){
|
||||||
const char = iter[i];
|
const char = iter[i];
|
||||||
if (!chars.includes(char)) throw new Error('Invalid ID');
|
if (!chars.includes(char)) throw new Error('Invalid ID');
|
||||||
result += BigInt(chars.indexOf(char)) * BigInt(36 ** i);
|
result = result * 36n + BigInt(chars.indexOf(char));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
throw new Error('Unknown ID type');
|
throw new Error('Unknown ID type');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue