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 🙏

© 2024 – Pkg Stats / Ryan Hefner

cainfoharbor-utils

v0.0.3

Published

Utils for javascript

Downloads

1

Readme

Utils for javascript

js实用工具库

目录

Install

安装

$ npm i [-S|-D] cainfoharbor-utils

Useage

使用

整体引入

const Utils = require('cainfoharbor-utils');
Utils.zeroFill(6);  // -> 06

最小化引入(推荐)

const zeroFill = require('cainfoharbor-utils/zeroFill');
zeroFill(6);  // -> 06

dateFormat

/**
 * 日期格式化
 * @since 0.0.1
 * @param {string} [format='yyyy-MM-dd HH:mm:ss'] 格式
 * @param {object} [date=new Date] 日期对象, 默认取当前日期对象
 * @return {string} 格式化后的字符串
 */
dateFormat([format='yyyy-MM-dd HH:mm:ss'], [date=new Date]);

Example

dateFormat();                                                         // -> 格式为'yyyy-MM-dd HH:mm:ss'的当前时间
dateFormat(new Date('2017/6/12 6:6:6'));                              // -> 2017-06-11 06:06:06
dateFormat('yyyy-MM-dd HH:mm:ss', new Date('2017/6/12 6:6:6'));       // -> 2017-06-12 06:06:06
dateFormat('yyyy-MM-dd HH:mm:ss vvv', new Date('2017/6/12 6:6:6'));   // -> 2017-06-12 06:06:06 000
dateFormat('yy-M-d h:m:s v a', new Date('2017/6/12 16:6:6'));         // -> 17-6-12 3:6:6 0 pm
dateFormat('yy-M-d h:m:s v A', new Date('2017/6/12 6:6:6'));          // -> 17-6-12 5:6:6 0 AM
dateFormat('OOO dd, yyyy, DDD', new Date('2017/6/12 6:6:6'));         // -> Jun 12, 2017, Mon
dateFormat('O dd, yyyy, D', new Date('2017/6/12 6:6:6')));            // -> June 12, 2017, Monday

格式字符 | 描述 | 返回值示例 -|-|- (yy)yy | (四)两位数的年份完整 | (19)99或(20)03 (M)M | (带前导零)月份 | (0)1-12 (d)d | (带前导零)日期 | (0)1-31 (H)H | (带前导零)24小时制时钟 | (0)0-23 (h)h | (带前导零)12小时制时钟 | (0)1-12 (m)m | (带前导零)分钟 | (0)0-59 (s)s | (带前导零)秒钟 | (0)0-59 (vv)v | (带前导零)毫秒数 | (00)0-999 w | 星期 | 0-6 L | 是否为闰年 | 1或0 a | 小写午前或午后 | am或pm A | 大写午前或午后 | AM或PM (OO)O | (仅前三个字母缩写)英文表达的月份 | Jan(uary)-Dec(ember) (DD)D | (仅前三个字母缩写)英文表达的星期 | Sun(day)-Sat(urday)

优先级顺序

  1. 格式化的优先级顺便按此列表顺序
  2. 带前导零的优先级高过不带前导零
  3. 仅前三个字母缩写的优先级高过全称

numberFormat

/**
 * 数字格式化
 * @since 0.0.1
 * @param {number} num 数字
 * @param {number} [decimals=0] 保留小数位数, 超出则四舍五入取整, 可为负数. 默认0不保留小数
 * @param {string} [thousands_separator=','] 千位分隔符, 默认','
 * @param {string} [decimals_point='.'] 小数点符号, 默认'.'
 * @return {string} 格式化后的字符串
 */
numberFormat(num, [decimals=0], [thousands_separator=','], [decimals_point='.']);

Example

numberFormat(1234567890.0987654321, 0);           // -> 1,234,567,890
numberFormat(1234567890.0987654321, -2);          // -> 1,234,567,900
numberFormat(1234567890.0987654321, 2);           // -> 1,234,567,890.10
numberFormat(1234567890.0987654321, 2, '');       // -> 1234567890.10
numberFormat(1234567890.0987654321, 2, ' ', ','); // -> 1 234 567 890,10

strToDate

/**
 * 字符串转日期对象
 * @since 0.0.1
 * @param {string} str 描述日期的字符串
 * @return {object} 日期对象
 */
numberFormat(str);

Example

strToDate('2017-06-12 16:16:16');     // -> 2017-06-12T08:16:16.000Z
strToDate('a2017b06c12d16e16f16g');   // -> 2017-06-12T08:16:16.000Z
strToDate('20170612161616');          // -> 2017-06-12T08:16:16.000Z
strToDate('201706121616');            // -> 2017-06-12T08:16:00.000Z
strToDate('2017061216');              // -> 2017-06-12T08:00:00.000Z
strToDate('20170612');                // -> 2017-06-11T16:00:00.000Z
strToDate('201706');                  // -> Invalid Date
strToDate('2017');                    // -> Invalid Date

zeroFill

/**
 * 为数字补指定位数的零
 * @since 0.0.1
 * @param {number} num 数字
 * @param {number} [len=2] 目标长度
 * @return {string} 补零后的字符串
 */
zeroFill(num, [len=2]);

Example

zeroFill(0);        // -> 00
zeroFill(6);        // -> 06
zeroFill(8, 3);     // -> 008
zeroFill(10);       // -> 10
zeroFill(10, 4);    // -> 0010
zeroFill(1000, 3);  // -> 1000

License

MIT - cainfoharbor