npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsweb/randkey

v4.1.5

Published

Simple JS module to generate random id/key/hash in various formats, including UUID v4

Downloads

77

Readme

@jsweb/randkey

Simple JS module to generate random id/key/hash in various formats, including UUID.

See tests at https://randkey.jsweb.app

package-npm module-es tests-ava


Installation

You can install it with NPM, Yarn or get from Unpkg CDN:

npm i -S @jsweb/randkey

yarn add @jsweb/randkey

pnpm add @jsweb/randkey

Unpkg CDN: https://unpkg.com/@jsweb/randkey

Usage

@jsweb/randkey is a full ES module, there is no UMD or CommonJS versions.

ES modules are the pattern in modern JS development, already natively supported by newer versions of Node.js and modern browsers.

Backward compatibility is not a concern here. If you use a module bundler (like Webpack or Rollup) to transpile your code, the result will be compatible according to your setup.

ES6

Tree shaking:

import { id16, uuid, ... } from '@jsweb/randkey'

From Unpkg CDN (installation not required)

<script type="module">
  import { uuid } from 'https://unpkg.com/@jsweb/randkey'

  const key = uuid()
</script>

Methods

rand(radix = 10, limit = 40)

Generates a random number as string with optional radix and size limit.

The radix parameter can be a number from 2 to 36. Default is 10, which generates base 10 random numbers.

The limit size parameter can be a number from 1 to 40. Default is 40, which is the bigger possible.

Keep in mind that radix can automatic limit the result max size.

import { rand } from '@jsweb/randkey'

rand()
// > '555847878175'

rand(2)
// > '1001010000010000000001111111010100000110'

rand(8)
// > '2036103777231'

rand(16)
// > '12a82628ee7'

rand(32)
// > '18vq5b2hq'

rand(36)
// > 'fdqlsnvb'

hex(limit = 11)

Generates a random hexadecimal number as string with optional custom size limit.

Size limit must be from 1 to 11. Default is 11, which is the bigger possible.

It is just an alias for rand(16, limit).

import { hex } from '@jsweb/randkey'

hex(6) // > 'f9c1d0'

id4()

Generates a random ID with 4 hexadecimal digits. It is just an alias for hex(4).

import { id4 } from '@jsweb/randkey'

id4() // > 'f9c1'

id8()

Generates a random ID with 8 hexadecimal digits. It is just an alias for hex(8).

import { id8 } from '@jsweb/randkey'

id8() // > 'bf9c61ed'

id16()

Generates a random ID with 16 hexadecimal digits.

import { id16 } from '@jsweb/randkey'

id16() // > '6c1f3ac8e0ba611d'

id32()

Generates a random ID with 32 hexadecimal digits.

import { id32 } from '@jsweb/randkey'

id32() // > 'f17e3ac8e0ba925a61ed19016c1f2eb0'

id64()

Generates a random ID with 64 hexadecimal digits.

import { id64 } from '@jsweb/randkey'

id64() // > 'f17e3ac8e0ba925a61ed19016c1f2eb0f17e3ac8e0ba925a61ed19016c1f2eb0'

uuid()

Generates a valid UUID v4 (RFC 4122 compilant).

import { uuid } from '@jsweb/randkey'

uuid() // > 'c30663ff-a2d3-4e5d-b377-9e561e8e599b'

puid()

Generates a random 5x5 ID with base2 progressive radix per block.

import { puid } from '@jsweb/randkey'

puid() // > 10100-13110-42720-98222-13prn

ruid(radix = 10)

Generates a random 5x5 ID using a common radix for all blocks using rand(radix, 5).

The radix parameter can be a number from 2 to 36. Default is 10, which generates base 10 random blocks.

import { ruid } from '@jsweb/randkey'

ruid(8) // > 15124-22432-17325-45517-15522

huid()

Generates a random 5x5 ID with hexadecimal blocks.

It is just an alias for ruid(16).

import { huid } from '@jsweb/randkey'

huid() // > d74b8-124e7-15854-15c73-82909

wuid()

Generates a random 5x5 ID with alphanumerical blocks, like Windows Product Key.

It is just an alias for ruid(36).

import { wuid } from '@jsweb/randkey'

wuid() // > 7wuiu-e4fw7-ari12-3z50r-iv04x