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

configjs-utilsjs

v5.1.1

Published

工具类

Downloads

161

Readme

configjs-utilsjs

介绍

configjs-utilsjs 是纯 js 方法库, 包含:时间转换,数字转换千分符等。

特性

  • 纯 js

更新日志

[5.1.0]

  • 新特性
    • 新增isDate(判断日期是否合法)方法

[5.0.1]

  • 优化
    • 调整一些方法

[5.0.0]

  • 新特性
    • 补充d.ts类型说明文件
  • 删除
    • 删除 formatMoney 方法
    • 删除 generateDateShortcuts 方法

[4.5.0]

  • 优化
    • 修改appendLink(body 添加 link 标签)方法参数类型可传数组,同时修改返回值为Promise
    • 修改appendScript(body 添加 js 标签)方法参数类型可传数组,同时修改返回值为Promise

[4.4.0]

  • 新特性
    • 新增getURLSearchParams(获取网址的参数,该方法调用 WEB API > URLSearchParams 方法)方法

[4.3.1]

  • Bug 修复
    • 修复formatThousands(千分符)方法接入的参数非法报错的情况,修复后该方法直接输出非法的参数,只有数字或数字字符串才执行后续方法

[4.3.0]

  • 其他
    • 取消 main、other 目录区分,如需要按需引入直接引用对应的文件或者可以选择 babel-plugin-import 等插件

[4.2.0]

[4.1.0]

[4.0.0]

  • 其他
    • 新增 other 目录,里边为一些不常用的方法
  • 新特性
    • other 目录新增formatRgbToHex(格式化颜色 RGB 为 HEX)方法
    • other 目录新增generateHex(生成hex类型的随机颜色)方法
    • other 目录新增getUrlQuery(获取网址的参数)方法
    • other 目录新增isDarkMode(判断设置主题是否为黑暗模式)方法
    • other 目录新增isNumber(判断数字是否合法)方法

[3.4.0]

[3.3.0]

  • 新特性
  • 优化
    • 修改formatTimes(时间转换)方法的 times 参数的默认值,改为传入非法值则不处理直接输出

[3.2.0]

  • 新特性
  • 优化
    • 修改formatTimes(时间转换)方法的 times 参数的默认值,改为必填

[3.1.1]

  • 优化
    • 修改formatThousands(千分符)方法的 decimalPlaces 参数规则,不填时不进行处理,为 0 时返回整数
  • Bug 修复
    • 修复formatThousands(千分符)方法出现的四舍五入精度丢失的情况

[3.1.0]

  • 新特性
  • 优化
    • 修改 代码结构
  • 其他

[3.0.1]

  • 新特性
  • 删除
    • 删除 formatStringDateTimes 方法,可通过formatTimes(时间转换)方法实现

[2.1.4]

  • 优化
    • 修改 千分符的方法,从 number 转 string,解决 number 长度问题

[2.1.3]

  • 优化
    • 修改 打包架构,配置按需加载功能

[1.1.2]

  • Bug 修复
    • 修复 千分符方法 bug ~ 修复 首字母大写 bug

[1.0.9]

  • 优化
    • 修改 formatMoney 方法新增 separators(分隔符) 参数

安装

# npm:
npm i configjs-utilsjs -S

引入方法

方式一. 自动按需引入 (推荐)

babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式。

// 安装插件
npm i babel-plugin-import -D

// 在 babel.config.js 中配置
plugins: [
    [
        'import',
        {
            libraryName: 'configjs-utilsjs',
            libraryDirectory: 'lib',
            camel2DashComponentName: false
        },
        'configjs-utilsjs'
    ]
];

// 接着你可以在代码中直接使用
import { formatTimes } from 'configjs-utilsjs';

const time = formatTimes(new Date(), 'yyyy-MM-dd');
console.log(time);

方式二. 手动按需引入

import formatTimes from 'configjs-utilsjs/lib/formatTimes';

const time = formatTimes(new Date(), 'yyyy-MM-dd');
console.log(time);

方式三. 导入所有

import configjsUtilsjs from 'configjs-utilsjs';

const time = configjsUtilsjs.formatTimes(new Date(), 'yyyy-MM-dd');
console.log(time);

文档

appendLink(body 添加 link 标签)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | files | 添加的链接/链接列表 | string / string[] | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 添加结果 | Promise<unknown> |

appendScript(body 添加 js 标签)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | files | 添加的链接/链接列表 | string / string[] | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 添加结果 | Promise<unknown> |

