Processor

数据处理

Description:
  • 数据处理

Source:
Since:
  • 1.1.0

Methods

(static) bytesToSize(bytes, optionsopt) → {string}

Description:
  • 字节转换存储单位

Source:
Since:
  • 3.8.0
Example
bytesToSize(0); // 0 B
bytesToSize(1024); // 1 KB
bytesToSize(3.213243*1024*1024); // 3.21 MB
bytesToSize(1024*1024*1024); // 1 GB

// 自定义间隔符号
bytesToSize(0, { spaceMark: '' }); // 0B
bytesToSize(1024, { spaceMark: '' }); // 1KB
Parameters:
Name Type Attributes Description
bytes number

字节大小

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
spaceMark string <optional>
' '

间隔字符

precision number <optional>
2

精度

Returns:

存储单位值

Type
string

(static) dataURLToBlob(data) → {Blob}

Description:
  • 将 DataURL 转为 Blob 对象

Source:
Since:
  • 4.1.0
See:
Example
const data = 'data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=';
dataURLToBlob(dataurl); // Blob {size: 32, type: 'text/html'}
Parameters:
Name Type Description
data string

data: 协议的URL

Returns:

Blob 对象

Type
Blob

(static) fileReader(blob, typeopt) → {Promise.<(string|ArrayBuffer)>}

Description:
  • 读取 Blob 或 File 对象,转为 Base64/String/ArrayBuffer

    注意:该方法仅适用于浏览器端。

Source:
Since:
  • 4.16.0
See:
Example
const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob

fileReader(htmlBlob).then(data=>{
  console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
});

const textBlob = new Blob(aFileParts, { type: 'text/plain' });

fileReader(textBlob, 'text').then(data=>{
  console.log(data); // <a id="a"><b id="b">hey!</b></a>
});
Parameters:
Name Type Attributes Default Description
blob Blob

Blob 或 File 对象

type 'arrayBuffer' | 'binaryString' | 'dataURL' | 'text' <optional>
'dataURL'

读取类型

Returns:

文件内容

Type
Promise.<(string|ArrayBuffer)>

(static) formatBankCard(bankCardNo, optionsopt) → {string}

Description:
  • 格式化银行卡号

Source:
Since:
  • 1.1.0
Example
// 19位银行卡
formatBankCard('6228480402564890018'); // 6228 4804 0256 4890 018

// 16位银行卡
formatBankCard('6228480402564890'); // 6228 4804 0256 4890

// 脱敏银行卡
formatBankCard('6228********890'); // 6228 **** **** 890

// 16位银行卡,"-"间隔
formatBankCard('6228480402564890', {spaceMark: '-'}); // 6228-4804-0256-4890
Parameters:
Name Type Attributes Description
bankCardNo string

要处理的字符串

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
spaceMark string <optional>
' '

间隔字符

length number <optional>
4

间隔长度

Returns:

格式化的银行卡号

Type
string

(static) formatMobile(mobileNo, optionsopt) → {string}

Description:
  • 格式化手机号码

Source:
Since:
  • 4.5.0
Example
formatMobile('13345678900'); // 133 4567 8900
formatMobile('13345678900', { spaceMark: '-' }); // 133-4567-8900

// 脱敏手机号码
formatMobile('133****1234'); // 133 **** 1234
formatMobile('133****1234', { spaceMark: '-' }); // 133-****-1234

// 手机号码位数不够
formatMobile('133'); // 133
formatMobile('133456'); // 133 456
formatMobile('13345678'); // 133 4567 8
Parameters:
Name Type Attributes Description
mobileNo string

手机号码

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
spaceMark string <optional>
' '

间隔字符

Returns:

格式化后的手机号码

Type
string

(static) formatMoney(num, optionsopt) → {string}

Description:
  • 格式化金额

Source:
Since:
  • 1.1.0
Example
// 整数
formatMoney(1000); // 1,000.00

// 小数(默认保留2位小数)
formatMoney(3000.03); // 3,000.03

// 保留4位小数
formatMoney(3000.03, { precision: 4 }); // 3,000.0300

// 保留10位小数
formatMoney(1500.2, { precision: 10 }); // 1,500.2000000000

// 自定义单位符号
formatMoney(1000.00, { symbol: '$' }); // $1,000.00

// 自定义千位分割符(默认',')
formatMoney(1000.00, { thousand: '|' }); // 1|000.00

// 自定义小数位分割符(默认'.')
formatMoney(1000.00, { decimal: '&' }); // 1,000&00

// 字符串数字
formatMoney('3000.03', { precision: 4 }); // 3,000.0300
Parameters:
Name Type Attributes Description
num string | number

需转换金额 (最大:9007199254740991 最小: -9007199254740991)

options Object <optional>

金额格式化配置

Properties
Name Type Attributes Default Description
precision number <optional>
2

保留位数 (最高:10位)

symbol string <optional>

货币符号

thousand string <optional>
","

千分位符号

decimal string <optional>
"."

小数位符号

Returns:

格式化的金额

Type
string

(static) numberToChinese(num, optionsopt) → {string}

Description:
  • 数字转中文数字

    如果数字不在安全数字 -9007199254740991~9007199254740991 范围内,处理会有异常。

Source:
Since:
  • 1.2.0
Example
numberToChinese(100); // 一百
numberToChinese(100.3); // 一百点三
numberToChinese(1234567890); // 一十二亿三千四百五十六万七千八百九十
numberToChinese(1234567890.11); // 一十二亿三千四百五十六万七千八百九十点一一

// 繁体
numberToChinese(100, {big5: true}); // 壹佰
numberToChinese(100.3, {big5: true}); // 壹佰點叁
numberToChinese(1234567890.11, {big5: true}); // 壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾點壹壹

