richardtallent / RT.Comb

Creating sequential GUIDs in C# for MSSQL or PostgreSql
203 stars 35 forks source link

RT.Comb.Provider.Postgre.Create().ToString("N") JavaScript version. #17

Open inercya-github opened 4 years ago

inercya-github commented 4 years ago

I use RT.Comb on the back-end, however I also need it on the browser, so I wrote the following JavaScript function:

function newSequentialGuid() {
  var ms = new Date().getTime().toString(16).padStart(12, "0");
  var a = new Uint8Array(10);
  crypto.getRandomValues(a);
  a[0] = a[0] & 15 | 64;
  a[2] = a[2] & 63 | 128;
  return ms + Array.prototype.map.call(a, x => x.toString(16).padStart(2, "0")).join("");
}

Please, could you confirm it is equivalent to RT.Comb.Provider.Postgre.Create().ToString("N") ?

richardtallent commented 3 years ago

I don't use PostgreSql, so I'll have to look into this when I can get some time to run a proper test. If someone else can confirm, that would be great!

I've considered releasing a JavaScript version of this library on npm... I wrote an equivalent function for doing Unix timestamps on MSSQL GUIDs awhile back, so perhaps combining this and that and a few other things, a robust solution can be created.