公共函数,使用'lj-utils'引入
- Description:
类型转换相关处理
- Source:
Methods
(inner) arrayToObj(list, key, extraData) → {Object}
- Description:
指定数组key,转换为对象
- Source:
Example
// 示例
const list = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const obj = arrayToObj(list, 'id');
console.log(obj); // 输出: { '1': { id: 1, name: 'Alice' }, '2': { id: 2, name: 'Bob' } }
Parameters:
Name | Type | Description |
---|---|---|
list |
Array.<any> | 数组。 |
key |
string | 对象对应的键值。 |
extraData |
string | object | 为string时,返回对象的键值对,为object时,返回对象。 |
Returns:
- 返回的对象。
- Type
- Object
(inner) autoPlayAudio(audioUrl, isWeixin) → {HTMLAudioElement}
Parameters:
Name | Type | Description |
---|---|---|
audioUrl |
string | 音频地址 |
isWeixin |
boolean | 是否在微信中 |
Returns:
- 音频元素
- Type
- HTMLAudioElement
(inner) blobToBase64(argBlob) → {Promise}
Example
blobToBase64(blob).then(base64 => console.log(base64));
Parameters:
Name | Type | Description |
---|---|---|
argBlob |
Blob | 需要处理的Blob对象 |
Returns:
返回base64字符串
- Type
- Promise
(inner) blobToFile(argBlob, argName) → {File}
Example
const file = blobToFile(blob, 'example.txt');
Parameters:
Name | Type | Description |
---|---|---|
argBlob |
Blob | Blob对象 |
argName |
string | 文件名,默认为当前时间戳 |
Returns:
返回File对象
- Type
- File
(inner) blobUrlToFile(argData) → {Promise}
Example
blobUrlToFile(url).then(file => console.log(file));
Parameters:
Name | Type | Description |
---|---|---|
argData |
string | blobUrl |
Returns:
返回File对象
- Type
- Promise
(inner) dataURLtoBlob(argData) → {Blob}
Example
const blob = dataURLtoBlob(dataUrl);
Parameters:
Name | Type | Description |
---|---|---|
argData |
string | dataUrl |
Returns:
返回Blob对象
- Type
- Blob
(inner) debounce(func, delay) → {function}
Example
```
const debouncedHello = debounce(sayHello, 1000);
debouncedHello('Hello from 1');
debouncedHello('Hello from 2');
```
Parameters:
Name | Type | Description |
---|---|---|
func |
function | 要进行防抖的函数 |
delay |
number | 延迟时间,单位毫秒 |
Returns:
返回防抖后的函数
- Type
- function
(inner) decodeHtml(argHtml) → {string}
- Description:
反转义HTML标签。
- Source:
Example
// 返回 "<p>Hello, World!</p>"
decodeHtml("<p>Hello, World!</p>");
Parameters:
Name | Type | Description |
---|---|---|
argHtml |
string | 需要反转义的文本。 |
Returns:
反转义后的文本。
- Type
- string
(inner) deepCopy(obj)
Parameters:
Name | Type | Description |
---|---|---|
obj |
需要拷贝的对象 |
Returns:
拷贝后的对象
(inner) delCookie(argName)
Parameters:
Name | Type | Description |
---|---|---|
argName |
string | 要清除的值 |
(inner) delay(time)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
time |
number |
0
|
延迟的时间量,单位为毫秒 |
(inner) dlFile(argData, argName, argDelTime)
Example
dlFile(data, 'example.txt', 10000);
Parameters:
Name | Type | Description |
---|---|---|
argData |
string | Blob | File | Blob对象/File对象/dataUrl/base64 |
argName |
string | 文件名 |
argDelTime |
number | 移除dateUrl时间 |
(inner) encodeHtml(argHtml) → {string}
- Description:
转义html标签
- Source:
Example
// returns '<p>Hello, World!</p>'
encodeHtml('<p>Hello, World!</p>')
Parameters:
Name | Type | Description |
---|---|---|
argHtml |
string | 需要转义的文本 |
Returns:
- Type
- string
(inner) formatNumber(argData, argNum) → {string}
- Description:
将数据转换为数量单位k/w
- Source:
Example
// returns '2k'
formatNumber(2000, 2)
Parameters:
Name | Type | Description |
---|---|---|
argData |
number | 数据 |
argNum |
number | 保留的小数位数,默认为2 |
Returns:
计算结果
- Type
- string
(inner) formatSize(argData, argNum, argIndex, argRate, unit) → {string}
- Description:
将byte数据转换为容量单位
- Source:
Example
// returns '1.95K'
formatSize(2000, 2, 0, 1024, ['B', 'K', 'M', 'G', 'T', 'P'])
Parameters:
Name | Type | Description |
---|---|---|
argData |
number | byte数据 |
argNum |
number | 保留的小数位数,默认为2 |
argIndex |
number | 起始单位偏移,例如: 0:b 1:k 2:m,默认为0 |
argRate |
number | 进制,默认为1024 |
unit |
Array.<string> | 进制,默认为['B', 'K', 'M', 'G', 'T', 'P'] |
Returns:
计算结果
- Type
- string
(inner) formatTime(date, fmt, emptyTip) → {string}
- Description:
日期格式化显示。
- Source:
Example
// 返回 '2022-01-01 00:00:00'
formatTime(new Date(2022, 0, 1));
Parameters:
Name | Type | Description |
---|---|---|
date |
string | number | Date | 时间对象或时间戳,默认为当前时间。 |
fmt |
string | 格式化字符串,默认为'YYYY-MM-DD HH:mm:ss'。E为星期数,EEE:星期一,q为季度,S为毫秒数。 |
emptyTip |
string | 当date为false时的默认值,默认为''。 |
Returns:
格式化后的日期字符串。
- Type
- string
(inner) friendlyTime(date, fmt, emptyTip) → {string}
- Description:
日期格式化友好显示。例如:刚刚,x分钟前,...。超过一年的按 fmt来格式化。
- Source:
Example
// 返回 '刚刚'
friendlyTime(new Date());
Parameters:
Name | Type | Description |
---|---|---|
date |
string | number | Date | 时间对象或时间戳,默认为当前时间。 |
fmt |
string | 格式化字符串,默认为'YYYY-MM-DD HH:mm:ss'。 |
emptyTip |
string | 当date为false时的默认值,默认为''。 |
Returns:
格式化后的友好日期字符串。
- Type
- string
(inner) getCookie(argName) → {string}
Parameters:
Name | Type | Description |
---|---|---|
argName |
string | 要获取的值 |
Returns:
- Type
- string
(inner) getCookieObj() → {object}
Returns:
- Type
- object
(inner) getDuration(argFile) → {Promise}
Example
getDuration(file).then(duration => console.log(duration));
Parameters:
Name | Type | Description |
---|---|---|
argFile |
Blob | File | string | 音视频数据,如果是string类型,则为链接 |
Returns:
返回时长(单位:秒)
- Type
- Promise
(inner) getEnv(key) → {string}
Parameters:
Name | Type | Description |
---|---|---|
key |
string | 要获取的值 |
Returns:
获取的值
- Type
- string
(inner) getInfo() → {Info}
Returns:
{ ua: ua, platform: 平台, isMobile: 移动端, isWin: winPC端, isIphone: iphone, isIpad: ipad, isMac: mac, isAppleMobile: 苹果移动端webview isSafari: Safari浏览器, isIos: Ios平台, isAndroid: android平台, isIE: 显示8 9 10, true为11以上 ... }
- Type
- Info
(inner) getNetworkStatus() → {boolean}
Example
```
const status = getNetworkStatus();
```
Returns:
-
boolean
-
- Type
- boolean
(inner) getObj(key, argData, isDeepCopy) → {AnyObject}
Parameters:
Name | Type | Description |
---|---|---|
key |
string | 要获取的值 |
argData |
string | 要合并的值 |
isDeepCopy |
boolean | 是否深拷贝 |
Returns:
获取的值
- Type
- AnyObject
(inner) getRandomColor()
Returns:
string
(inner) getRegexp() → {object|object}
Example
```
const regex = getRegexp();
```
Returns:
-
- Type
- object
-
- Type
- object
(inner) getStorage(argKey, argNoJson) → {any}
Parameters:
Name | Type | Description |
---|---|---|
argKey |
string | 要获取的key |
argNoJson |
string | true时不自动转换JSON字符串 |
Returns:
key对应的数据
- Type
- any
(inner) getUrlParam(argName, argUrl) → {string}
Parameters:
Name | Type | Description |
---|---|---|
argName |
string | 要获取的key |
argUrl |
string | url数据 |
Returns:
- Type
- string
(inner) getUrlParamObj(argData) → {any}
Parameters:
Name | Type | Description |
---|---|---|
argData |
string | 要处理的数据 |
Returns:
- Type
- any
(inner) getUuid() → {string}
Returns:
uuid
- Type
- string
(inner) hideInfo(argData, argStart, argEnd) → {string}
- Description:
数据中间加星号
- Source:
Example
// 示例
const data = '1234567890';
const hiddenData = hideInfo(data, 3, 4);
console.log(hiddenData); // 输出: '123***7890'
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argData |
string | number | 要处理的数据。 |
|
argStart |
number |
3
|
前端显示多少位。 |
argEnd |
number |
4
|
后面显示多少位。 |
Returns:
- 返回处理好的数据。
- Type
- string
(inner) isBlob(argData) → {boolean}
Example
isBlob(new Blob()); // true
Parameters:
Name | Type | Description |
---|---|---|
argData |
any | 需要判断的数据 |
Returns:
如果是Blob返回true,否则返回false
- Type
- boolean
(inner) isFile(argData) → {boolean}
Example
isFile(new File([""], "filename")); // true
Parameters:
Name | Type | Description |
---|---|---|
argData |
any | 需要判断的数据 |
Returns:
如果是File返回true,否则返回false
- Type
- boolean
(inner) isIdCard(code) → {boolean}
Example
isIdCard('320311770706001'); // false
Parameters:
Name | Type | Description |
---|---|---|
code |
string | 身份证号码 |
Returns:
如果是正确的身份证号码返回true,否则返回false
- Type
- boolean
(inner) isJson(argData) → {boolean}
Example
isJson('{"name":"John"}'); // true
Parameters:
Name | Type | Description |
---|---|---|
argData |
any | 需要判断的数据 |
Returns:
如果是JSON返回true,否则返回false
- Type
- boolean
(inner) isPrime(argValue) → {boolean}
Example
isPrime(5); // true
Parameters:
Name | Type | Description |
---|---|---|
argValue |
number | 需要判断的数值 |
Returns:
如果是质数返回true,否则返回false
- Type
- boolean
(inner) loadFile(argUrl, argType, argOptions) → {Promise}
Example
loadFile(url, 'js', {force: true}).then(() => console.log('Loaded'));
Parameters:
Name | Type | Description |
---|---|---|
argUrl |
string | 要加载的url |
argType |
string | 加载类型 js/css |
argOptions |
object | |
argOption.disCheck |
string | 不检查是否有相同标签 |
argOption.force |
string | 是否强制添加,true时先删除再添加 |
Returns:
返回Promise对象
- Type
- Promise
(inner) objToArray(obj, keyopt, valueopt, isReverseopt) → {array}
- Description:
将对象转换为键值对数组。
- Source:
Example
// Example usage
const obj = { a: 1, b: 2 };
const result = objToArray(obj, 'id', 'v', true);
console.log(result); // Output: [{ v: 'a', id: 1 }, { v: 'b', id: 2 }]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | 要转换的对象。 |
||
key |
string |
<optional> |
'key'
|
每个键值对的键名称默认key。 |
value |
string |
<optional> |
'value'
|
每个键值对的值名称默认value。 |
isReverse |
boolean |
<optional> |
false
|
如果为true,则键值对将被颠倒,默认为false。 |
Returns:
- 键值对数组。
- Type
- array
(inner) objToObj(obj, tmplObj, isAssign) → {Object}
- Description:
从原始对象中复制属性,并将其赋值给新对象,生成一个新对象。
- Source:
Example
// 示例
const obj = { a: 1, b: 2 };
const tmplObj = { a: 0, c: 0 };
const newObj = objToObj(obj, tmplObj);
console.log(newObj); // 输出: { a: 1, c: '' }
Parameters:
Name | Type | Default | Description |
---|---|---|---|
obj |
Object | 要复制属性的原始对象。 |
|
tmplObj |
Object | 要赋值属性的新对象模板。 |
|
isAssign |
boolean |
false
|
可选。确定是否将属性赋值给原始对象。 |
Returns:
复制了属性的新对象。
- Type
- Object
(inner) off(el, event, handler)
Parameters:
Name | Type | Description |
---|---|---|
el |
Element | 绑定元素 |
event |
string | 事件名称 |
handler |
function | 事件处理函数 |
(inner) on(el, event, handler)
Parameters:
Name | Type | Description |
---|---|---|
el |
Element | 绑定元素 |
event |
string | 事件名称 |
handler |
function | 事件处理函数 |
(inner) px2vw(argPx, argWith, argUnit, argNum) → {string}
- Description:
将px单位转换为vw单位
- Source:
Example
// returns '187.5vw'
px2vw(200, 375, 'vw', 6)
Parameters:
Name | Type | Description |
---|---|---|
argPx |
number | px单位的数值 |
argWith |
number | px对应的最大宽度,默认为375 |
argUnit |
string | 单位,'vw'或'%',默认为'vw' |
argNum |
number | 保留的小数位数,默认为6 |
Returns:
转换后的数值
- Type
- string
(inner) randomInt(min, max) → {number}
Parameters:
Name | Type | Default | Description |
---|---|---|---|
min |
number |
0
|
最小值 |
max |
number |
100
|
最大值 |
Returns:
- Type
- number
(inner) remInit(argBaseSize, argWidth)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argBaseSize |
number |
16
|
基础大小 16px(要跟配置一致) |
argWidth |
number |
375
|
基准宽度 |
(inner) replaceUrlParam(name, value, url) → {string}
Parameters:
Name | Type | Description |
---|---|---|
name |
string | key |
value |
string | number | undefined | null | 要替换的value |
url |
string | 要替换的网址,默认location.href |
Returns:
- Type
- string
(inner) requestDeviceMotionPermission()
Example
```
const hasRequest = await requestDeviceMotionPermission()
if (hasRequest) {
useEventListener(window, 'devicemotion', handleShake)
} else {
showToast('未授权访问运动传感器,请手动点击!')
}
```
Returns:
Promise,指示是否已授予权限。
(inner) rmbPrice(argData, argRate, argUnit, argDef) → {string}
- Description:
显示人民币价格。
- Source:
Example
// 返回 '¥100.00'
rmbPrice(100, 2);
Parameters:
Name | Type | Description |
---|---|---|
argData |
unknown | 价格。 |
argRate |
number | 保留多少位小数,默认为-1。 |
argUnit |
string | 单位,默认为'¥'。 |
argDef |
string | 空数据默认值,默认为'--'。 |
Returns:
格式化后的价格字符串,例如:'¥100'。
- Type
- string
(inner) safe(argData, argCheck, argValue, argSetValueForce) → {any}
Parameters:
Name | Type | Description |
---|---|---|
argData |
any | [原始数据] |
argCheck |
string | [要返回的数据,用'.'连接,数组用'.+数字表示'] |
argValue |
any | [如果数据有误,返回的值,选填] |
argSetValueForce |
boolean | 0 | 1 | [是否强制赋值argValue] |
Returns:
- Type
- any
(inner) safeData(argData, argCheck, argValue, argSetValueForce) → {any}
Parameters:
Name | Type | Description |
---|---|---|
argData |
any | [原始数据] |
argCheck |
string | [要返回的数据,用'.'连接,数组用'.+数字表示'] |
argValue |
any | [如果数据为undefined/null,返回argValue,选填] |
argSetValueForce |
boolean | 0 | 1 | [是否强制赋值argValue,强制赋值时只返回true/false] |
Returns:
- Type
- any
(inner) secondToTime(argData, argOption) → {string}
- Description:
将秒数转换为倒计时格式
- Source:
Example
// returns '01h05s'
secondToTime(3905, {s: '秒', m: '分', h: '时', d: '天', M: '月', y: '年', minUtil: 's', maxUnit: 'y', hideZero: false, isAddZero: true})
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
argData |
number | 秒数 |
|||||||||||||||||||||||||||||||||
argOption |
Object | 配置项 Properties
|
Returns:
转换后的倒计时格式的字符串
- Type
- string
(inner) setCookie(argName, argValue, argTime, argPath, domain)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argName |
string | 要设置的key |
|
argValue |
string | 要设置的value |
|
argTime |
number |
24
|
过期时间/时 默认24 |
argPath |
number |
/
|
path |
domain |
number | domain |
(inner) setEnv(env)
Example
setEnv(import.meta.env)
Parameters:
Name | Type | Description |
---|---|---|
env |
AnyObject | 要设置的值 |
(inner) setLog(logConfig)
Example
setLog(localStorage.getItem('logLevel'),{
error: (...arg) => {
// todo
}
})
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
logConfig |
AnyObject | 重写配置 Properties
|
(inner) setObj(key, data) → {AnyObject}
Parameters:
Name | Type | Description |
---|---|---|
key |
string | 要设置的key |
data |
AnyObject | 要设置的值 |
Returns:
- Type
- AnyObject
(inner) setStorage(argKey, argData) → {string}
Parameters:
Name | Type | Description |
---|---|---|
argKey |
string | 要获取的key |
argData |
unknown | 要保存的数据 |
Returns:
保存的数据String
- Type
- string
(inner) setTitle(argTitle)
Parameters:
Name | Type | Description |
---|---|---|
argTitle |
string | 标题 |
(inner) setUrlParams(argParams, noMarknullable) → {string}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
argParams |
any | 参数对象 |
|
noMark |
boolean |
<nullable> |
默认带?,true时,不带 |
Returns:
- Type
- string
(inner) sleep(ms)
Parameters:
Name | Type | Description |
---|---|---|
ms |
number | 时间,毫秒 |
(inner) string10to62(argData) → {string}
- Description:
将10进制数据转换为62进制,用于短网址转换。
- Source:
Example
// 示例
const data = 123456;
const convertedData = string10to62(data);
console.log(convertedData); // 输出: 'w7E'
Parameters:
Name | Type | Description |
---|---|---|
argData |
number | string | 要处理的数据。 |
Returns:
- 返回处理好的数据。
- Type
- string
(inner) string62to10(argData) → {number}
- Description:
将62进制数据转换为10进制,用于短网址转换。
- Source:
Example
// 示例
const data = 'w7E';
const convertedData = string62to10(data);
console.log(convertedData); // 输出: 123456
Parameters:
Name | Type | Description |
---|---|---|
argData |
string | 要处理的数据。 |
Returns:
- 返回处理好的数据。
- Type
- number
(inner) throttle(fn, delayopt, …extra) → {function}
Example
```
const sayHelloFn = (a: string,b:any) => {
console.log(a,b);
}
const throttleHello = throttle(sayHelloFn, 300,'extra info);
throttleHello('Hello from 1');
throttleHello('Hello from 2');
// Hello from 1 extra info
```
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
fn |
function | 要节流的函数。 |
||
delay |
number |
<optional> |
300
|
函数执行之间的延迟时间(毫秒)。默认为 300 毫秒。 |
extra |
any |
<repeatable> |
可选的额外参数,传递给节流函数的原始调用。 |
Returns:
返回一个新的函数,该函数具有节流行为。
- Type
- function
(inner) toFixed(argData, argNum, argType) → {string}
- Description:
对数据进行toFixed处理。
- Source:
Example
// 示例
const data = 123.456;
const fixedData = toFixed(data, 2, 'round');
console.log(fixedData); // 输出: '123.46'
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argData |
string | number | 要处理的数据。 |
|
argNum |
number |
2
|
要保留的位数,默认返回2位小数。 |
argType |
string |
round
|
返回类型,round:默认四舍五入,floor:向下取整,ceil:向上取整,abs:绝对值。 |
Returns:
- 返回处理好的数据。
- Type
- string
(inner) toHump(argData, argUnit) → {string}
- Description:
将下划线字符串转换为驼峰字符串
- Source:
Example
// 示例
const data = 'underscore_case';
const convertedData = toHump(data);
console.log(convertedData); // 输出: 'underscoreCase'
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argData |
string | 要转换的字符串。 |
|
argUnit |
string |
_
|
要转换的字符,默认为“_”。 |
Returns:
- 转换后的驼峰字符串。
- Type
- string
(inner) toLine(argData, argUnit) → {string}
- Description:
将驼峰字符串转换为下划线字符串
- Source:
Example
// 示例
const data = 'camelCase';
const convertedData = toLine(data);
console.log(convertedData); // 输出: 'camel_case'
Parameters:
Name | Type | Default | Description |
---|---|---|---|
argData |
string | 要转换的字符串。 |
|
argUnit |
string |
_
|
要转换的字符,默认为“_”。 |
Returns:
- 转换后的下划线字符串。
- Type
- string