MatrixAI / js-id

ID generation for JavaScript & TypeScript Applications
https://polykey.com
Apache License 2.0
10 stars 1 forks source link

Base Encoding Order and Fixing Fixed Point Bug for IdSortable #8

Closed CMCDragonkai closed 2 years ago

CMCDragonkai commented 2 years ago

Description

Issues Fixed

Tasks

  1. [x] - Made toFixedPoint check if the fractional becomes 1 and adds it to the integer
  2. [x] - Changed Math.round to Math.floor in assignment of fractionalFixed to avoid 4096 in 12 bits in toFixedPoint
  3. [x] - Added tests for checking edge conditions of close to 1 and close 0 for toFixedPoint and fromFixedPoint
  4. [x] - Added tests to check if IdSortable is producing bad ids after a long time
  5. [x] - Added tests to check if base encodings are preserving the same order, and demonstrating that base64 is not lexicographically ordered

Final checklist

CMCDragonkai commented 2 years ago

These changes were essential to fixing the IdSortable:

  // if the fractional is rounded to 1
  // then it should be added to the integer
  if (fractional === 1) {
    integer += fractional;
    fractional = 0;
  }
  // floor is used to round down to a number that can be represented by the bit size
  // if ceil or round was used, it's possible to return a number that would overflow the bit size
  // for example if 12 bits is used, then 4096 would overflow to all zeros
  // the maximum for 12 bit is 4095
  const fractionalFixed = Math.floor(fractional * (2 ** size));
  return [integer, fractionalFixed];