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

format-tools

v0.0.7

Published

the formatter with multiple function for javascript

Downloads

7

Readme

format-tools

Build Status

NPM

一个简单的格式化工具;包含格式化日期时间、数字、字符串的几个常用方法;

安装

node

$ npm install format-tools

浏览器

<script src='./dist/index.js'></script>

以amd或cmd模式引入,如果环境不支持amd或cmd,挂载到window上,window.formatTools;

点击此处查看测试页面

  // seajs测试
  seajs.use('formatTools', function (formatTools) {
    console.log(formatTools);
  });
  
  // requirejs 测试
  requirejs(['formatTools'], function (formatTools) {
    console.log(formatTools);
  });
  
  // 浏览器原生
  console.log(window.formatTools);

用法

引用

var formatTools = require('format-tools'); // node环境中的require引用、浏览器环境
import formatTools from 'format-tools/index'; // es6的import引用

format(str, args)

一个简单的字符串格式化工具,支持下标和属性两种方式,属性名称为数字、字母和下划线的组合;

  • str:模板字符串
  formatTools.format('{0}-{1}-{2}', 2018, '08', 10); // => '2018-08-10'
  formatTools.format('{year}-{month}-{day}', {year:2018, month:'08', day: 10}); // => '2018-08-10'

formatDate(date, splitChar)

格式化日期,返回日期部分;有两个参数:

  • date:date|number|string,支持日期对象、时间戳;
  • splitChar: 可选,日期的分隔符号,默认为 '-'
  formatTools.formatDate(new Date(2018, 9, 10, 12, 34, 56)); // => '2018-10-10'
  formatTools.formatDate(1539146096000, '/'); // => '2018/10/10'

formatTime(date)

格式化日期,返回时间部分;一个参数:

  • date:date|number|string,支持日期对象、时间戳;
  formatTools.formatTime(new Date(2018, 9, 10, 12, 34, 56)); // => '12:34:56'
  formatTools.formatTime(1539146096000); // => '12:34:56'

formatDateTime(date, splitChar)

格式化日期时间,返回日期+时间;有两个参数:

  • date:date|number|string,支持日期对象、时间戳;
  • splitChar: 可选,日期的分隔符号,默认为 '-'
  formatTools.formatDateTime(new Date(2018, 9, 10, 12, 34, 56)); // => '2018-10-10 12:34:56'
  formatTools.formatDateTime(1539146096000, '/'); // => '2018/10/10 12:34:56'

formatByThousand(num)

格式化数字,金额用到的时候较多,三位加一个千分位符号,参数:

  • num: number|string,需要格式化的数字;
  formatTools.formatByThousand(1234567); // => '1,234,567'
  formatTools.formatDateTime('1234.56'); // => '1,234.56'
  1234.56.toLocaleString('en-us'); // => '1,234.56'

split4(str)

格式化字符串,四位加一个空格,银行卡号用到的较多;

  • str: number|string,需要格式化的字符串;
  formatTools.split4(6225880112345678); // => '6225 8801 1234 5678'

getType(args)

获取参数类型字符串(小写);

  • args: String|Object|Number|Date|null|undefined,需要格式化的字符串;
    formatTools.getType(new Date()); // => 'date'
    formatTools.getType(123); // => 'number'
    formatTools.getType('test'); // => 'string'
    formatTools.getType({a: 1}); // => 'object'
    formatTools.getType(null);  // => 'null'
    formatTools.getType(undefined); // => 'undefined'