# lmy-utils

> utils for js/ts

Latest version **1.3.1** (published 2022-09-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install lmy-utils
pnpm add lmy-utils
yarn add lmy-utils
bun add lmy-utils
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2022-09-22 |
| First published | 2022-09-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 26.9 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Author | 刘牧野 |
| Maintainers | liumuye |
| Keywords | utils, javascript, typescript, quicker code |

## Links

- npm: https://www.npmjs.com/package/lmy-utils
- npm.io page: https://npm.io/package/lmy-utils

## Dependencies (3)

- [dayjs](https://npm.io/package/dayjs.md) ^1.11.5
- [nanoid](https://npm.io/package/nanoid.md) ^4.0.0
- [currency.js](https://npm.io/package/currency.js.md) ^2.0.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2022-09-22
- 1.3.0 — 2022-09-22
- 1.2.0 — 2022-09-19
- 1.1.4 — 2022-09-16
- 1.1.3 — 2022-09-16
- 1.1.2 — 2022-09-14
- 1.1.1 — 2022-09-14
- 1.1.0 — 2022-09-14
- 1.0.7 — 2022-09-13
- 1.0.6 — 2022-09-13
- 1.0.5 — 2022-09-12
- 1.0.4 — 2022-09-12
- 1.0.3 — 2022-09-12
- 1.0.2 — 2022-09-12
- 1.0.1 — 2022-09-09
- … 1 more at https://npm.io/package/lmy-utils/versions

## README

# 1.3.1更新内容
  - 12.金额转大写
  - 13.运算精度丢失问题(两数的加减乘除,如需更近一步了解参考14)
  - 14.[currency.js](https://currency.js.org/)是一个轻量级的〜1kb JavaScript库，用于处理货币值。 它旨在解决javascript中的浮点问题。
# 用法

## JavaScript
```js
import { removeRepeat , ...rest } from 'lmy-utils'

```

## TypeScript

```js
import { removeRepeat , ...rest } from 'lmy-utils/index'

```

## 1.removeRepeat(数组对象去重)
`removeRepeat (arr: Array<object>,attribute: string ,order: boolean = true)`
```js
let arr = [
  {
    name:'xiaoming',
    age:'12'
  },
  {
    name:'xiaohong',
    age:'12'
  }
]
removeRepeat(arr,'age');//[{name:'xiaoming',age:'12'}]
```

## 2.random(获取唯一字符串)不传参数默认21位
`random(size:number) :string`
```js
random(22);
random(32);
```

## 3.timeMethod依赖[day.js](https://dayjs.fenxianglu.cn/)进行时间处理
`timeMethod(date: string | number, format: boolean = true)`
```js
    timeMethod().today 获取当前时间不带时分秒
    timeMethod().todayComplete 获取当前时间带时分秒
    timeMethod().formatTime(date,format) 穿参数进行时间格式化,第一个参数时间戳，第二个参数布尔值，默认true，默认带时分秒
    timeMethod().exposeDayjs 暴露的dayjs可以根据需求使用
```

## 4.deepClone深拷贝
`deepClone(obj: unknown):unknown`
```js
let arr = [1,2,3];
let newArr = deepClone(arr);
newArr[0] = 4;
console.log(arr,newArr);//[1,2,3]  [4,2,3]
```

## 5.debounce防抖
`debounce(fn:Function, time:number = 1000)`

## 6.throttle节流
`throttle(fn:Function, time:number = 1000)`

## 7.judgmentType判断任意数据的类型
`judgmentType(data:unknown)`

## 8.characterFrequency统计数组或字符串中出现字符的次数
`characterFrequency(data: Array<string|number> | string )`
```js
characterFrequency([1,2,3,1,2,3]); //{ '1': 2, '2': 2, '3': 2 }
characterFrequency('12332'); //{ '1': 1, '2': 2, '3': 2 }
```

## 9.timeAround计算时间在N之前或在N之后(可传时间戳/可传时间格式的字符串[2022-12-12 or 2022-12-12 12:00:00]) 
`timeAround(time: number | string)`
```js
timeAround("1663141162171");//xx秒之前
timeAround("2022-09-16 18:00:00");//xx秒之前
```

# 10.regularCheckFun正则校验
`regularCheckFun(data:string | number,type:number,subclass?:string)`
 data 需要校验的数据
 type 需要校验的类型 
  - 1.中国地区手机号 2.国内座机电话 3.邮箱地址 4.银行卡号 5.身份证号 6.中国邮政编码 7.密码校验(强:strong,中:in,弱:weak)
    - 密码校验
        >强：最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
        
        >中：最少6位，字母+数字，字母+特殊字符，数字+特殊字符

        >弱：最少6位，纯数字，纯字母，纯特殊字符
  <hr />
 subclass 校验类型的子类 目前只有密码校验存在子类

 ```js
regularCheckFun(15939678299,1);//true
regularCheckFun('123456',7,'weak');//true
regularCheckFun('123456',7,'strong');//false
regularCheckFun('aA1@aaf12',7,'strong');//true

 ```

 # 11.objToArrObj对象处理成数组对象
 `interface Person { [key:string]: unknown;}  objToArrObj(obj:Person)`
 ```js
 let obj = {a:1b:2,c:3}
 objToArrObj(obj);//[ { a: 1 }, { b: 2 }, { c: 3 } ]
 ```

 # 12.金额转大写
`convertCurrency (money:number | string)`
```js
convertCurrency("120.12")//壹佰贰拾元壹角贰分
convertCurrency(520)//伍佰贰拾元整
```

# 13.运算精度丢失问题(两数的加减乘除,如需更近一步了解参考14)
>type calculationType = string | number;

`plus(arg1:calculationType, arg2:calculationType)加法`
`subtract(arg1:calculationType, arg2:calculationType)减法`
`multiply(arg1:calculationType, arg2:calculationType)乘法`
`divide(arg1:calculationType, arg2:calculationType)除法`

```js
calculation.plus(0.1,0.05);//0.15
calculation.subtract(0.1,0.05);//0.05
calculation.multiply(0.1,0.05);//0.005
calculation.divide(0.1,0.05);//2
```

# 14.[currency.js](https://currency.js.org/)
>currency.js是一个轻量级的〜1kb JavaScript库，用于处理货币值。 它旨在解决javascript中的浮点问题。

`currencyFun()`
```js
//其他用法参照官方文档正常使用
let currency = currencyFun();
let {value} = currency("$2.51").add(0.1).add(0.3);
console.log(value);//2.91
```

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