# @zao-an/an-ut

> 一个还挺实用的Util库

Latest version **1.1.0** (published 2022-08-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zao-an/an-ut
pnpm add @zao-an/an-ut
yarn add @zao-an/an-ut
bun add @zao-an/an-ut
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-08-06 |
| First published | 2021-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 161.8 KB |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| Author | zao-an |
| Maintainers | zao-an |
| Keywords | lodash, lib, library, js, javascript |

## Links

- npm: https://www.npmjs.com/package/@zao-an/an-ut
- npm.io page: https://npm.io/package/@zao-an/an-ut

## Dependencies (6)

- [webpack](https://npm.io/package/webpack.md) ^5.54.0
- [ts-loader](https://npm.io/package/ts-loader.md) ^9.2.6
- [typescript](https://npm.io/package/typescript.md) ^4.4.3
- [webpack-cli](https://npm.io/package/webpack-cli.md) ^4.8.0
- [webpack-dev-server](https://npm.io/package/webpack-dev-server.md) ^4.3.0
- [html-webpack-plugin](https://npm.io/package/html-webpack-plugin.md) ^5.3.2

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2022-08-06
- 1.0.39 — 2022-08-06
- 1.0.38 — 2022-06-19
- 1.0.37 — 2022-06-18
- 1.0.36 — 2022-06-15
- 1.0.35 — 2022-06-02
- 1.0.32 — 2022-04-09
- 1.0.31 — 2022-04-09
- 1.0.30 — 2022-01-21
- 1.0.29 — 2022-01-01
- 1.0.28 — 2022-01-01
- 1.0.27 — 2022-01-01
- 1.0.26 — 2022-01-01
- 1.0.25 — 2022-01-01
- 1.0.24 — 2021-12-25
- … 26 more at https://npm.io/package/@zao-an/an-ut/versions

## 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: 事件名称

---
_Source: https://npm.io/package/@zao-an/an-ut · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
