maybe-random-string
v1.0.0
Published
Returns a random or pseudorandom string
Downloads
2,792
Readme
maybe-random-string
Returns a random or pseudorandom string.
npm i --save maybe-random-stringUsage
Generate a cryptographically random string:
import { maybeRandomString } from "maybe-random-string";
console.log(maybeRandomString()); // Prints e.g. "FPT2KAOyy19aVihGghr5m".Generate a pseudorandom string in the same format, using package seedrandom:
import { maybeRandomString } from "maybe-random-string";
import seedrandom from "seedrandom";
const prng = seedrandom("42");
console.log(maybeRandomString({ prng })); // Prints "1hzgoFbbyPobjILzNNxbd" every time.You can change the string's length and character set with the options object:
console.log(maybeRandomString({ length: 8, chars: "abcd" })); // Prints e.g. "cdabdbdc".The defaults are similar to nanoid: length 21, alphanumeric chars, as much entropy as a UUIDv4.
Environments
This package is designed to work in both the browser and Node.js, including Node.js versions that predate Node's Web Crypto support. If you run into environment- or bundler-specific errors, please file an issue.
- Node.js build ("main" in package.json) uses
require("crypto")and a CommonJS module. - Browser build ("browser" and "module" in package.json) uses
globalThis.cryptoand an ESM module. - Node.js ESM build? Sorry, use the CommonJS build. You should still be able to write
import { maybeRandomString } from "maybe-random-string";in a Node ESM project with Node v14.13+. (TypeScript and Node ESM fight over.jsvs.mjsextensions; I'm not qualified to mediate.)