// 不带计数单位
numberToChinese(1990, {unit: false}); // 一九九零

// 不带计数单位,修改0
numberToChinese(1990, {unit: false, zero:'〇'}); // 一九九〇
Parameters:
Name Type Attributes Description
num number

数字

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
big5 boolean <optional>
false

繁体

unit boolean <optional>
true

计数单位

decimal string <optional>
"点"

中文小数点,繁体字为點

zero string <optional>
"零"

设置0。常用配置 〇

negative string <optional>
"负"

负数前面的字

unitConfig Object <optional>

节点单位配置

Properties
Name Type Attributes Default Description
w string <optional>
"万"

设置计数单位万。常用配置 萬

y string <optional>
"亿"

设置计数单位亿。常用配置 億

Returns:

中文数字

Type
string

(static) padZero(value, sizeopt) → {string}

Description:
  • 前置补零

Source:
Since:
  • 4.7.0
Example
padZero(5); // 05
padZero('5'); // 05

padZero(12); // 12
padZero('12'); // 12

padZero(688); // 688
padZero('688'); // 688

padZero(688, 5); // 00688
padZero('688', 5); // 00688
Parameters:
Name Type Attributes Default Description
value string | number

要处理的值

size number <optional>
2

指定字符串长度

Returns:

用零填充数字到给定长度的字符串

Type
string

(static) parseIdCard(id) → {IdCardInfo|null}

Description:
  • 解析身份证号码,支持15、18位身份证号码

Source:
Since:
  • 4.0.0
See:
Example
parseIdCard('123456789123456'); // null

// 18位身份证号码
parseIdCard('130701199310302288');
// =>
{
  birthday: "1993-10-30",
  gender: "女",
  origin: { province: "13", city: "07", area: "01", year: "1993", month: "10", day: "30", gender: "8" },
  province: "河北省"
}

// 15位身份证号码
parseIdCard('130701931030228');
// =>
{
  birthday: "93-10-30",
  gender: "女",
  origin: { province: "13", city: "07", area: "01", year: "93", month: "10", day: "30", gender: "8" },
  province: "河北省"
}
Parameters:
Name Type Description
id string

身份证号码,支持15位

Returns:

省份、生日、性别,省/市/区/年/月/日/性别编码。如果解析失败将返回 null 。

Type
IdCardInfo | null

(static) replaceChar(str, optionsopt) → {string}

Description:
  • 替换字符,应用场景如:脱敏

Source:
Since:
  • 1.1.0
Example
// 手机号 前3后4
replaceChar('13000000000'); // 130****0000

// 手机号 前2后4
replaceChar('13000000000', { start: 2 }); // 13*****0000

// 身份证 前6后4
replaceChar('130701199310302288', { start: 6, end: -4 }); // 130701********2288

// 邮箱@前两位
const email = '12345@qq.com';
const emailAtIndex = email.indexOf('@');
replaceChar('email', { start: emailAtIndex - 2, end: emailAtIndex }); // 123**@qq.com

// 邮箱 前2和@后面内容,固定替换字符4位
replaceChar(email, {start: 2, end: email.indexOf('@'), repeat: 4}); // 12****@qq.com

// 银行卡号 只展示后4位,固定替换字符4位
replaceChar('6228480402564890018', {start: 0, end: -4, repeat: 4}); // ****0018

// 银行卡号 前6后4
replaceChar('6228480402564890018', { start: 6, end: -4 }); // 622848*********0018

// 银行卡号 前4后3 忽略格式的空格
replaceChar('6228 4804 0256 4890 018', {start: 4, end: -4, exclude: ' '}); // 6228 **** **** **** 018

// 用户名
replaceChar('林某某', {start: 1, end: Infinity, repeat: 2}); // 林**
replaceChar('林某', {start: 1, end: Infinity, repeat: 2}); // 林**
replaceChar('林某某某', { start: 1, end: -1, repeat: 1 }); // 林*某
Parameters:
Name Type Attributes Description
str string

要处理的字符串

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
start number <optional>
3

开始位置

end number <optional>
-4

结束位置

char string <optional>
"*"

替换字符

repeat number <optional>

替换字符的重复次数,默认为替换内容长度,可设置为固定值

exclude string <optional>

排除字符,如果指定排除项,repeat设置无效

Returns:

处理后的字符

Type
string

(static) safeDate(valueopt, …argsopt) → {Date}

Description:
  • 创建一个 Date 实例日期对象,同 new Date()

    规避了苹果设备浏览器不支持部分格式(例如,YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。

    如果参数为 undefined 正常返回 Date 。

Source:
Since:
  • 4.4.0
See:
Example
safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
Parameters:
Name Type Attributes Description
value string | number | Date <optional>

日期时间字符串、毫秒数、日期对象

args number <optional>
<repeatable>

月/日/时/分/秒/毫秒

Returns:

Date 实例日期对象

Type
Date

(static) setDataURLPrefix(data, mimeTypeopt, base64opt) → {string}

Description:
  • 设置 DataURL 前缀、MIME 类型、base64 标识。

    如果你需要获取DataURL 的 MIME 类型和数据本身,推荐使用 data-urls

Source:
Since:
  • 4.1.0
See:
Example
const data = 'PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=';
setDataURLPrefix(data); // data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
setDataURLPrefix(data, 'image/png', false); // data:image/png,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
setDataURLPrefix(data, 'image/jpg'); // data:image/jpg;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
setDataURLPrefix(data, 'text/html'); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
setDataURLPrefix(data, ''); // data:;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
setDataURLPrefix(data, '', false); // data:,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
Parameters:
Name Type Attributes Default Description
data string

数据本身

mimeType string <optional>
"image/png"

MIME 类型

base64 boolean <optional>
true

添加 base64 标识

Returns:

DataURL 格式的字符串

Type
string