Processor

数据处理
Source:
Since:
  • 1.1.0

Methods

(static) formatBankCard(str, optionsopt) → {String}

Source:
Since:
  • 1.1.0
格式化银行卡号
Example
// 19位银行卡
formatBankCard('6228480402564890018');
// => 6228 4804 0256 4890 018

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

// 16位银行卡,"-"间隔
formatBankCard('6228480402564890', {char: '-'});
// => 6228-4804-0256-4890
Parameters:
Name Type Attributes Description
str String 要处理的字符串
options Object <optional>
配置项
Properties
Name Type Attributes Default Description
char String <optional>
' ' 间隔字符
length Number <optional>
4 间隔长度
Returns:
格式化的银行卡号
Type
String

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

Source:
Since:
  • 1.1.0
格式化金额
Example
// 整数
formatMoney('1000');
// => 1,000.00

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

// 保留4位小数
formatMoney('3000.0300', { 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, { thousand: '&' });
// => 1,000&00
Parameters:
Name Type Attributes Description
num String | Number 需转换金额 (最大:9007199254740991 最小: -9007199254740991)
options Object <optional>
金额格式化配置
Properties
Name Type Attributes Default Description
precision String | Number <optional>
2 保留位数 (最高:10位)
symbol String <optional>
货币符号
thousand String <optional>
, 千分位符号
decimal String <optional>
. 小数位符号
Returns:
格式化的金额
Type
String

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

Source:
Since:
  • 1.2.0
数字转中文数字 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
Example
numberToChinese(100);
// => 一百

numberToChinese(100.3);
// => 一百点三

// 繁体
numberToChinese(100, {big5: true});
// => 壹佰

numberToChinese(100.3, {big5: true});
// => 壹佰点叁

numberToChinese(1234567890, {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 计数单位
decomal 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) replaceChar(str, optionsopt) → {String}

Source:
Since:
  • 1.1.0
替换字符,应用场景如:脱敏
Example
// 手机号
replaceChar('13000000000');
// => 130****0000

// 身份证
replaceChar('130701199310302288');
// => 130***********2288

// 邮箱
const email = '12345@qq.com'
replaceChar(email, {start: 2, end: email.indexOf('@'), repeat: 4});
// => 12****@qq.com

// 银行卡号
replaceChar('6228480402564890018', {start: 0, end: -4, repeat: 4});
// => ****0018

// 带格式的银行卡号
replaceChar('6228 4804 0256 4890 018', {start: 4, end: -4, exclude: ' '});
// => 6228 **** **** **** 018

// 用户名
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