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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jl-utils

v1.0.2

Published

common toolkit

Readme

jl-utils

一个功能强大、简单易用的 JavaScript 验证工具库,提供丰富的验证方法和灵活的验证规则。

安装

bash

npm install jl-utils

快速开始

基本使用

javascript

// 方式一:导入默认实例
const validator = require('jl-utils');

// 方式二:导入 Validator 类
const { Validator } = require('jl-utils');

// 使用静态方法
console.log(Validator.isEmail('[email protected]')); // true
console.log(Validator.isPhone('13800138000')); // true

ES6 模块方式

javascript

import validator, { Validator } from 'jl-utils';

// 或者只导入需要的验证方法
import { isEmail, isPhone, validatePassword } from 'jl-utils';

功能特性

  • 邮箱验证 - 验证标准邮箱格式
  • 手机号验证 - 支持中国手机号验证
  • 身份证验证 - 支持中国 15 位和 18 位身份证验证
  • URL 验证 - 验证 URL 格式有效性
  • 数字验证 - 数字和整数验证
  • 空值检查 - 多种数据类型的空值检查
  • 长度验证 - 字符串长度范围验证
  • 范围验证 - 数字范围验证
  • 中文检测 - 检测字符串是否包含中文
  • 字母数字验证 - 纯字母数字字符串验证
  • 密码强度验证 - 可配置的密码强度验证
  • 日期格式验证 - 多种日期格式验证
  • 批量验证 - 支持多字段批量验证
  • 自定义规则 - 支持添加自定义验证规则

API 文档

基础验证方法

isEmail(email)

验证邮箱格式

javascript

Validator.isEmail('[email protected]'); // true
Validator.isEmail('invalid-email'); // false

isPhone(phone)

验证中国手机号格式

javascript

Validator.isPhone('13800138000'); // true
Validator.isPhone('12345678901'); // false

isIdCard(idCard)

验证中国身份证号

javascript

Validator.isIdCard('110101199001011234'); // true
Validator.isIdCard('123456789012345'); // true (15位)

isURL(url)

验证 URL 格式

javascript

Validator.isURL('https://example.com'); // true
Validator.isURL('not-a-url'); // false

isNumber(value)

验证是否为数字

javascript

Validator.isNumber(123); // true
Validator.isNumber('123'); // true
Validator.isNumber('abc'); // false

isInteger(value)

验证是否为整数

javascript

Validator.isInteger(123); // true
Validator.isInteger(123.45); // false

isEmpty(value)

验证是否为空值

javascript

Validator.isEmpty(''); // true
Validator.isEmpty([]); // true
Validator.isEmpty({}); // true
Validator.isEmpty(null); // true
Validator.isEmpty('text'); // false

字符串验证

isLength(str, min, max)

验证字符串长度范围

javascript

Validator.isLength('hello', 3, 10); // true
Validator.isLength('hi', 3, 10); // false

containsChinese(str)

验证是否包含中文

javascript

Validator.containsChinese('hello 世界'); // true
Validator.containsChinese('hello world'); // false

isAlphanumeric(str)

验证是否只包含字母和数字

javascript

Validator.isAlphanumeric('abc123'); // true
Validator.isAlphanumeric('abc 123'); // false

数字验证

isInRange(num, min, max)

验证数字是否在指定范围内

javascript

Validator.isInRange(5, 1, 10); // true
Validator.isInRange(15, 1, 10); // false

密码验证

validatePassword(password, options)

验证密码强度

javascript

const result = Validator.validatePassword('StrongPass123!', {
  minLength: 8,
  requireUppercase: true,
  requireLowercase: true,
  requireNumbers: true,
  requireSpecialChars: true
});

console.log(result);
// {
//   isValid: true,
//   strength: 'strong',
//   score: 5,
//   errors: []
// }

选项参数:

  • minLength: 最小长度 (默认: 8)
  • requireUppercase: 需要大写字母 (默认: true)
  • requireLowercase: 需要小写字母 (默认: true)
  • requireNumbers: 需要数字 (默认: true)
  • requireSpecialChars: 需要特殊字符 (默认: true)

日期验证

isDate(date, format)

验证日期格式

javascript

Validator.isDate('2023-12-25'); // true (默认格式 YYYY-MM-DD)
Validator.isDate('25/12/2023', 'DD/MM/YYYY'); // true
Validator.isDate('12-25-2023', 'MM-DD-YYYY'); // true

高级功能

批量验证

javascript

const data = {
  email: '[email protected]',
  phone: '13800138000',
  age: 25,
  password: 'Test123!'
};

const rules = {
  email: [
    { validator: 'isEmail', message: '邮箱格式不正确' }
  ],
  phone: [
    { validator: 'isPhone', message: '手机号格式不正确' }
  ],
  age: [
    {
      validator: (value) => Validator.isInRange(value, 18, 60),
      message: '年龄必须在18-60之间'
    }
  ],
  password: [
    {
      validator: (value) => Validator.validatePassword(value).isValid,
      message: '密码强度不足'
    }
  ]
};

const result = Validator.validate(data, rules);
console.log(result);
// {
//   isValid: true,
//   errors: {}
// }

自定义验证规则

javascript

// 添加自定义验证规则
Validator.addRule('isUsername', (username) => {
  return /^[a-zA-Z0-9_]{3,20}$/.test(username);
});

// 使用自定义规则
Validator.isUsername('john_doe123'); // true

// 在批量验证中使用
const rules = {
  username: [
    { validator: 'isUsername', message: '用户名格式不正确' }
  ]
};

错误处理

所有验证方法在遇到无效输入时会返回 false,不会抛出异常,确保代码的稳定性。

javascript

// 安全的验证调用
const isValid = Validator.isEmail(someUserInput);
if (!isValid) {
  // 处理无效输入
  console.log('邮箱格式不正确');
}

贡献

欢迎提交 Issue 和 Pull Request 来改进这个项目。

许可证

MIT License

更新日志

v1.0.0

  • 初始版本发布
  • 包含所有基础验证功能
  • 支持批量验证和自定义规则