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

@zao-an/an-ut

v1.1.0

Published

一个还挺实用的Util库

Downloads

7

Readme

安装

npm install @zao-an/an-ut

使用

import * as aut from '@zao-an/an-ut'

具体方法

数组

chunk

将指定数组拆分为指定数量块,并生成一个新的数组返回,如果无法平均分,且fill未传,则最后的数据有多少是多少,如果指定了fill,则会进行填充
@parma array 要处理的数组
@param size 分割的长度
@param fill 未能平均分时最后一个数组要填充的数据

aut.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]

aut.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]

aut.chunk(['a', 'b', 'c', 'd'], 3, '--');
// => [['a', 'b', 'c'], ['d', '--', '--']]

compact

compact 从指定数组中删除掉所有假值,并将剩余的非假值放入一个新的数组中并返回

aut.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

concat

将所有的数据和指定数组连接起来,放入新的数组并返回

let array = [1];
let other = aut.concat(array, 2, [3], [[4]]);

console.log(other);
// => [1, 2, 3, [4]]

console.log(array);
// => [1]

difference

从指定数组中找出在剩余参数所有数组中不存在的数据,并放入一个数组中并返回

aut.difference([3, 2, 1], [4, 2]);
// => [3, 1]

differenceBy

找出指定数组中和剩余传入的数组中不相同的数据,怎么找由最后一个参数决定,最后一个参数可以是函数或字符串

aut.differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor);
// => [3.1, 1.3]

aut.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// => [{ 'x': 2 }]

differenceWith

传入一个comparator函数 将指定数组和剩余数组进行comparator函数比较,返回true则删除,返回false则留下,留下来的数据放入新的数组中并返回

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];

aut.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]

drop

从数组中删除指定个数的数据,从左开始

aut.drop([1, 2, 3]);
// => [2, 3]

aut.drop([1, 2, 3], 2);
// => [3]

aut.drop([1, 2, 3], 5);
// => []

aut.drop([1, 2, 3], 0);
// => [1, 2, 3]

dropMany

从指定数组中删除指定下标组的数据,并返回新的数组

aut.drop([1, 2, 3, 4, 5], [2, 3]);
// => [1, 2, 5]

aut.drop([1, 2, 3], [1]);
// => [1, 3]

dropRight

同drop,从右侧开始删除

aut.dropRight([1, 2, 3]);
// => [1, 2]

aut.dropRight([1, 2, 3], 2);
// => [1]

aut.dropRight([1, 2, 3], 5);
// => []

aut.dropRight([1, 2, 3], 0);
// => [1, 2, 3]

fill

/**
* 在指定数组中指定的位置(startIndex, endIndex)插入指定的值,指定位置的数据将按照option来处理
* array: any[]
* value: any
* option: {
*     replace?: boolean  // default true 为true时指定位置的数据将会被替换(不会改变原数组长度)
*     move?: 'right' | 'left' // default 'right' 指定位置的数据向 左 或 右 移动(会改变原数组长度)
* }
* startIndex?: number // default 0
* endIndex?: number // default array.length
*/

aut.fill([1, 2, 3, 4, 5], 'x', {replace: true}, 2, 3)
// => [1, 2, 'x', 'x', 5]

aut.fill([1, 2, 3, 4, 5], 'x', {replace: false}, 2, 3)
// => [1, 2, 'x', 'x', 3, 4, 5]

aut.fill([1, 2, 3, 4, 5], 'x', {replace: false, move: 'left'}, 2, 3)
[1, 2, 3, 4, 'x', 'x', 5]

flatten

减少一级嵌套深度

aut.flatten([[1, 2, 3], 4, 5, [6, 7, [8, 9, [10]]]])
// => [1, 2, 3, 4, 5, 6, 7, [8, 9, [10]

flattenDeep

将数组递归为一维数组

aut.flattenDeep([[1, 2, 3], 4, 5, [6, 7, [8, 9, [10]]]])
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

flattenDepth

根据 depth 递归减少 array 的嵌套层级

aut.flattenDepth([[1, 2, 3, [4]], 5], 1)
// => [1, 2, 3, [4], 5]

aut.flattenDepth([[1, 2, 3, [4]], 5], 2)
// => [1, 2, 3, 4, 5]

fromPairs

这个方法返回一个由键值对pairs构成的对象。

aut.fromPairs([['fred', 30], ['barney', 40]]);
// => { 'fred': 30, 'barney': 40 }

indexOf

返回首次 value 在数组array中被找到的 索引值, 如果 fromIndex 为负值,将从数组array尾端索引进行匹配。
如果要找的数据为对象,则会进行深度比对

aut.indexOf([1, {a: 'b', c: 'd'}, 1, 2], {a: 'b', c: 'd'});
// => 1

intersection

取所有数组的交集,并放入一个新的数组并返回,允许存在复杂对象,会进行深度比对

aut.intersection([2, 1], [4, 2], [1, 2]);
// => [2]

intersectionBy

