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

complex-utils

v2.10.3

Published

a complex utils

Readme

Complex-Utils

complex-utils 是一个功能强大且全面的 TypeScript/JavaScript 工具库,旨在为现代Web开发提供一组高效、可靠的辅助函数和类。

✨ 功能特性

  • 类型安全: 使用 TypeScript 编写,提供强大的类型支持。
  • 模块化: 所有功能均以独立的模块导出,支持摇树优化(Tree Shaking)。
  • 功能丰富: 涵盖对象操作、异步控制、数字格式化、数据结构转换等多个方面。
  • 环境兼容: 可同时在浏览器和 Node.js 环境下工作。

🚀 安装

npm install complex-utils

📚 API 文档 & 用法

Class (面向对象工具)

Life - 事件与生命周期管理器

一个强大的发布-订阅(Pub/Sub)系统,用于管理事件回调和生命周期。

import { Life } from 'complex-utils';

const life = new Life();

// 订阅事件
const id = life.on('data-loaded', (data) => {
  console.log('Data has been loaded:', data);
});

// 触发事件
life.trigger('data-loaded', { user: 'Alice' });

// 取消订阅
life.off('data-loaded', id);

Limit - 白名单/黑名单限制器

轻松实现功能的白名单或黑名单控制。

import { Limit } from 'complex-utils';

// 黑名单模式 (默认)
const blackList = new Limit({ list: ['admin'] });
console.log(blackList.getLimit('admin')); // true (被限制)
console.log(blackList.getLimit('user'));  // false (不被限制)

// 白名单模式
const whiteList = new Limit({ type: 'allow', list: ['user'] });
console.log(whiteList.getLimit('admin')); // true (被限制)
console.log(whiteList.getLimit('user'));  // false (不被限制)

Function (函数工具)

debounce - 函数防抖

在事件频繁触发时,确保函数只在最后一次触发后的指定时间内执行一次。

import { debounce } from 'complex-utils';

window.addEventListener(
  'resize',
  debounce(() => {
    console.log('Window resized!');
  }, 250)
);

throttle - 函数节流

确保函数在指定的时间间隔内最多只执行一次。

import { throttle } from 'complex-utils';

window.addEventListener(
  'scroll',
  throttle(() => {
    console.log('User is scrolling!');
  }, 100)
);

Object (对象和数组工具)

deepClone - 深拷贝

提供两种深拷贝模式,满足不同需求。

import { deepClone } from 'complex-utils';

const obj = { a: 1, b: { c: new Date() } };

// 快速模式 (默认, 基于 JSON.stringify)
const clonedObj1 = deepClone(obj);

// 复杂模式 (支持 Map, Set, 循环引用等)
const clonedObj2 = deepClone(obj, true);

formatTree - 列表转树形结构

高效地将包含 idparentId 的扁平数组转换为嵌套的树形结构。

import { formatTree } from 'complex-utils';

const list = [
  { id: 1, name: 'Root', parentId: 0 },
  { id: 2, name: 'Child A', parentId: 1 },
  { id: 3, name: 'Child B', parentId: 1 },
];

const tree = formatTree(list).parse();
/*
[
  {
    id: 1,
    name: 'Root',
    parentId: 0,
    children: [
      { id: 2, name: 'Child A', parentId: 1 },
      { id: 3, name: 'Child B', parentId: 1 }
    ]
  }
]
*/

getComplexProp - 安全地获取深层属性

通过点分隔的字符串路径安全地访问对象的深层属性。

import { getComplexProp } from 'complex-utils';

const user = {
  name: 'Alice',
  address: {
    city: 'Wonderland',
    zip: 12345
  }
};

const city = getComplexProp(user, 'address.city'); // 'Wonderland'
const street = getComplexProp(user, 'address.street'); // undefined

... 以及更多强大的工具函数等待您的探索!

🧪 单元测试

项目使用 Vitest 进行单元测试,以确保代码的质量和可靠性。

  • 运行测试:

    npm test
  • 生成测试覆盖率报告:

    npm run coverage