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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hjs-lib/utils

v0.0.9

Published

自用工具函数库

Readme

工具函数库

自用工具函数库

检测类型

可检测任意javascript类型

import { checkType } from "@hjs-lib/utils";

const demo = {
  a: 1,
  b: "text",
  c: {
    id: 1
  },
  d: [1, 2],
  e() {
    return this;
  },
  async f() {
    return demo;
  },
  g: /[^0-9]+/g,
}

checkType(demo.a); // "number";
checkType(demo.b); // "string";
checkType(demo.c); // "object";
checkType(demo.d); // "array";
checkType(demo.e); // "function";
checkType(demo.f); // "asyncfunction";
checkType(demo.g); // "regexp";

判断并推断类型

判断任意值的类型,作用与checkType一致,外加一个辅助功能:当函数返回值为true时,可以传入泛型来确定target的类型(类型收窄)

import { isType } from "@hjs-lib/utils";

type User = {
  id: number
  name: string
}

function setData(params: string | User | Array<User>) {
  if (isType<User>(params, "object")) {
    params.name = "xxx";
  }
  if (isType(params, "array")) {
    params.push({ id: 1, name: "add" });
  }
  // ...do some
}

格式化日期

import { formatDate } from "@hjs-lib/utils";

formatDate(); // 2025-09-26 12:01:32 (当前时间);
formatDate(1603264465956); // 2020-10-21 15:14:25
formatDate(1603264465956, "h:m:s"); // 15:14:25
formatDate(1603264465956, "Y年M月D日"); // 2020年10月21日
formatDate(1603264465956, "日期:Y-M-D"); // 日期:2020-10-21

复制文本

import { copyText } from "@hjs-lib/utils";

copyText("copy text!");

自定义对象数组去重

import { filterRepeat } from "@hjs-lib/utils";

const list = [{ id: 10, code: "abc" }, {id: 12, code: "abc"}, {id: 12, code: "abc"}];

filterRepeat(list, (a, b) => a.id === b.id);

将深嵌套层key的对象转换成标准结构对象

import { filterRepeat } from "@hjs-lib/utils";

const target = {
  id: 12,
  "info.name": "sub-1",
  "info.price": 12,
  "info.desc.date": "2024-12-22",
  content: "this is a text!"
}
const result = formatDeepKeyObj(target);
// 输出为正确结构的对象
console.log(result);

// 输出结果:
// {
//   "id": 12,
//   "info": {
//     "name": "sub-1",
//     "price": 12,
//     "desc": {
//       "date": "2024-12-22"
//     }
//   },
//   "content": "this is a text!"
// }

获取嵌套对象的值

import { getValueByDeepKey } from "@hjs-lib/utils";

const obj = {
  info: {
    value: "content"
  },
  id: 12
};

getValueByDeepKey(obj, "info.value"); // "content"
getValueByDeepKey(obj, "id"); // 12

设置嵌套对象的值

import { setValueByDeepKey } from "@hjs-lib/utils";

const obj = {};

setValueByDeepKey(obj, "info.value", "content");
setValueByDeepKey(obj, "id", 99);

console.log(obj);
// 结果:
// obj = {
//   info: {
//     value: "content"
//   },
//   id: 99
// };

获取url?后面参数(JSON对象)

import { getLinkQuery } from "@hjs-lib/utils";

// 当前网址为 www.https://hjs.com?id=99&age=123&key=abc
const current = getLinkQuery();
// 输出: { id: "99", age: "12", key: "abc" }

const params = getLinkQuery("id=12&version=1.4.3&name=hjs");
// 输出: { id: "12", version: "1.4.3", name: "hjs" }

对象转选项列表

import { toMapOptions } from "@hjs-lib/utils";

const map = { 1: "价格", "goods": "商品", "game": "游戏" };

const options = toMapOptions(map);

console.log(options);

// 输出结果:
// [
//   { label: "价格", value: 1, disabled: false },
//   { label: "商品", value: "goods", disabled: false },
//   { label: "游戏", value: "game", disabled: false }
// ]

格式化数字

注意,不会对小数位进行四舍五入

import { formatNumber } from "@hjs-lib/utils";

formatNumber(123456789); // "123,456,789.00"
formatNumber(102.888); // "102.88"
formatNumber(102.999, 1); // "102.9"
formatNumber(188.6666, 3); // "188.666"

数字计算

解决javascript的精度运算问题,可链式调用;

import { computeNumber } from "@hjs-lib/utils";

const res = computeNumber(1.3, "-", 1.2).next("+", 1.5).next("*", 2.3).next("/", 0.2).result;
console.log(res); // 18.4

const res2 = computeNumber(1.3, "-", 1.2).result;
console.log(res2); // 0.1

小数点进位

  • 应用场景:商品价格100,用了优惠券结算价格为33.333333...,取小数点两位则是33.33
  • 如果有1000个人都以33.33去结算的话,那么最终就会损失3块钱,以此类推;
  • 所以该方法就是在小数取位后面补1,像这样:
const res1 = computeNumber(100, "/", 3).toUp(2);
console.log(res1); // 33.34

const res2 = computeNumber(166, "/", 100).toUp(2);
console.log(res2); // 1.66

const res3 = computeNumber(1212, "/", 100).toUp(1);
console.log(res3); // 12.2