1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-11 09:45:24 +01:00

fix: Fix reversed base62 encoding (#1083)

This commit is contained in:
Will Pearson 2019-03-05 20:03:55 -08:00 committed by Nolan Lawson
parent f179a63255
commit 8665a18ac0
2 changed files with 10 additions and 18 deletions

View File

@ -6,8 +6,7 @@ import { padStart } from './lodash-lite'
// Unfortunately base62 ordering is not the same as JavaScript's default ASCII ordering, // Unfortunately base62 ordering is not the same as JavaScript's default ASCII ordering,
// used both for JS string comparisons as well as IndexedDB ordering. // used both for JS string comparisons as well as IndexedDB ordering.
const BASE62_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' const BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
const ASCII_ORDERING = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
const MAX_ID_LENGTH = 30 // assume that Mastodon/Pleroma IDs won't get any bigger than this const MAX_ID_LENGTH = 30 // assume that Mastodon/Pleroma IDs won't get any bigger than this
const BASE62_LOOKUP = new Map(BASE62_ALPHABET.split('').map((char, i) => ([char, i]))) const BASE62_LOOKUP = new Map(BASE62_ALPHABET.split('').map((char, i) => ([char, i])))
@ -17,14 +16,7 @@ export function zeroPad (str, toSize) {
} }
export function toPaddedBigInt (id) { export function toPaddedBigInt (id) {
let asciiOrdered = '' return zeroPad(id, MAX_ID_LENGTH)
for (let i = 0; i < id.length; i++) {
let char = id.charAt(i)
let idx = BASE62_LOOKUP.get(char)
let asciiChar = ASCII_ORDERING[idx]
asciiOrdered += asciiChar
}
return zeroPad(asciiOrdered, MAX_ID_LENGTH)
} }
export function toReversePaddedBigInt (id) { export function toReversePaddedBigInt (id) {
@ -34,7 +26,7 @@ export function toReversePaddedBigInt (id) {
let char = padded.charAt(i) let char = padded.charAt(i)
let idx = BASE62_LOOKUP.get(char) let idx = BASE62_LOOKUP.get(char)
let reverseIdx = BASE62_ALPHABET.length - 1 - idx let reverseIdx = BASE62_ALPHABET.length - 1 - idx
reversed += ASCII_ORDERING[reverseIdx] reversed += BASE62_ALPHABET[reverseIdx]
} }
return reversed return reversed
} }

View File

@ -56,10 +56,10 @@ describe('test-id-sorting.js', () => {
it('can sort base62 IDs correctly', () => { it('can sort base62 IDs correctly', () => {
let id1 = '0' let id1 = '0'
let id2 = 'a' let id2 = 'A'
let id3 = 't' let id3 = 'Z'
let id4 = 'A' let id4 = 'a'
let id5 = 'Z' let id5 = 't'
lt(toPaddedBigInt(id1), toPaddedBigInt(id2)) lt(toPaddedBigInt(id1), toPaddedBigInt(id2))
lt(toPaddedBigInt(id2), toPaddedBigInt(id3)) lt(toPaddedBigInt(id2), toPaddedBigInt(id3))
@ -99,17 +99,17 @@ describe('test-id-sorting.js', () => {
let ids = [ let ids = [
'9gP7cpqqJWyp93GxRw', '9gP7cpqqJWyp93GxRw',
'9gP7p4Ng7RdTgOSsro', '9gP7p4Ng7RdTgOSsro',
'9gP8eQQFvdZgoQ9tw0',
'9gP8XTjVDpsT3Iqgb2', '9gP8XTjVDpsT3Iqgb2',
'9gP8eQQFvdZgoQ9tw0',
'9gP99enEY6IAMJnaXA', '9gP99enEY6IAMJnaXA',
'9gP9WIcp8QCIGbj6ES', '9gP9WIcp8QCIGbj6ES',
'9gPA897muEuxo0FxCa', '9gPA897muEuxo0FxCa',
'9gPAG1uvaSBblj05Y0',
'9gPAaSqTB8Rv4nev0C', '9gPAaSqTB8Rv4nev0C',
'9gPAhfTCdeRCG5D9IO', '9gPAhfTCdeRCG5D9IO',
'9gPAG1uvaSBblj05Y0',
'9gPBatpwvN76kABf7Y',
'9gPBA9SYjPFVNUUZTU', '9gPBA9SYjPFVNUUZTU',
'9gPBOzteZJZO3wFCQy', '9gPBOzteZJZO3wFCQy',
'9gPBatpwvN76kABf7Y',
'9gPC7jAtaS1vEQdcnY', '9gPC7jAtaS1vEQdcnY',
'9gPC9Ps4KQMLwRdZWy', '9gPC9Ps4KQMLwRdZWy',
'9gPCF0G8SvCKFHYg52', '9gPCF0G8SvCKFHYg52',