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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ty-tech-npm

v0.1.1

Published

前端常用基础工具函数:时间处理、大小写转换等

Readme

ty-tech-npm

前端常用基础工具函数集合,主要面向你自己项目中复用的小工具,例如:

  • 时间处理:格式化、时间差计算等
  • 字符串大小写转换:camelCasesnake_casekebab-casePascalCase
  • 数字格式化:千分位、小数位控制等
  • URL 处理:query 构建与解析、URL 拼接等
  • 对象工具:深拷贝

安装

npm install ty-tech-npm
# 或者
yarn add ty-tech-npm
pnpm add ty-tech-npm

使用示例

时间工具

import { formatDate, toTimestamp, diffDays, now } from "ty-tech-npm";

formatDate(Date.now()); // 默认:YYYY-MM-DD HH:mm:ss
formatDate(new Date(), "YYYY/MM/DD"); // 自定义格式

toTimestamp("2025-01-01 00:00:00"); // number | null

diffDays("2025-01-10", "2025-01-01"); // 9

now(); // 当前时间戳(毫秒)

支持的格式占位符:

  • YYYY:四位年份
  • MM:两位月份
  • DD:两位日期
  • HH:两位小时(24 小时制)
  • mm:两位分钟
  • ss:两位秒
  • SSS:三位毫秒

字符串大小写工具

import {
  toCamelCase,
  toPascalCase,
  toSnakeCase,
  toKebabCase,
  capitalize,
  uncapitalize,
  upperCase,
  lowerCase,
} from "ty-tech-npm";

toCamelCase("hello-world_test"); // helloWorldTest
toPascalCase("hello world"); // HelloWorld
toSnakeCase("helloWorld"); // hello_world
toKebabCase("HelloWorld"); // hello-world

capitalize("hello"); // Hello
uncapitalize("Hello"); // hello

upperCase("hello"); // HELLO
lowerCase("HELLO"); // hello

数字格式化

import { formatNumber, toSafeNumber } from "ty-tech-npm";

formatNumber(1234567.89); // 1,234,567.89
formatNumber(1234.5, { decimals: 2 }); // 1,234.50
formatNumber(1234.5, { decimals: 2, trimTrailingZeros: true }); // 1,234.5
formatNumber("1234.567", {
  decimals: 1,
  thousandSeparator: " ",
  decimalSeparator: ",",
}); // 1 234,6

toSafeNumber("42.3"); // 42.3
toSafeNumber("foo"); // null

URL 工具

import { buildQuery, parseQuery, joinUrl, withQuery } from "ty-tech-npm";

buildQuery({ a: 1, b: "x y", tags: ["a", "b"] });
// a=1&b=x%20y&tags=a&tags=b

parseQuery("?a=1&tags=a&tags=b");
// { a: "1", tags: ["a", "b"] }

joinUrl("https://example.com/", "/api/", "v1/users");
// https://example.com/api/v1/users

withQuery("https://example.com/list?page=1", { page: 2, pageSize: 20 });
// https://example.com/list?page=1&page=2&pageSize=20

对象深拷贝

import { deepClone } from "ty-tech-npm";

const source = {
  a: 1,
  b: { c: 2 },
  list: [1, { x: 3 }],
  createdAt: new Date(),
};

const copy = deepClone(source);
copy.b.c = 999;

console.log(source.b.c); // 2(不会被影响)

API 列表

  • 时间相关

    • formatDate(input, pattern?):格式化时间,返回字符串,非法输入返回空字符串
    • toTimestamp(input):转为毫秒时间戳,非法输入返回 null
    • fromTimestamp(timestamp):从时间戳生成 Date,非法输入返回 null
    • now():当前毫秒时间戳
    • diffMilliseconds(a, b):时间差(毫秒)
    • diffSeconds(a, b):时间差(秒)
    • diffDays(a, b):时间差(天)
  • 字符串相关

    • toCamelCase(input):转为 camelCase
    • toPascalCase(input):转为 PascalCase
    • toSnakeCase(input):转为 snake_case
    • toKebabCase(input):转为 kebab-case
    • capitalize(input):首字母大写
    • uncapitalize(input):首字母小写
    • upperCase(input):全部转大写
    • lowerCase(input):全部转小写
  • 数字相关

    • formatNumber(input, options?):数字格式化,支持千分位、小数位控制等
    • toSafeNumber(input):安全转换为数字,失败返回 null
  • URL 相关

    • buildQuery(params):将对象构建为 querystring(不包含 ?
    • parseQuery(qs):解析 querystring 为对象
    • joinUrl(...parts):拼接 URL 片段
    • withQuery(url, params):在 URL 上追加 query 参数
  • 对象相关

    • deepClone(input, options?):对象深拷贝,支持数组、Date、RegExp、Map、Set、plain object 等

本地开发

# 安装依赖
npm install

# 开发模式(watch 构建)
npm run dev

# 打正式包
npm run build

源码在 src 目录下,index.ts 为统一导出入口。

npm 发布流程(手动)

  1. 确认 package.json 中的 name 已经在 npm 上未被占用
  2. 更新版本号(遵循 semver),例如:
npm version patch   # 或 minor / major
  1. 打包并检查产物:
npm run build
ls dist
  1. 登录并发布:
npm login
npm publish --access public

如果是私有包,可以去掉 --access public,或者配置私有 registry。