函数(Func)
基础 url 配置
baseUrl
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
key | RedirectionType | RedirectionType.APIURL | 是 |
env | ImportMetaEnv 或 NodeJS.ProcessEnv | - | 是 |
注意: key 的类型值:枚举《RedirectionType》
js
import { baseUrl, RedirectionType } from '@masonjs/utils'
// 打包类型vue-cli,env传值为:process.env
// 打包类型vite,env传值为:import.meta.env
baseUrl(RedirectionType.APIURL, import.meta.env) // 返回dev、test、uat环境变量VITE_APP_FETCH_BASE_URL的值,生产读取body属性apiurl值
baseUrl(RedirectionType.DIREACTIVEURL, import.meta.env) // 返回dev、test、uat环境变量VITE_APP_REDIRECT_BASE_URL的值,生产读取body属性direactiveurl值
baseUrl(RedirectionType.ENTRYURL, import.meta.env) // 返回dev、test、uat环境变量VITE_APP_ONLYOFFICE_BASE_URL的值,生产读取body属性entryurl值
baseUrl(RedirectionType.PRINTURL, import.meta.env) // 返回dev、test、uat环境变量VITE_APP_PRINT_URL的值,生产读取body属性printurl值
baseUrl(RedirectionType.MICROURL, import.meta.env) // 返回dev、test、uat环境变量VITE_APP_MICRO_BASE_URL的值,生产读取body属性microurl值
版本:1.0.5 以上
防抖函数
debouce
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
callback | function | - | 是 |
wait | number | 1000 | 是 |
- 返回值:函数(function)
js
import { debouce } from '@masonjs/utils'
debouce(() => {
console.log(new Date())
}, 2000) // 2s之后打印日期
版本:1.0.0 以上
文件下载
downFile
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
value | Blob | - | 是 |
filename | string | - | 是 |
type | string | - | 是 |
js
import { downFile } from '@masonjs/utils'
downFile([流], [文件名称], [文件类型])
版本:1.0.5 以上
获取值数据类型
getValueType
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
value | any | - | 是 |
js
import { getValueType } from '@masonjs/utils'
getValueType('test') // [object String]
getValueType(123) // [object Number]
版本:1.0.5 以上
节流函数
throttle
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
callback | function | - | 是 |
delay | number | 1000 | 是 |
option | false | 非必填 |
— 返回值:函数(function)
js
import { throttle } from '@masonjs/utils'
throttle(() => { console.log(new Date()) }, 5000)() // 5s后执行打印
throttle(() => { console.log(new Date()) }, 5000, { immediate: true })() // 立即执行打印
- 版本:1.0.5 以上
深度拷贝
deepCopy
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
tSource | any | - | 是 |
js
import { deepCopy } from '@masonjs/utils'
const obj = { a:1, { c : 3 } }
const newObj = deepCopy(obj)
- 版本:1.0.5 以上
睡眠函数
- sleep
- 参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
time | number | - | 是 |
js
import { sleep } from '@masonjs/utils'
const fn = async () =>{
// --- 执行
await sleep(1000) // 等待 1s
// ---- 执行
}
- 版本:1.0.5 以上
获取唯一 id
- uuid
js
import { uuid } from '@masonjs/utils'
const uuid = uuid()
- 版本:1.0.5 以上
转换千分位函数
formatCommafy
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num | String / Number | - | 是 |
js
import { formatCommafy } from '@masonjs/utils'
const amount = formatCommafy(123456.123) //123,456.123
- 版本:1.0.5 以上
过滤 null 对象
filterNull
过滤对象属性为 null、undefined
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
params | Object | - | 是 |
js
import { filterNull } from '@masonjs/utils'
const params = { a: '1', b: null, c: { e: undefined, f: '' } }
filterNull(params) // { a: '1', c: { f: '' } }
- 版本:1.0.5 以上
加法
plus
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num1 | number | - | 是 |
num2 | number | - | 是 |
js
import { plus } from '@masonjs/utils'
plus(0.1,0.2) // 0.3
plus('0.1', '0.2', '0.3') // 0.6
plus(1.6e-30, 1.6e-30) // 3.2e-30
plus(...new Array(500).fill(1)) // 500
- 版本:1.0.6 以上
减法
minus
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num1 | number | - | 是 |
num2 | number | - | 是 |
js
import { minus } from '@masonjs/utils'
minus(0.2,0.1) // 0.1
minus(8893568.397103781249, -7.29674059550) // 8893575.693844376749
minus('6', '3', '2', '1', '2', '3') // -5
minus(500, ...new Array(500).fill(1)) // 0
- 版本:1.0.6 以上
乘法
times
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num1 | number | - | 是 |
num2 | number | - | 是 |
js
import { times } from '@masonjs/utils'
times(0.07, 100) // 7
times(118762317358.75, 1e-8) // 1187.6231735875
times(...new Array(500).fill(1)) // 1
- 版本:1.0.6 以上
除法
times
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num1 | number | - | 是 |
num2 | number | - | 是 |
js
import { divide } from '@masonjs/utils'
divide(1.21, 1.1) // 1.1
divide(-20, 5.2) // -3.8461538461538462
divide(...new Array(500).fill(1)) // 1
divide(1024, 4, 8, 2) // 16
- 版本:1.0.6 以上
四舍五入
round
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num1 | number | - | 是 |
num2 | number | - | 是 |
js
import { divide } from '@masonjs/utils'
round(0, 1) // 0
round(0.7875, 3) // 0.788
round('0.1049999999', 2) // 0.1
- 版本:1.0.6 以上
获取小数的长度
digitLength
参数:
参数 | 类型 | 默认值 | 是否必填 |
---|---|---|---|
num | number | - | 是 |
js
import { digitLength } from '@masonjs/utils'
digitLength(123.4567890123) // 10);
digitLength(1.23e-5) // 7;
digitLength(1.23E-5) // 7;
digitLength(1.233467e-5) === 11;
- 版本:1.0.6 以上