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

wcollect

v1.0.21

Published

数组集合字典操作类

Readme

wcollect

一个轻量级的 TypeScript 集合操作工具库,提供了一系列用于处理数组、Map 等集合类型的实用方法。

安装

npm install wcollect

功能特点

  • 提供丰富的集合操作方法
  • 完全使用 TypeScript 编写,提供完整的类型支持
  • 无外部依赖
  • 支持链式调用

使用方法

基础操作

import collect from 'wcollect';

// 过滤数组
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = collect.filter(numbers, n => n % 2 === 0); // [2, 4]

// 统计数量
const count = collect.count(numbers, n => n > 3); // 2

// 计算总和
const sum = collect.sum(numbers); // 15

// 获取最大值
const max = collect.max(numbers); // 5

// 获取最小值
const min = collect.min(numbers); // 1

// 计算平均值
const avg = collect.avg(numbers); // 3

判断操作

// 判断是否存在满足条件的元素
const hasEven = collect.any(numbers, n => n % 2 === 0); // true

// 判断是否所有元素都满足条件
const allPositive = collect.all(numbers, n => n > 0); // true

// 判断两个数组是否相等
const arr1 = [1, 2, 3];
const arr2 = [3, 2, 1];
const isEqual = collect.equals(arr1, arr2); // true

转换操作

// 转换为数组
const set = new Set([1, 2, 3]);
const array = collect.toArray(set); // [1, 2, 3]

// 对象转 Map
const obj = { a: 1, b: 2 };
const map = collect.toMap(obj); // Map { 'a' => 1, 'b' => 2 }

// Map 转对象
const newObj = collect.toObject(map); // { a: 1, b: 2 }

// 合并数组
const nested = [[1, 2], [3, 4]];
const flat = collect.union(nested, arr => arr); // [1, 2, 3, 4]

分组和统计

const users = [
    { id: 1, name: 'Alice', age: 20 },
    { id: 2, name: 'Bob', age: 20 },
    { id: 3, name: 'Charlie', age: 30 }
];

// 按年龄分组
const byAge = collect.mapByFor(users, user => user.age);
// Map {
//   20 => [{id: 1, name: 'Alice', age: 20}, {id: 2, name: 'Bob', age: 20}],
//   30 => [{id: 3, name: 'Charlie', age: 30}]
// }

// 统计每个年龄的人数
const ageCount = collect.mapCount(users, user => user.age);
// Map { 20 => 2, 30 => 1 }

// 去重
const ages = [20, 20, 30, 40, 30];
const uniqueAges = collect.distinct(ages); // [20, 30, 40]

API 文档

基础操作

  • filter(array, func): 根据条件过滤数组
  • count(array, func): 统计满足条件的元素数量
  • sum(array, func?): 计算数组元素的总和
  • max(array, func?): 获取数组中的最大值
  • min(array, func?): 获取数组中的最小值
  • avg(array, func?): 计算数组元素的平均值

判断操作

  • any(array, func): 判断是否存在满足条件的元素
  • all(array, func): 判断是否所有元素都满足条件
  • equals(array1, array2): 判断两个数组是否相等

转换操作

  • toArray(iterable): 将可迭代对象转换为数组
  • toMap(object): 将对象转换为 Map
  • toObject(map): 将 Map 转换为对象
  • union(array, func): 将数组中的每个元素映射为数组,然后合并所有结果

分组和统计

  • mapByKey(array, keyFunc, valueFunc?): 根据键函数将数组转换为 Map
  • mapByFor(array, forFunc): 根据键函数将数组分组为 Map
  • mapByNodes(array, nodesFunc): 根据节点函数将数组分组为 Map
  • mapCount(array, keyFunc): 统计数组中每个键出现的次数
  • groupByKey(array, keyFunc, valueFunc?): 根据键函数将数组转换为对象
  • groupByFor(array, keyFunc): 根据键函数将数组分组为对象
  • groupByNodes(array, nodesFunc): 根据节点函数将数组分组为对象
  • groupCount(array, keyFunc): 统计数组中每个键出现的次数

其他操作

  • distinct(array, keyFunc?): 去除数组中的重复元素
  • mapFilter(map, filter): 根据条件过滤 Map

贡献

欢迎提交 Issue 和 Pull Request。

许可证

MIT