自用工具函数库
npm install @hjs-lib/utilsjavascript类型
js
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的类型(类型收窄)
`ts
import { isType } from "@hjs-lib/utils";
type User = {
id: number
name: string
}
function setData(params: string | User | Array) {
if (isType(params, "object")) {
params.name = "xxx";
}
if (isType(params, "array")) {
params.push({ id: 1, name: "add" });
}
// ...do some
}
`
格式化日期
`js
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
`
复制文本
`js
import { copyText } from "@hjs-lib/utils";
copyText("copy text!");
`
自定义对象数组去重
`js
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的对象转换成标准结构对象
`js
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!"
// }
`
获取嵌套对象的值
`js
import { getValueByDeepKey } from "@hjs-lib/utils";
const obj = {
info: {
value: "content"
},
id: 12
};
getValueByDeepKey(obj, "info.value"); // "content"
getValueByDeepKey(obj, "id"); // 12
`
设置嵌套对象的值
`js
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对象)
`js
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" }
`
对象转选项列表
`js
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 }
// ]
`
格式化数字
> 注意,不会对小数位进行四舍五入
`js
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的精度运算问题,可链式调用;
`js
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,像这样:
`js
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
``