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

units-conversion

v1.2.0-beta.0

Published

单位转换

Downloads

6

Readme

units-conversion

快捷帮忙开发替换单位的库,包含时间,内存大小,数值

安装

npm install units-conversion

引入

// ConverTim - 转换时间
// ConverMemory - 转换内存大小
// ConverValue - 转换数值
// UnitConversionFactory - 自定义
import {
  ConverTim,
  ConverMemory,
  ConverValue,
  UnitConversionFactory,
} from "units-conversion";

使用

1、转换值

转换值不带单位时,默认最小转换

ConverTime.converValue("1000"); // {result: { text: '1', unit: 's', value: '1s' }}
ConverTime.converValue("1000s"); // { result: { text: '17', unit: 'min', value: '17min' }}

2、保留小数

toFixed

默认保留 0 位小数

// 默认不保留小数
ConverTime.converValue("1000"); // { result: { text: '17', unit: 'min', value: '17min' }}

// 保留2位小数
ConverTime.converValue("1000s", { toFixed: 2 }); // {result: { text: '16.67', unit: 'min', value: '16.67min' }}

3、末尾 0 抛弃

abandonZero

默认抛弃末尾没有意义的 0, 通常和 toFixed 连用

// 保留末尾的0
ConverTime.converValue("1000", { toFixed: 2, abandonZero: false }); // {result: { text: '1.00', unit: 's', value: '1.00s' }}

4、单位转换

unitType

默认不开启单位转换(使用内置默认配置中的 key 字段名)

ConverTime.converValue("1000", { toFixed: 2, abandonZero: false }); // {result: { text: '1.00', unit: 's', value: '1.00s' }}

ConverTime.converValue("1000", {
  toFixed: 2,
  abandonZero: false,
  unitType: "zh",
}); // {result: { text: '1.00', unit: '秒', value: '1.00秒' }}

函数说明

| 行数名称 | 函数作用 | 说明 | | ------------ | -------- | ------------------------------------------------------ | | ConverTime | 时间转换 | converValue--待转化值,小转大, 可带单位; formateConfig | | ConverMemory | 内存转换 | converValue--待转化值,小转大, 可带单位; formateConfig | | ConverValue | 内存转换 | converValue--待转化值,小转大, 可带单位; formateConfig |

formateConfig 配置说明

| 配置 | 类型 | 说明 | 可选值 | 进度 | | ------------------------- | -------------------- | --------------------------------------------- | ---------------------- | ------ | | formateConfig.converTo | string | 转化到哪一个单位 | | 待完成 | | formateConfig.unitType | string | undefined | 使用什么单位 | undefined|'zh' | 完成 | | formateConfig.valueType | string | undefined | 数值转中文,默认不转 | undefined|'zh' | 完成 | | formateConfig.toFixed | number |undefined | 保留几位小数,默认 0 | undefined|number | 完成 | | formateConfig.abandonZero | boolean | undefined | 是否抛弃小数点和后面的 0, 通常和 toFixed 连用 | undefined|true|false | 完成 |

内置转换单位枚举

  • yb
  • zb
  • pb
  • eb
  • tb
  • gb
  • mb
  • kb
  • b
  • y (年)
  • m(月)
  • w(周)
  • d(天)
  • h(小时)
  • min(分钟)
  • s(秒)
  • ms(毫秒)
  • 千亿
  • 百亿
  • 十亿
  • 亿
  • 千万
  • 百万
  • 十万

UnitConversionFactory 自定义转换类使用说明

import { UnitConversionFactory } from "units-conversion";

let myConver = new UnitConversionFactory([
  { unit: "千亿", zh: "仟", scale: 10 },
  { unit: "百亿", zh: "佰", scale: 10 },
  { unit: "十亿", zh: "拾", scale: 10 },
  { unit: "亿", zh: "亿", scale: 10 },
  { unit: "千万", zh: "仟", scale: 10 },
  { unit: "百万", zh: "佰", scale: 10 },
  { unit: "十万", zh: "拾", scale: 10 },
  { unit: "万", zh: "万", scale: 10 },
  { unit: "千", zh: "仟", scale: 10 },
  { unit: "百", zh: "佰", scale: 10 },
  { unit: "十", zh: "拾", scale: 10 },
  { unit: "", zh: "", scale: 1 },
]);

myConver.converValue("1001百"); // {result: '1十万01百'}
myConver.converValue("1001百", {
  valueType: "zh",
}); // {result: '壹十万零壹百}

版本更新预告

  • 1.2.0
    • 增加日期时间转换?
    • 增加指定转换到
    • 调整代码目录结果
  • 1.1.0 补充说明文档