1.3.1 • Published 5 years ago
cmss-format v1.3.1
cmss-format
Install
npm install cmss-format
Usage
详细使用示例请参见本库test。
数字判断
本库将undefined
、null
、""
、 isNaN
都视为非数字。
方法 | 返回值 | 说明 |
---|---|---|
isBadNumber(value) | true/false | undefined/null/""/ isNaN, 都视为非数字 |
isBadNumber
数字格式化
数字格式化延续isBadNumber的思路,非数字统一返回null。 若精度precision为isBadNumber,返回值为digital(value)。
方法 | 返回值 | 说明 |
---|---|---|
digital(value) | null/数字 | 非数字返回null |
round(value, precision) | null/数字 | 四舍五入;非数字返回null |
roundZero(value, precision) | null/数字 | 四舍五入;按精度强制补0 |
numberAbbr(value, precision, options) | {number:123.46,unit:'万' } | 数字单位自适应,如1234567,缩写为123.46w 或123.46万 |
numberAbbrByUnit(value, precision, targetUnit, options) | 12.3456 | 数字转换为某一数量级单位,如123456转换为万,得到12.3456 |
-- | -- | -- |
numberUnitAdapt(value, precision, sourceUnit, options) | {number:1.23457,unit:'MB' } | 数字指标单位自适应,如1234567B自适应为1.23457MB |
numberByUnit(value, precision, sourceUnit, targetUnit, options) | 1.23457 | 数字转换为某一目标单位,如1234567B转换为MB |
digital
round
roundZero
numberAbbr
numberAbbrByUnit
参数 | 说明 | 必填 | 示例 |
---|---|---|---|
value | 值 | 是 | 1234567 |
precision | 精度: 0则取整,isBadNumber则返回digital(value) | 2 | |
targetUnit | 目标单位。与options对应。 | 万\千万 | |
options | 用户自定义单位规则 |
options示例:
// 默认
const options = {
steps: [1000, 10000, 1000000, 10000000, 100000000],
units: ['千', '万', '百万', '千万', '亿'],
};
// 用户自定义
const options = {
steps: [1000, 10000, 1000000, 10000000, 100000000],
units: ['千', '万', '百万', '千万', '亿'],
};
或者
const options = {
steps: [1000, 10000, 1000000],
units: ['k', 'w', 'bw'],
};
numberUnitAdapt
numberByUnit
参数 | 说明 | 必填 | 示例 |
---|---|---|---|
value | 值 | 是 | 1234567 |
precision | 精度: 0则取整,isBadNumber则返回digital(value) | 2 | |
sourceUnit | 原始单位。与options对应。 | MB或者MB/s | |
targetUnit | 目标单位。与options对应。 | TB或者TB/s | |
options | 用户自定义单位 |
options示例:
// 默认
const options = {
step: 1000,
units: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
};
const options = {
step: 1024,
units: ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s', 'PB/s', 'EB/s', 'ZB/s', 'YB/s'],
};
// 用户自定义
const options1 = {
step: 1024,
units: ['Bps', 'KBps', 'MBps', 'GBps', 'TBps', 'PBps', 'EBps', 'ZBps', 'YBps'],
};
const options2 = {
step: 2,
units: ['a', 'b', 'c', 'd', 'e'],
};
注意:本库借鉴了 numeral
思想。但 numeral
①只能以B
作为原始单位,在B
的基础上实现自适应; ②numeral
将数值和单位拼接为字符串一起返回,但有时将{value, unit}分开返回更符合实际需要。
场景举例:在折线图数据点集合(原始单位为MB),取一数据点A,进行单位自适应得到单位GB(正向自适应)。而后,采用GB作为目标单位,将折线图集合中全部的点都格式化为GB单位下的数值。最终实现数据集合点单位的统一转换。
时间
本库基于momentjs
做常用封装。time可以是string/number/timestamp/monent任意格式。
方法 | 返回值 | 说明 |
---|---|---|
formatTimeMoment(time) | moment | 时间统一格式化为moment |
getTimeLine(startTime, endTime, dim = 'month', step = 1) | list | 获取时间轴列表 |
formatTimeByDim(time, dim = 'day') | string | 时间按维度格式化 |
getTimeRecentByMinutes(time, minutes = 15) | moment | 获取与指定时间最近且不超过指定时间的5/15分钟等分钟粒度的时间 |
getHoursLine(time, hourCount = 24) | list | 获取若干小时的小时时间轴,默认一天 |
getMinutesLine(time, minutes = 15, minuteCount = 96) | string | 获取若干小时的分钟时间轴,默认一天 |
formatTimeMoment
getTimeLine
formatTimeByDim
getTimeRecentByMinutes
getHoursLine
getMinutesLine
其他
方法 | 返回值 | 说明 |
---|---|---|
echartTooltip(params, unit, showIcon = true) | string | echarts tooltip格式化 |
renderReportColumns(keys, info = {}, orderInfo) | list | antd table columns格式化生成 |
echartTooltip
renderReportColumns
相关库
数字格式化参考:numeraljs 、lodash
时间格式化参考:moment