这个方法类似intersection,区别是它接受一个 iteratee 调用每一个arrays的每个值以产生一个值,通过产生的值进行了比较。结果值是从第一数组中选择。iteratee 会传入一个参数:(value)。

aut.intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor);
// => [2.1]

aut.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }]

intersectionIndex

找出所有数组的数据交集的下标(存在于第一个数组的下标),并放入一个数组中并返回

aut.intersectionIndex([2, 1, 3, 6], [4, 2], [6, 1]);
// => [0, 1, 2]

aut.intersectionIndex([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }]);
// => [0]

intersectionWith

这个方法类似 intersection,区别是它接受一个 comparator 调用比较arrays中的元素。结果值是从第一数组中选择。comparator 会传入两个参数:(arrVal, othVal)。

aut.intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor);
// => [2.1]

join

将 array 中的所有元素转换为由 separator 分隔的字符串。

aut.join([1, 2, 3, 4, 5, 6], '~')
// => 1~2~3~4~5~6

lastIndexOf

这个方法类似 indexOf ,区别是它是从右到左遍历array的元素。

aut.lastIndexOf([1, {a: 'b', c: 'd'}, 1, 2, {a: 'b', c: 'd'}], {a: 'b', c: 'd'});
// => 4

pull

移除数组array中所有和给定值相等的元素,

let array = [1, 2, 3, 1, 2, 3];
aut.pull(array, 2, 3);
console.log(array);
// => [1, 1]

pullAll

这个方法类似 pull,区别是这个方法接收一个要移除值的数组。

let array = [1, 2, 3, 1, 2, 3];
aut.pull(array, [2, 3]);
console.log(array);
// => [1, 1]

pullAllBy

这个方法接受一个 iteratee(迭代函数) 调用 array 和 values的每个值以产生一个值,通过产生的值进行了比较。iteratee 会传入一个参数: (value)。

aut.pullAllBy([1, 1.8, 3, 1.9, 2.8, 1], [2, 3], Math.ceil)
// => [1, 1]

screenBy

根据指定 iteratee 函数或字符串 从数组中筛选出来。
注:
  传入的iteratee为函数时,则每项都当做参数传给iteratee函数
  传入的iteratee为字符串时,则当前项为JSON对象时,则取key为iteratee的数据,取不到则取原值

aut.screenBy([{x: '66', y: '77'}, {r: '5', y: '88'}, {q: '8', y: '99'}], 'y')
// => ['77', '88', '99']

aut.screenBy([3.1, 2.2, 1.3], Math.ceil)
// => [4, 3, 2]

sortByLength

根据长度和指定方式进行排序,返回排序后的数组

@param array: any[][], 
@param sort: 'asc' | 'desc' = 'desc'

aut.sortByLength([[1, 2, 3], [4, 3, 2, 1]])
// => [[4, 3, 2, 1], [1, 2, 3]]

aut.sortByLength([[1, 2, 3], [4, 3, 2, 1]], 'asc')
// => [[1, 2, 3], [4, 3, 2, 1]]

sortedIndex

使用二进制的方式检索来决定 value值 应该插入到数组中 尽可能小的索引位置,以保证array的排序。

util.sortedIndex([30, 50], 60)
// => 2

util.sortedIndex([30, 50, 90, 40], 70)
// => 3

字符串

basisReg

生成一些常用的正则表达式

@param type 'phone' | 'name' | 'identity-card-no-15' | 'identity-card-no-18' | 'date' | 'email' | 'special-chart'

aut.basisReg('phone')
// => /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/

aut.basisReg('special-chart')
// => /[%`_~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?-]/

camelCase

转换字符串string为驼峰写法。会清除特殊字符

aut.camelCase('__foo_bar--royo@@Qed')
// => 'fooBarRoyoQed'

aut.camelCase('__foo_bar--Toyo')
// => 'fooBarToyo'

capitalize

转换字符串string首字母为大写,剩下为小写。

aut.capitalize('FRED')
// => 'Fred'

dropStrBy

从字符串中删除 iteratee 函数返回true的字符

aut.dropStrBy('--foo-bar--', function iteratee(str){ return str === '-' })
// => 'foobar'

dropStrByRE

从字符串中删除指定正则匹配true的字符

aut.dropStrByRE('__FOO_BAR__', util.basisReg('special-chart'))
// => 'FOOBAR'

kebabCase

转换字符串string为kebab case.

aut.kebabCase('__foo_bar__left')
// => 'foo-bar-left'

lowerCaseSplit

将字符串全部变成小写,遇特殊字符或大写字符按指定字符分割分割,默认分割字符为',',会清除字符串前后的特殊字符

aut.lowerCaseSplit('__fooBar_hrHr__%%%left', '-')
// => 'foo-bar-hr-hr-left'

aut.lowerCaseSplit('__fooBar_hrHr__%%%left', '-')
// => 'foo,bar,hr,hr,left'

genUuid

生成唯一32位长度的字符串,

aut.genUuid()
// => '3d65db9a0caa43bd96141341bf808936'

convertStr

转成字符串类型

