TMeerhof / fractional-indexing-jittered

Creative Commons Zero v1.0 Universal
24 stars 1 forks source link

`generateJitteredKeyBetween` doesn't work when the first arg is `null` #2

Open miyanokomiya opened 1 day ago

miyanokomiya commented 1 day ago

Hello, thanks for the great tool.

I came across unexpected behavior of generateJitteredKeyBetween. I'd appreciate if there's any idea or help for it.

This works and returns b0U.

generateJitteredKeyBetween("b0T46n", null);

But, this doesn't work.

generateJitteredKeyBetween(null, "b0T46n");

Error logs.

Error: invalid order key length: bS
 ❯ getIntegerPart node_modules/fractional-indexing-jittered/lib/index.js:256:11
    254|   const integerPartLength = integerLength(head, charSet);
    255|   if (integerPartLength > orderKey.length) {
    256|     throw new Error("invalid order key length: " + orderKey);
       |           ^
    257|   }
    258|   return orderKey.slice(0, integerPartLength);
 ❯ paddingNeededForJitter node_modules/fractional-indexing-jittered/lib/index.js:353:19
 ❯ Module.generateJitteredKeyBetween node_modules/fractional-indexing-jittered/lib/index.js:417:25
 ❯ src/utils/findex.spec.ts:47:27

The function without jittered works and returns bS.

console.log(generateKeyBetween(null, "b0T46n"));

Thanks!

miyanokomiya commented 1 day ago

It seems tweaking this part would solve this case though, not sure how it affects other parts.

while (result.length > 2 && result[0] === charSet.byCode[0]) {

https://github.com/TMeerhof/fractional-indexing-jittered/blob/8077c3090ab497c39d89f7b478b7abcc1fd897c0/src/keyAsNumber.ts#L109