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 🙏

© 2025 – Pkg Stats / Ryan Hefner

random-string-utils

v1.0.0

Published

A comprehensive random string generation utility library for Node.js

Readme

Random String Utils

一个功能强大的随机字符串生成工具库,适用于Node.js环境。

功能特性

  • ✅ 生成各种类型的随机字符串
  • ✅ 支持自定义字符类型和长度
  • ✅ 包含参数验证和错误处理
  • ✅ 支持中文文档
  • ✅ 轻量级,无外部依赖

安装

npm install random-string-utils

使用方法

基本使用

const RandomStringUtils = require('random-string-utils');

// 生成默认16位随机字符串
const randomStr = RandomStringUtils.generateRandomString();
console.log(randomStr); // 输出类似: "aB3#xY8!pQ9$mN2@"

// 生成指定长度的字符串
const customStr = RandomStringUtils.generateRandomString(12);
console.log(customStr); // 输出类似: "kL5#jH8*mN3@"

高级配置

// 自定义字符类型
const customStr = RandomStringUtils.generateRandomString(10, {
    includeUppercase: true,
    includeLowercase: true,
    includeNumbers: true,
    includeSpecialChars: false, // 不包含特殊符号
    customSpecialChars: '+-*/'  // 自定义特殊字符集
});

// 只包含大写字母和数字
const alphanumericStr = RandomStringUtils.generateRandomString(8, {
    includeLowercase: false,
    includeSpecialChars: false
});

专用方法

// 生成只包含字母的字符串
const alphabeticStr = RandomStringUtils.generateAlphabeticString(10);

// 生成只包含数字的字符串
const numericStr = RandomStringUtils.generateNumericString(8);

// 生成字母数字混合字符串
const alphanumericStr = RandomStringUtils.generateAlphanumericString(12);

// 生成强密钥
const strongKey = RandomStringUtils.generateStrongKey(20);

// 打乱字符串顺序
const shuffled = RandomStringUtils.shuffleString('abcdefghij');

API 文档

generateRandomString(length = 16, options = {})

生成指定长度的随机字符串。

参数:

  • length (number): 字符串长度,默认16,范围6-128
  • options (object): 配置选项
    • includeUppercase (boolean): 是否包含大写字母,默认true
    • includeLowercase (boolean): 是否包含小写字母,默认true
    • includeNumbers (boolean): 是否包含数字,默认true
    • includeSpecialChars (boolean): 是否包含特殊符号,默认true
    • customSpecialChars (string): 自定义特殊字符集,默认"!@#$%^&*()_+-=[]{}|;:,.<>?"

返回值: (string) 生成的随机字符串

generateAlphabeticString(length = 16)

生成只包含字母的随机字符串。

generateNumericString(length = 16)

生成只包含数字的随机字符串。

generateAlphanumericString(length = 16)

生成包含字母和数字的随机字符串。

generateStrongKey(length = 16)

生成用于密钥的强随机字符串。

shuffleString(str)

打乱字符串中字符的顺序。

错误处理

库会验证输入参数,并在以下情况下抛出错误:

  • 长度不在6-128范围内
  • 没有选择任何字符类型
  • 参数类型不正确
try {
    const result = RandomStringUtils.generateRandomString(5); // 长度过短
} catch (error) {
    console.error(error.message); // "长度必须是6-128之间的整数"
}

使用场景

  • 🔐 密码生成
  • 🎫 验证码生成
  • 🔑 API密钥生成
  • 🆔 唯一标识符生成
  • 🎲 随机测试数据生成

开发

运行测试

npm test

构建

无需构建步骤,直接使用源码。

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

更新日志

v1.0.0

  • 初始版本发布
  • 包含完整的随机字符串生成功能
  • 支持自定义配置和错误处理