aut.convertStr({a: 1, b: "2", c: false, d: ["1", 2, false, null], e: {q: false, w: 33, e: "88"}})
// => '{"a":1,"b":"2","c":false,"d":["1",2,false,null],"e":{"q":false,"w":33,"e":"88"}}'

JSON对象

compareObj

两个JSON对象之间的比较, 深比较

aut.compareObj({a: {b: 2}, fn: function fn(){ console.log('123') }, v: [1,211,3, {w: 'e', f: [{a: '2222'}]}]}, {a: {b: 2}, v: [1,211,3, {w: 'e', f: [{a: '2222'}]}], fn: function fn(){ console.log('123') }})
// => true

// 最后一个函数的打印改一些东西
aut.compareObj({a: {b: 2}, fn: function fn(){ console.log('123') }, v: [1,211,3, {w: 'e', f: [{a: '2222'}]}]}, {a: {b: 2}, v: [1,211,3, {w: 'e', f: [{a: '2222'}]}], fn: function fn(){ console.log('1234') }})
// => false

isEmpty

判断对象是否为空对象

aut.isEmpty({})
// => true

aut.isEmpty({a: "1"})
// => false

公共的

assertJsonObj

断言为JSON对象类型

aut.assertJsonObj({})
// => true

aut.assertJsonObj(1)
// => false

assertArrayObj

断言为数组类型

aut.assertJsonObj([])
// => true

aut.assertJsonObj(1)
// => false

aut.assertJsonObj({})
// => false

assertFunctionObj

断言为函数类型

aut.assertJsonObj(function fn(){})
// => true

aut.assertJsonObj(1)
// => false

aut.assertJsonObj({})
// => false

aut.assertJsonObj([])
// => false

assertEqualType

断言传入的所有数据都是同一个类型

aut.assertEqualType(1, 1)
// => true

aut.assertEqualType(1, '')
// => false

assertSpecifiedType

断言传入的数据为指定类型, 传入的类型为一个数组,类型多个的话表示或者

@param data: any
@param type: 'String' | 'Number' | 'Boolean' | 'Null' | 'Undefined' | 'Function' | 'Array' | 'Object' []

aut.assertSpecifiedType(1, ['Number'])
// => true

aut.assertSpecifiedType(1, ['Number', 'Function'])
// => true

aut.assertSpecifiedType([], ['Number', 'Function'])
// => false

aut.assertSpecifiedType([], ['Number', 'Array'])
// => true

mustArray

必须为数组类型,否则报错必须是Array类型,如果想判断数组里面的所有数据的类型,第二个参数传入一个类型数组

@param array: any
@param dataType?: 'String' | 'Number' | 'Boolean' | 'Null' | 'Undefined' | 'Function' | 'Array' | 'Object' []

aut.mustArray([])
// => undefined

aut.mustArray(1)
// => errorMessage.ts:13 Uncaught Error: 数据类型错误,必须是Array类型
        at Object.mustArray (VM4656 errorMessage.ts:13)
        at <anonymous>:1:6

mustJson

必须为JSON对象类型,否则报错

mustFunction

必须为函数类型,否则报错

mustString

必须为字符串类型,否则报错

mustNumber

必须为数字类型,否则报错

mustBoolean

必须为布尔类型,否则报错

mustSpecifiedType

必须为指定的类型,否则报错

@param data: any 
@param type: 'String' | 'Number' | 'Boolean' | 'Null' | 'Undefined' | 'Function' | 'Array' | 'Object' []
@param preCustomMsg?: string // 前置报错信息

isEqual

比较两个数据,可以比较 Function | Array | JSON | string | number | boolean | null | undefined

@param data: any
@param compareData: any

deepCopy

数据的深拷贝

let a = {a: 1, b: '2', c: false, d: [1, 2, 3 ,'4', function fn(){}], e: function fv(){}}
let b = aut.deepCopy(a)

console.log(b)
// => {a: 1, b: '2', c: false, d: [1, 2, 3 ,'4', function fn(){}], e: function fv(){}}

console.log(a === b)
// false

ternary

替代三元表达式,第一个参数为任意表达式,当表达式为true时返回第二个参数,否则返回第三个

console.log(aut.ternary(null, 1, 2))
// => 2

console.log(aut.ternary(1, 1, 2))
// => 1

console.log(aut.ternary(() => true, 1, 2))
// => 1

console.log(aut.ternary(() => false, 1, 2))
// => 2

nvl

第一个参数为false时,返回第二个参数

aut.nvl(1, 2)
// => 1
aut.nvl(0, 2)
// => 2

Console

封装了Console对象,向外暴露log对象,并提供五个函数:success, error, warning, debug, info

拓展

pubSub (发布订阅)

  • subscribe 订阅消息

@param eventType 事件名称

@param effect 事件函数

@return collectorId 订阅ID

  • publish 发布事件

@param eventType 事件名称

@param args 事件函数参数

@param collectorId? 订阅ID

  • ubSubscribe 取消单个订阅消息

@param collectorId: 订阅ID

  • unSubscribeByEvent 取消订阅事件

@param eventType: 事件名称