Appearance
super-tools-lib - v1.74.0 / Modules / tools
Module: tools
Table of contents
Functions
- awaitTo
- celsiusToFahrenheit
- checkPassWord
- colorHex
- darken
- debounce
- digitUppercase
- fahrenheitToCelsius
- fuzzyQuery
- generateSign
- getAge
- getFitSize
- getSex
- guid
- hexToRgba
- injectScript
- lighten
- rgbaToHex
- sinogToLetter
- throttle
- viewportToPixels
Functions
awaitTo
▸ awaitTo<T
, U
>(promise
, errorExt?
): Promise
<[U
, undefined
] | [null
, T
]>
将promise的错误转换为数组
Example
ts
import { awaitTo } from 'super-tools-lib'
const [err, res] = await awaitTo(fetch('https://www.baidu.com'))
if (err) {
console.log(err)
} else {
console.log(res)
}
Type parameters
Name | Type |
---|---|
T | T |
U | Error |
Parameters
Name | Type | Description |
---|---|---|
promise | Promise <T > | promise对象 |
errorExt? | object | 额外的错误信息 |
Returns
Promise
<[U
, undefined
] | [null
, T
]>
celsiusToFahrenheit
▸ celsiusToFahrenheit(celsius
): number
将摄氏温度转华氏温度
Since
1.62.0
Example
ts
import { celsiusToFahrenheit } from 'super-tools-lib'
celsiusToFahrenheit(10); // 50
Parameters
Name | Type | Description |
---|---|---|
celsius | number | 摄氏温度 |
Returns
number
返回华氏温度
checkPassWord
▸ checkPassWord(str
): number
检测密码强度
Since
1.62.0
Example
ts
import { checkPassWord } from 'super-tools-lib'
checkPassWord("ssssss@1Sdddd"); // 4
Parameters
Name | Type | Description |
---|---|---|
str | string | 要检测的密码 |
Returns
number
返回密码强度等级 1:密码弱 2:密码中等 3:密码强 4:密码很强
colorHex
▸ colorHex(color
): string
RGB颜色转16进制
Since
1.62.0
Example
ts
import { colorHex } from 'super-tools-lib'
colorHex("255,192,203"); // '#ffc0cb'
colorHex("rgb(255,192,203)"); // '#ffc0cb'
Parameters
Name | Type | Description |
---|---|---|
color | string | 要操作的RGB颜色值 |
Returns
string
返回16进制的颜色值
darken
▸ darken(color
, level
): string
将color颜色变深level倍
Example
ts
import { darken } from 'super-tools-lib'
darken('#000', 0.5) // '#000000'
Parameters
Name | Type | Description |
---|---|---|
color | string | 颜色值 |
level | number | 变深倍数 |
Returns
string
返回变深后的颜色值
debounce
▸ debounce(fn
, delay
): <T>(...rest
: T
[]) => void
防抖
- 频繁触发,但只执行规定时间内的最后一次调用
Since
1.62.0
Example
ts
import { debounce } from 'super-tools-lib'
const fn = debounce((e)=>console.log(e),3000)
fn(1)
fn(2)
fn(3)
// => 3
Parameters
Name | Type |
---|---|
fn | <T>(...rest : T []) => void |
delay | number |
Returns
fn
▸ <T
>(...rest
): void
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
...rest | T [] |
Returns
void
digitUppercase
▸ digitUppercase(n
): string
数字转化为大写金额
Since
1.62.0
Example
ts
import { digitUppercase } from 'super-tools-lib'
digitUppercase(10000); // '壹万元整'
Parameters
Name | Type | Description |
---|---|---|
n | number | 要操作数值 |
Returns
string
返回大写的金额
fahrenheitToCelsius
▸ fahrenheitToCelsius(fahrenheit
): number
将华氏温度转换为摄氏温度
Since
1.62.0
Example
ts
import { fahrenheitToCelsius } from 'super-tools-lib'
fahrenheitToCelsius(50); // 10
Parameters
Name | Type | Description |
---|---|---|
fahrenheit | number | 华氏温度 |
Returns
number
返回摄氏温度
fuzzyQuery
▸ fuzzyQuery<T
>(list
, key
, keyWord
): T
[]
模糊查询
Since
1.62.0
Example
ts
import { fuzzyQuery } from 'super-tools-lib'
const arr = [{id: 1}, {id: 2}, {id: 3}]
fuzzyQuery(arr,'id', 2) // [{id: 2}]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
list | T [] | 进行查询的数组 |
key | string | 进行查询的数组的字段 |
keyWord | string | 查询的关键词 |
Returns
T
[]
返回查询的结果
generateSign
▸ generateSign(obj
): string
生成sign 按 Key 的 ASCII 正序排序,拼接为字符串返回。
Since
1.62.0
Example
ts
import { generateSign } from 'super-tools-lib'
generateSign({userName: '1', paseWord: '1111'}) // 'paseWord1111userName1'
Parameters
Name | Type | Description |
---|---|---|
obj | Record <string , unknown > | 要操作的数据 |
Returns
string
返回sign字符串
getAge
▸ getAge(id
): string
根据身份证号获取年龄
Since
1.62.0
Example
ts
import { getAge } from 'super-tools-lib'
getAge("xxxxxxxxxxxxxxxxx"); // '29岁0月14天'
Parameters
Name | Type | Description |
---|---|---|
id | string | 身份证号 |
Returns
string
返回年龄
getFitSize
▸ getFitSize(px
, draft?
): number
返回设计稿上px在不同屏幕下的适配尺寸
Since
1.62.0
Example
ts
import { getFitSize } from 'super-tools-lib'
//375屏幕下
getFitSize(100, 750); // 50
Parameters
Name | Type | Default value | Description |
---|---|---|---|
px | number | undefined | 要操作的px尺寸 |
draft | number | 750 | 设计稿宽度 |
Returns
number
返回适配后的px尺寸
getSex
▸ getSex(id
): "男"
| "女"
根据身份证号获取性别
Since
1.62.0
Example
ts
import { getSex } from 'super-tools-lib'
getSex("xxxxxxxxxxxxxxxxx"); // '男'
Parameters
Name | Type | Description |
---|---|---|
id | string | 身份证号 |
Returns
"男"
| "女"
返回性别
guid
▸ guid(): string
生成一个唯一的guid
Since
1.62.0
Example
ts
import { guid } from 'super-tools-lib'
guid(); // 'bfa39b2f-f77e-425e-8f41-1fe0d8ac38b4'
Returns
string
返回唯一的uuid
hexToRgba
▸ hexToRgba(str
, alpa
): string
16进制颜色转RGBA
Since
1.62.0
Example
ts
import { hexToRgba } from 'super-tools-lib'
hexToRgba("#ffc0cb"); // 'rgba(255,192,203,1)'
hexToRgba("#ffc0cb", 0.7); // 'rgba(255,192,203,0.7)'
Parameters
Name | Type | Description |
---|---|---|
str | string | 要操作的16进制颜色值 |
alpa | number | 透明度 |
Returns
string
返回RGBA颜色值
injectScript
▸ injectScript(src
): void
动态引入js
Since
1.62.0
Example
ts
import { injectScript } from 'super-tools-lib'
injectScript(src)
Parameters
Name | Type | Description |
---|---|---|
src | string | js链接地址 |
Returns
void
lighten
▸ lighten(color
, level
): string
将color颜色变浅level倍
Since
1.63.0
Example
ts
import { lighten } from 'super-tools-lib'
lighten('#000', 0.5) // '#808080'
Parameters
Name | Type | Description |
---|---|---|
color | string | 颜色值 |
level | number | 变浅倍数 |
Returns
string
返回变浅后的颜色值
rgbaToHex
▸ rgbaToHex(color
): string
rgba颜色转16进制
Since
1.62.0
Example
ts
import { rgbaToHex } from 'super-tools-lib'
rgbaToHex("rgba(255,192,203,1)"); // '#ffc0cb'
Parameters
Name | Type | Description |
---|---|---|
color | any | 要操作的RGBA颜色值 |
Returns
string
返回16进制颜色值
sinogToLetter
▸ sinogToLetter(str
): string
汉字转字母
Since
1.62.0
Example
ts
import { sinogToLetter } from 'super-tools-lib'
sinogToLetter("你好"); // 'NH'
Parameters
Name | Type | Description |
---|---|---|
str | string | 要操作的汉字 |
Returns
string
返回汉字首字母
throttle
▸ throttle(fn
, delay?
): <T>(...rest
: T
[]) => void
节流
- 频繁触发,但只执行规定时间内的第一次
Since
1.62.0
Example
ts
import { throttle } from 'super-tools-lib'
const fn = throttle((e)=>console.log(e),3000)
// 连续多次触发会过滤掉规定时间内的第二次及以后的调用
fn(1)
fn(2)
fn(3)
// => 1
Parameters
Name | Type | Default value | Description |
---|---|---|---|
fn | <T>(...rest : T []) => void | undefined | - |
delay | number | 200 | 节流时间,毫秒 |
Returns
fn
返回截流函数
▸ <T
>(...rest
): void
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
...rest | T [] |
Returns
void
viewportToPixels
▸ viewportToPixels(value
): number
计算vh / vw转px
Since
1.62.0
Example
ts
import { viewportToPixels } from 'super-tools-lib'
viewportToPixels("90vh"); // 640
Parameters
Name | Type | Description |
---|---|---|
value | string | 要操作的vh/vw值 |
Returns
number
返回转换后的px值