capitalize(首字母大写)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 需要转换的内容 | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 转换后的字符串 | string |

compareVersion(版本比较)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | v1 | 被比较的版本1 | string | — | — | | v2 | 被比较的版本2 | string | — | — | | separators | 版本号分隔符 | string | — | '.' |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 返回值 0: v2 > v1 1:v1 > v2 2: v1 = v2 | 0 / 1 / 2 |

deepCopy(深拷贝)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | data | 被拷贝值 | T | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 拷贝出来的值 | T |

dictionaryClassification(字典分类)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | data | 需要分类的值 | string[] | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 分类后的值 | {[propName: string]: string[]} |

dictionarySort(字典排序)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | data | 需要排序的值 | string[] | — | — | | type | 排序类型,0:默认 1:比默认多A-Z值 | 0 / 1 | — | 0 |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 排序后的值 | string[] |

formatMoneyUppercase(金额大写)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | amount | 金额数值 | number | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 大写的金额 | string |

formatRgbToHex(格式化颜色 RGB 为 HEX)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | r | rgb中的r,可选值为0~255 | number | 0~255 | — | | g | rgb中的g,可选值为0~255 | number | 0~255 | — | | b | rgb中的b,可选值为0~255 | number | 0~255 | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 格式化后的颜色 | string |

formatRound(四舍五入)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 需要四舍五入值 | string / number | — | — | | decimalPlaces | 数值保留小数位 | number | — | 0 |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 转换后的字符串 | string |

formatThousands(千分符)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 需要转千分符的值 | string / number | — | — | | separators | 千分符号 | string | — | ',' | | decimalPlaces | 数值保留小数位 | number / undefined | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 转换后的字符串 | string |

formatTimes(时间转换)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | times | 时间 | Date[] / number[] / string[] / Date / number / string | — | — | | format | 时间转换类型,具体写法看详情 | string | — | 'yyyy-MM-dd hh:mm:ss' |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 转换后的数据 | string[] / string / number[] / number |

generateDateTimes(根据开始结束时间生成连续的时间数组)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | startTime | 开始时间 | Date | — | — | | endTime | 结束时间 | Date | — | — | | type | 根据时间中的值来生成数组,具体区别看详情,可选值为'y',"M","d" | 'y' / 'M' / 'd' | 'y',"M","d" | 'd' | | format | 时间转换类型,具体写法看详情 | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 转换后的数据 | string[] / Date[] / number[] |

generateHex(生成hex类型的随机颜色)

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | hex类型的随机颜色 | string |

generateRandomString(生成随机字符串)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | length | 生成长度 | number | — | 16 | | chars | 随机值,默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1 | string | — | 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 生成的字符串 | string |

getUnicode(unicode解码工具)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 需要解码的值 | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 解后的码 | string |

getUrlQuery(获取网址的参数)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | url | 网址 | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 网址的参数 | { [index: string]: string / string[] } |

getURLSearchParams(获取网址的参数,该方法调用 WEB API > URLSearchParams 方法)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 网址或者网址参数的字符串,如 "http://localhost:8080?a=1" 或者 "?a=1&a=3&a=2" | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 网址的参数 | { [index: string]: string / string[] } |

isDarkMode(判断设置主题是否为黑暗模式)

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 判断结果 | boolean |

isDate(判断日期是否合法)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | val | 参数 | array | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 判断结果 | boolean |

isNumber(判断数字是否合法)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | value | 需要判断的值 | any | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 判断结果 | boolean |

moneyUnitConversion(金额的单位转换功能)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | money | 金额 | number | — | — | | decimal | 保留的小数 | number | — | 0 |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | [转换后的金额, 金额单位] | [number, string] |

setUnicode(unicode加密工具)

属性 attr

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | :----: | :----: | :----: | :----: | :----: | | str | 需要加密的值 | string | — | — |

返回 return

| 参数 | 说明 | 类型 | | :---: | :---: | :---: | | value | 解码后的内容 | string |

formatTimes-foramt 详情说明

| 参数 | 说明 | 范围 | | :---: | :----: | :---: | | y | 年 | 1~4 | | M | 月 | 1~2 | | d | 日 | 1~2 | | h | 小时 | 1~2 | | m | 分 | 1~2 | | s | 秒 | 1~2 | | q | 季度 | 1~2 | | S | 毫秒 | 1 | | t | 时间戳 | 1 | | w | 星期 | 1 |

联系方式

链接