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

hzzt-extensions

v0.0.5

Published

数组、字符串、对象扩展

Readme

使用方法

npm install hzzt-extensions

import 'hzzt-extensions'

数组扩展方法

first

获取或设置数组的第一个元素

const arr = [1, 2, 3];
console.log(arr.first); // 1
arr.first = 10;
console.log(arr); // [10, 2, 3]

last

获取或设置数组的最后一个元素

const arr = [1, 2, 3];
console.log(arr.last); // 3
arr.last = 30;
console.log(arr); // [1, 2, 30]

isEmpty

判断数组是否为空

[].isEmpty; // true
[1].isEmpty; // false

isNotEmpty

判断数组是否不为空

[].isNotEmpty; // false
[1].isNotEmpty; // true

isSingle

判断数组是否只有一个元素

[].isSingle; // false
[1].isSingle; // true
[1, 2].isSingle; // false

isMultiple

判断数组是否有多个元素

[].isMultiple; // false
[1].isMultiple; // false
[1, 2].isMultiple; // true

treeForeach(callback, key = 'children', parent?, lastIndex?, level?)

多维数组的遍历方法

const tree = [{id: 1, children: [{id: 2}]}];
tree.treeForeach(item => console.log(item.id)); // 输出: 1, 2

treeMap(callback, key = 'children', parent?, lastIndex?, level?)

多维数组的映射方法

const tree = [{id: 1, children: [{id: 2}]}];
const newTree = tree.treeMap(item => ({...item, id: item.id * 10}));

treeFilter(callback, key = 'children', parent?, lastIndex?, level?)

多维数组的过滤方法(仅保留匹配元素)

const tree = [{id: 1, children: [{id: 2}]}];
const filtered = tree.treeFilter(item => item.id > 1);

treeFilterV2(callback, key = 'children', parent?, lastIndex?, level?)

多维数组的过滤方法(保留匹配元素及其祖先)

const tree = [{id: 1, children: [{id: 2}]}];
const filtered = tree.treeFilterV2(item => item.id === 2);

treeFind(callback, key = 'children', parent?, lastIndex?, level?)

多维数组的查找方法

const tree = [{id: 1, children: [{id: 2}]}];
const found = tree.treeFind(item => item.id === 2);

next(predicate)

查找满足条件的下一个元素

const arr = [1, 2, 3];
arr.next(v => v === 2); // 3

prev(predicate)

查找满足条件的上一个元素

const arr = [1, 2, 3];
arr.prev(v => v === 2); // 1

toMap()

将数组转化为Map对象

[1, 2, 3].toMap(); // Map(3) {0 => 1, 1 => 2, 2 => 3}

toSet()

将数组转化为Set对象

[1, 1, 2].toSet(); // Set(2) {1, 2}

getRange(start, end)

获取数组指定范围内的元素

[1, 2, 3, 4].getRange(1, 3); // [2, 3]

splitBy(key)

根据属性将数组分组

const users = [
{name: 'Alice', role: 'admin'},
{name: 'Bob', role: 'user'},
{name: 'Charlie', role: 'admin'}
];
users.splitBy('role');
// 返回: {admin: [{name: 'Alice'}, {name: 'Charlie'}], user: [{name: 'Bob'}]}

removeAt(index)

根据索引删除元素

const arr = [1, 2, 3];
arr.removeAt(1); // 返回被删除的元素 [2]
// arr变为 [1, 3]

remove(value)

根据值删除元素

const arr = [1, 2, 3];
arr.remove(2); // 返回被删除的元素 [2]
// arr变为 [1, 3]

removeWhere(predicate)

根据条件删除元素

const arr = [1, 2, 3];
arr.removeWhere(v => v > 1); // 返回被删除的元素 [2, 3]
// arr变为 [1]

deDuplication()

基本类型数组去重

[1, 1, 2].deDuplication(); // [1, 2]

deDuplicationBy(key)

