Converting random() from cuid to Typescript

Viewed 98

I'm trying to convert this function to Typescript. It works in the cuid library.

This is what I have so far:

import * as crypto from "crypto"

var lim = Math.pow(2, 32) - 1;

export function random () {
  return Math.abs(crypto.randomBytes(4)
    .readInt32BE() / lim);
};

Typescript says that this method is expecting an argument. The default value is 0, per the documentation.

https://www.geeksforgeeks.org/node-js-buffer-readint32be-method/

So I assume it's ok to just pass in zero?

Thus it becomes:

import * as crypto from "crypto"

var lim = Math.pow(2, 32) - 1;

export function random () {
  return Math.abs(crypto.randomBytes(4)
    .readInt32BE(0) / lim);
}

Pure Typescript Library:

In case anyone needs it: https://www.npmjs.com/package/@fireflysemantics/cuid

0 Answers
Related