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

any-format

v0.15.0

Published

用于数据格式化的工具库

Downloads

11

Readme

any-format 介绍

用于前端常见的数据格式化,如手机号脱敏,金额格式化展示,字符串去除空白字符等

安装

浏览器环境

下载链接 拷贝压缩好的代码到本地的 js 文件引入

<script src="anyFormat.js"></script>
使用 npm
npm install any-format --save
使用 yarn
yarn add any-format
使用 pnpm
pnpm add any-format

使用例子

// 局部导入
import { moneyFormat } from 'any-format';
moneyFormat(12000); //12,000.00

// 导入所有
import * as anyFormat from 'any-format';
anyFormat.moneyFormat(12000); //12,000.00

全部功能示例

desensitizationFormat - 通用脱敏

desensitizationFormat(param1, param2, param3);

入参:param1(string):原字符串 param2(number):起始保留长度 param3(number):结束保留长度,到String.length

例子

import { desensitizationFormat } from 'any-format';

desensitizationFormat('浙江省杭州市', 2, 1); //浙江***市
desensitizationFormat('浙江省杭州市', 2, 0); //浙江****
desensitizationFormat('浙江省杭州市', 0, 2); //****州市

phoneFormat- 手机号脱敏

phoneFormat(param1);

入参:param1(string):手机号字符串

例子

import { phoneFormat } from 'any-format';

phoneFormat('17611112222'); //176****2222

emailFormat - 邮箱脱敏

emailFormat(param1);

入参:param1(string):邮箱字符串

例子

import { emailFormat } from 'any-format';

emailFormat('[email protected]'); //123****@163.com

identityFormat - 身份证号码脱敏

identityFormat(param1);

入参:param1(string):身份证号字符串

例子

import { identityFormat } from 'any-format';

identityFormat('330921199110253510'); //330***********3510

nameFormat - 姓名脱敏

nameFormat(param1, param2, param3);

入参:param1(string):姓名字符串 param2(number):明文展示的长度,其他变为*(默认为1)param3(number):是否倒序隐藏(0:正序,1:倒序,默认为0)

例子

import { nameFormat } from 'any-format'

nameFormat('王爱红') //王**
nameFormat('王爱红', 1) //王**
nameFormat('王爱红', 2) //王爱*
nameFormat('王爱红', 1, 1) //**红
nameFormat('王爱红', 2, 1) //*爱红

bankCardFormat - 银行卡号脱敏

bankCardFormat(param1);

入参:param1(string):银行卡号字符串

例子

import { bankCardFormat } from 'any-format';

bankCardFormat('6212268202014638888'); //'6212 **** **** 8888'

moneyFormat - 金额格式化

moneyFormat(param1, param2);

入参:param1(string | number):金额字符串或者数字 param2(string):分隔符号(默认为,)

例子

import { moneyFormat } from 'any-format';

moneyFormat(12000); //12,000.00
moneyFormat(12000, '~'); //12~000.00

numberFormat - 数字格式化

numberFormat(param1, param2);

入参:param1(string | number):原数据 param2(string):分隔符号(默认为,)

例子

import { numberFormat } from 'any-format';

numberFormat(10000); //10,000
numberFormat(10000, '-'); //10-000

bigNumberTransform - 大额数字格式化

bigNumberTransform(param1);

入参:param1(string | number):大额数据

例子

// 入参:数字值
import { bigNumberTransform } from 'any-format';

bigNumberTransform(100); //100
bigNumberTransform(1000); //1千
bigNumberTransform(10000); //1万
bigNumberTransform(10000000); //1千万
bigNumberTransform(100000000); //1亿

dateFormat - 日期格式化

dateFormat(param1, param2);

入参:param1(string | date):日期字符串或者日期对象 param2(string):格式化字符串(默认yyyy-MM-dd)

例子

import { dateFormat } from 'any-format';

dateFormat(new Date()); //2023-01-03
dateFormat(new Date(), 'yyyy/MM/dd'); //2023/01/03
dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'); //2023-01-03 12:12:12

toCamelFormat - 字符串转驼峰格式

toCamelFormat(param1, param2);

入参:param1(string):非驼峰字符串 param2(string):非驼峰字符串间隔符

例子

import { toCamelFormat } from 'any-format';

