Skip to content
On this page

super-tools-lib - v1.74.0 / Modules / tools

Module: tools

Table of contents

Functions

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

NameType
TT
UError

Parameters

NameTypeDescription
promisePromise<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

NameTypeDescription
celsiusnumber摄氏温度

Returns

number

返回华氏温度


checkPassWord

checkPassWord(str): number

检测密码强度

Since

1.62.0

Example

ts
import { checkPassWord } from 'super-tools-lib'

checkPassWord("ssssss@1Sdddd"); // 4

Parameters

NameTypeDescription
strstring要检测的密码

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

NameTypeDescription
colorstring要操作的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

NameTypeDescription
colorstring颜色值
levelnumber变深倍数

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

NameType
fn<T>(...rest: T[]) => void
delaynumber

Returns

fn

▸ <T>(...rest): void

Type parameters
Name
T
Parameters
NameType
...restT[]
Returns

void


digitUppercase

digitUppercase(n): string

数字转化为大写金额

Since

1.62.0

Example

ts
import { digitUppercase } from 'super-tools-lib'

digitUppercase(10000); // '壹万元整'

Parameters

NameTypeDescription
nnumber要操作数值

Returns

string

返回大写的金额


fahrenheitToCelsius

fahrenheitToCelsius(fahrenheit): number

将华氏温度转换为摄氏温度

Since

1.62.0

Example

ts
import { fahrenheitToCelsius } from 'super-tools-lib'

fahrenheitToCelsius(50); // 10

Parameters

NameTypeDescription
fahrenheitnumber华氏温度

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

NameTypeDescription
listT[]进行查询的数组
keystring进行查询的数组的字段
keyWordstring查询的关键词

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

NameTypeDescription
objRecord<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

NameTypeDescription
idstring身份证号

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

NameTypeDefault valueDescription
pxnumberundefined要操作的px尺寸
draftnumber750设计稿宽度

Returns

number

返回适配后的px尺寸


getSex

getSex(id): "男" | "女"

根据身份证号获取性别

Since

1.62.0

Example

ts
import { getSex } from 'super-tools-lib'

getSex("xxxxxxxxxxxxxxxxx"); // '男'

Parameters

NameTypeDescription
idstring身份证号

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

NameTypeDescription
strstring要操作的16进制颜色值
alpanumber透明度

Returns

string

返回RGBA颜色值


injectScript

injectScript(src): void

动态引入js

Since

1.62.0

Example

ts
import { injectScript } from 'super-tools-lib'

injectScript(src)

Parameters

NameTypeDescription
srcstringjs链接地址

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

NameTypeDescription
colorstring颜色值
levelnumber变浅倍数

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

NameTypeDescription
colorany要操作的RGBA颜色值

Returns

string

返回16进制颜色值


sinogToLetter

sinogToLetter(str): string

汉字转字母

Since

1.62.0

Example

ts
import { sinogToLetter } from 'super-tools-lib'

sinogToLetter("你好"); // 'NH'

Parameters

NameTypeDescription
strstring要操作的汉字

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

NameTypeDefault valueDescription
fn<T>(...rest: T[]) => voidundefined-
delaynumber200节流时间,毫秒

Returns

fn

返回截流函数

▸ <T>(...rest): void

Type parameters
Name
T
Parameters
NameType
...restT[]
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

NameTypeDescription
valuestring要操作的vh/vw值

Returns

number

返回转换后的px值