npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cmss-format

v1.3.1

Published

```bash npm install cmss-format ```

Downloads

15

Readme

cmss-format

Install

npm install cmss-format

Usage

详细使用示例请参见本库test

数字判断

本库将undefinednull""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