toCamelFormat('aa_bb_cc'); //aaBbCc
toCamelFormat('aa-bb-cc', '-'); //aaBbCc

stripFormat - 去空白字符

stripFormat(param1, param2);

入参:param1(string):原字符串 param2(string):默认不传或者传all去除所有空格,trim:去首尾空格,head:去头部空格,tail:去尾部空格

例子

import { stripFormat } from 'any-format';

stripFormat('  abc df '); //'abcdf'
stripFormat('  abc df ', 'trim'); //'abc df'
stripFormat('  abc df ', 'head'); //'abc df '
stripFormat('  abc df ', 'tail'); //'  abc df'

truncateFormat - 字符串截断

truncateFormat(param1, param2, param3);

入参:param1(string):原字符串 param2(number):截断的长度(默认为10) param3(string):截断后拼接的后缀(默认是...)

例子

import { truncateFormat } from 'any-format';

truncateFormat('abcdefghigklmn', 6); //'abcdef...'
truncateFormat('abcdefghigklmn', 100); //'abcdefghigklmn'
truncateFormat('abcdefghigklmn', 6, '等等'); //'abcdef等等'

toUpperCaseFormat - 字符串转大写

toUpperCaseFormat(param1);

入参:param1(string):原字符串

例子

import { toUpperCaseFormat } from 'any-format';

toUpperCaseFormat('world'); //'WORLD'

toLowerCaseFormat - 字符串转小写

toLowerCaseFormat(param1);

入参:param1(string):原字符串

例子

import { toLowerCaseFormat } from 'any-format';

toLowerCaseFormat('WORLD'); //'world'

toLowerLineFormat - 驼峰格式转下划线

toLowerLineFormat(param1);

入参:param1(string):原字符串

例子

import { toLowerLineFormat } from 'any-format';

toLowerLineFormat('isBetter'); //'is_better'
toLowerLineFormat('IsBetter'); //'is_better'

noDataFormat - 空数据格式化

noDataFormat(param1, param2);

入参:param1(string):原字符串 param2(string):替换字符串(默认'')

例子

import { noDataFormat } from 'any-format';

noDataFormat(null); // '';
noDataFormat('null'); // '';
noDataFormat(undefined); // '';
noDataFormat('undefined'); // '';
noDataFormat(undefined, '-'); // '-';
noDataFormat('abc'); // 'abc';

urlParmasFormat - 获取 url 的参数转为对象

urlParmasFormat(param);

入参:param(string):字符串(默认window.location.search.slice(1))

例子

import { urlParmasFormat } from 'any-format';

urlParmasFormat(); // 当前url的参数转为对象;
urlParmasFormat('a=1&b=2'); // {a: '1', b: '2'}

compareVersion - 版本号比较

compareVersion(v1, v2);

入参:v1(string):2.0.0 v2(string):2.1.0

例子

import { compareVersion } from 'any-format';

compareVersion('2.0.0', '2.0.0'); // 0
compareVersion('2.0.0', '2.0'); // 0
compareVersion('2.1.0', '2.0.0'); // 1
compareVersion('2.0.1', '2.0.0'); // 1
compareVersion('2.1.0', '3.0.0'); // -1

timeAgoFormat - 时间间隔展示

compareVersion(dateInput, format);

入参:dateInput(Date/string):new Date format(string):yyyy-MM-dd HH:mm:ss

例子

import { timeAgoFormat } from 'any-format';

const now = new Date();
const dateInput = new Date(now.getTime() - 50 * 1000); // 50 seconds ago
const result = timeAgoFormat(dateInput); // '刚刚'

const now = new Date();
const dateInput = new Date(now.getTime() - 30 * 60 * 1000); // 30 minutes ago
const result = timeAgoFormat(dateInput); // 30分钟前

const now = new Date();
const dateInput = new Date(now.getTime() - 2 * 3600 * 1000); // 2 hours ago
const result = timeAgoFormat(dateInput); // 2小时前

const now = new Date();
const dateInput = new Date(now.getTime() - 1 * 86400 * 1000); // 1 day ago
const result = timeAgoFormat(dateInput); // 1天前

// 大于3天展示具体的日期
const dateInput = new Date(2023, 1, 15); // April 15, 2023 10:30:45
const result = timeAgoFormat(dateInput, 'yyyy-MM-dd'); // '2023-02-15'

贡献者