对象数组根据属性去重

const arr = [{id: 1}, {id: 1}, {id: 2}];
arr.deDuplicationBy('id'); // [{id: 1}, {id: 2}]

insert(index, item)

在指定位置插入元素

[1, 3].insert(1, 2); // [1, 2, 3]

insertAll(index, items)

在指定位置插入多个元素

[1, 4].insertAll(1, [2, 3]); // [1, 2, 3, 4]

divide(predicate)

根据条件将数组一分为二

[1, 2, 3, 4].divide(v => v % 2 === 0);
// 返回: [[2, 4], [1, 3]]

divideByNum(n)

将数组按指定数量分割

[1, 2, 3, 4, 5].divideByNum(2);
// 返回: [[1, 2], [3, 4], [5]]

pluck(key)

从数组中提取指定属性的值

const users = [{name: 'Alice'}, {name: 'Bob'}];
users.pluck('name'); // ['Alice', 'Bob']

flatPluck(key, childKey?)

从多维数组中提取并扁平化值

const tree = [
{name: 'A', children: [{name: 'A1'}]},
{name: 'B', children: [{name: 'B1'}]}
];
tree.flatPluck('children', 'name'); // ['A1', 'B1']

filterEmpty()

过滤数组中的空值

[0, 1, null, undefined, false, ''].filterEmpty();
// 返回: [0, 1, '']

mapJoin(fn, split = ',', filter = true)

map和join的组合操作

const users = [{name: 'Alice'}, {name: 'Bob'}, {name: null}];
users.mapJoin('name'); // 'Alice,Bob'
users.mapJoin(u => u.name, ';', false); // 'Alice;Bob;null'

sum(key?)

数组求和

[1, 2, 3].sum(); // 6
[{price: 10}, {price: 20}].sum('price'); // 30
[{price: 10}, {price: 20}].sum((i) => i.price); // 30

sortBy(key?, orderBy = 'asc') 根据属性排序

const users = [{name: 'Bob'}, {name: 'Alice'}];
users.sortBy('name'); // [{name: 'Alice'}, {name: 'Bob'}]
users.sortBy('name', 'desc'); // [{name: 'Bob'}, {name: 'Alice'}]

findBy(key, value)

根据属性查找元素

const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
users.findBy('id', 2); // {id: 2, name: 'Bob'}

toObject(key?, fn?)

将数组转化为对象

const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
users.toObject('id');
// {1: {id:1, name:'Alice'}, 2: {id:2, name:'Bob'}}

users.toObject('id', 'name');
// {1: 'Alice', 2: 'Bob'}

filterMap(fn)

过滤和映射的组合操作

[1, 2, 3, 4].filterMap(v => v % 2 === 0 ? v * 2 : null);
// 返回: [4, 8]

字符串扩展方法

isEmpty

判断字符串是否为空

''.isEmpty; // true
'abc'.isEmpty; // false

isNotEmpty

判断字符串是否不为空

''.isNotEmpty; // false
'abc'.isNotEmpty; // true

isBlank

判断字符串是否为空或空白字符

''.isBlank; // true
'  '.isBlank; // true
' a '.isBlank; // false

toFirstUpperCase

将字符串首字母转为大写

'hello world'.toFirstUpperCase(); // 'Hello world'
'测试字符串'.toFirstUpperCase(); // '测试字符串'

snakeToCamel(key?)

将下划线命名转为驼峰命名

'user_name'.snakeToCamel(); // 'userName'(默认分隔符'_')
'user-name'.snakeToCamel('-'); // 'userName'(自定义分隔符'-')
'my_test_string'.snakeToCamel(); // 'myTestString'

camelToSnake(key?)

将驼峰命名转为下划线命名

'userName'.camelToSnake(); // 'user_name'(默认分隔符'_')
'userName'.camelToSnake('-'); // 'user-name'(自定义分隔符'-')
'myTestString'.camelToSnake(); // 'my_test_string'