hantools 常用函数
描述
这是一个工具库
集合了下面几个工具库中最常用的几个函数
npm install dayjs
npm install lodash
npm install qs
npm i merge
npm install mathjs
文档地址
https://blog.youkuaiyun.com/u012258564/article/details/148332621?spm=1011.2415.3001.5331
git 地址
https://gitee.com/hanhanker/hantools.git
安装教程
npm i hantools --save
使用说明
import {isArray} from 'hantools'
具体的方法请进入 src 文件内查看
- arrFun 数组处理
- dateFun 日期处理
- mathFun 数字处理
- calcFun 计算处理
- objFun 对象处理
- storeFun 存储处理
- stringFun 字符串处理
文档
数组处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| isArray | isArray([]) | true | 判断是否是一个数组 |
| arrChunk | arrChunk([1,2,3,4], 2) | [[1,2]], [3,4]] | 将数组进行切分 参数二: 默认为true, 每个切分的数量 |
| arrPage | arrPage([‘Alice’, ‘Bob’, ‘Charlie’, ‘David’, ‘Eve’, ‘Frank’], 2, 3) | {“currentPage”: 2, “data”: [“David”, “Eve”, “Frank”], “pageSize”: 3, “total”: 6, “totalPages”: 2} | 手动分页组件参数一:原数组 参数二:第几页 参数三:每页页数 |
| arrCompact | arrCompact([1, 2, 3, 4, 0, null, undefined]) | [1, 2, 3, 4, 0] | 数组去除假值 null undefined ‘’参数二: 默认为true, 不把0去除 |
| arrUnique | arrUnique([1, 2, 3, 4, 0, 3, null, undefined]) | [[1, 2, 3, 4, 0, null, undefined]] | 数组去重 |
| arrUniqueProp | arrUniqueProp([{id: 1}, {id: 2}, {id: 1}],id) | [{id: 1}] | 数组去重,根据某个属性参数二: 默认为id, 根据id去重 |
| arrSort | arrSort([1, 2, 3, 4, 0, 3, null, undefined]) | [[1, 2, 3, 4, 0, null, undefined]] | 数组排序参数二: 默认为’asc’, 正序排序;‘desc’: 倒序 |
| arrObjSort | arrObjSort([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 0 }, { id: -1 }], ‘id’) | [{“id”: -1}, {“id”: 0}, {“id”: 1}, {“id”: 2}, {“id”: 3}] | 数组中根据对象组成的数组根据某个属性排序参数二:排序的属性 参数三: 默认为’asc’, 正序排序;‘desc’: 倒序 |
| convertToTree | convertToTree([{ id: 2, name: ‘Node 2’, value: 2, parentId: 1 },{ id: 3, name: ‘Node 3’, value: 3, parentId: 2 },{ id: 4, name: ‘Node 4’, value: 4, parentId: 3 },{ id: 5, name: ‘Node 5’, value: 5, parentId: 3 }], ‘id’, ‘parentId’, ‘’, ‘’) | 下面为返回值 | 将扁平数组转换为树形对象 |
| [{“children”: [{“children”: [{“id”: 4, “name”: “Node 4”, “parentId”: 3, “value”: 4}, {“id”: 5, “name”: “Node 5”, “parentId”: 3, “value”: 5}], “id”: 3, “name”: “Node 3”, “parentId”: 2, “value”: 3}], “id”: 2, “name”: “Node 2”, “parentId”: 1, “value”: 2}] | |||
| convertToFlat | convertToFlat([{id: 1, parentId: 0, name: ‘沃尔玛’, childrens: [{id: 2, parentId: 1, name: ‘生鲜区’, childrens: [{ id: 4, parentId: 2, name: ‘鱼’ },{ id: 5, parentId: 2, name: ‘牛肉’ }] }, {id: 3, parentId: 1, name: ‘日用品区’, childrens: [{ id: 6, parentId: 3, name: ‘卫生纸’ },{ id: 7, parentId: 3, name: ‘牙刷’ } ] }]}]) | 下面为返回值 | 树形转扁平 |
| [{“id”: 4, “name”: “鱼”, “parentId”: 2}, {“id”: 5, “name”: “牛肉”, “parentId”: 2}, {“id”: 2, “name”: “生鲜区”, “parentId”: 1}, {“id”: 6, “name”: “卫生纸”, “parentId”: 3}, {“id”: 7, “name”: “牙刷”, “parentId”: 3}, {“id”: 3, “name”: “日用品区”, “parentId”: 1}, {“id”: 1, “name”: “沃尔玛”, “parentId”: 0}] | 注意字段名 |
日期处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| orgDate | orgDate(new Date()) | “2025-05-30” | 日期形成标准的 format 例如:“yyyy/MM/dd” ‘yyyy-MM-dd HH:mm:ss’参数二:返回格式 |
| getDate | getDate(new Date()) | “2025-05-30” | 日期形成标准的yyyy-MM-dd;上面第二个参数传yyyy-MM-dd,快捷方式 |
| getDateTime | getDateTime(new Date()) | “2025-05-30 10:11:12” | 日期时间形成标准的 format ‘yyyy-MM-dd HH:mm:ss' |
| getTimestamp | getTimestamp() | 1748575624271 | 获取时间戳参数一:时间格式 |
| compareDate | compareDate(new Date(), ‘2021-01-01’) | 1 | 比较2个日期的大小返回值 0:相等;1:结束日期 大于 开始日期;-1:结束日期 小于 开始日期 |
| getDateDiffDay | getDateDiffDay(‘2025-01-06’, ‘2026-02-01’) | 391` | 返回相距的天数 |
| getNextDate | getNextDate(‘2025-01-06’, 10) | “2025-01-16” | 获取几天前、几天后的时间参数三:返回的时间格式;默认yyyy-MM-dd |
| getBetweenDate | getBetweenDate(‘2025-01-06’, ‘2025-01-10’) | [“2025-01-06”, “2025-01-07”, “2025-01-08”, “2025-01-09”, “2025-01-10”] | 获得两个日期之间所有的日期 |
| getMonthDate | getMonthDate(‘2025-05’) | [“2025-05-01”, “2025-05-31”] | 获取某个日期第一天和最后一天 |
| getLastDate | getLastDate(-1) | [“2025-05-01”, “2025-05-31”] | 获取当前日期往后或往前天数的月份的第一天和最后一天 |
| getOneYearPastDate | getOneYearPastDate(new Date(), ‘yyyy-MM-dd’, 2) | “2027-05-29” | 获取某个日期几年后的前一天的日期(例子是跨度2年)参数二:返回的格式 参数三:n年后 |
| getMaxDate | getMaxDate([‘2022-01-05’, ‘2025-05-30’, ‘2021-01-09’], ‘max’) | ‘2025-05-30’ | 获取一个时间数组里 最大/最小的时间参数二: ‘max’ / ‘min’ |
| isLeapYear | isLeapYear(‘2025’) | false | 判断是否为闰年 |
| getMonthDays | getMonthDays(‘2025-05’) | 31 | 判断某月多少天 |
数字处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| numberFormat | numberFormat(129099388.838, 2, ‘.’, ‘,’) | ‘129,099,388.84’ | 格式化的数字参数一:要格式化的数字 参数二:保留几位小数 参数三:小数位符号 参数四:千数位符号 |
| numberFormat2 | numberFormat2(129099388.838) | ’129,099,388.84‘ | 上面方法的保留2位的快捷写法 |
| floatToPercent | floatToPercent(0.15) | ’15%‘ | 浮点数转为对应的百分比参数二:默认true, 返回值带% |
| floatToPercent | percentToFloat(15) | 0.15 | 百分比转为对应的浮点数传参兼容’15%’ |
| removeDecimalZero | removeDecimalZero(13.230) | ‘13.23’ | 去掉尾部的0 |
| convertCurrency | convertCurrency() | “壹仟贰佰叁拾肆亿伍仟陆佰柒拾捌万玖仟零伍拾伍元玖角捌分柒毫陆厘” | 把金额转为大写 |
| toWan | toWan(1234567.890) | 123.46 | 数字单位转为万 |
| toThousand | toThousand(1234567.890) | 1234.57 | 数字单位转为千位 |
| is0 | is0(‘0’) | true | 验证是否是0 |
| isNumber | isNumber(1) | true | 验证是否是数字 |
| isEmptyNot0 | isEmptyNot0(null) | true | 验证是否是null, undefined, 空字符串 |
| isEqual | isEqual(1,‘1.0’) | true | 判断两个数字是否相等(考虑浮点数精度问题)` |
计算处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| strip | strip(0.09999999999999998) | 0.1 | 精度取值 |
| plus | plus(0.1, 0.2,0.3) | 0.6 | 加法;可多个值相加 |
| minus | minus(1.0, 0.9) | 0.1 | 减法;可多个值相减 |
| times | times(0.362, 100) | 36.2 | 乘法;可多个值相乘 |
| divide | divide(1.21, 1.1) | 1.1 | 除法;可多个值相除 |
| round | round(0.105, 2) | 0.11 | 保留小数位 |
对象处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| deepClone | 深拷贝 | ||
| isObject | isObject(null) | false | 判断是否是一个对象 |
| validObj | validObj({ a: 1, b: null }, [‘b’]) | false | 判断对象里面的属性是否为空属性b是空的,所以返回false |
| isEmptyObj | isEmptyObj(null) | true | 判断是否是空null,undefined, ''返回true {},[]返回true |
| mergeObj | mergeObj({ a: null, b: { a: 3 } }, { c: 2 }) | {“a”: null, “b”: {“a”: 3}, “c”: 2} | 合并两个对象参数三:是否把合并里未空的字段删除,防止覆盖;默认:false |
| delNullAttr | delNullAttr({ a: null, b: { a: 3, c: ‘’ } }) | {“b”: {“a”: 3}} | 删除对象中为空的属性(不包括空数组和空对象) |
| debounceFun | 函数防抖 参数一:函数; 参数二:延迟执行毫秒数; 参数三:是否立即执行;默认true | ||
| throttleFun | 函数节流参数一:函数 参数二:延迟执行毫秒数 参数三:使用表时间戳,在时间段开始的时候触发 false 使用表定时器,在时间段结束的时候触发;默认为true |
存储处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| setStore | setStore(‘key’, ‘content’) | 设置localStorage参数二:可传递对象/字符串/数字 | |
| getStore | getStore(‘key’) | 获取localStorage | |
| removeStore | removeStore(‘key’) | 删除localStorage | |
| clearStore | clearStore() | 清除所有localStorage | |
| setStoreSession | setStoreSession(‘key’,‘content’) | 设置sessionStorage,下同 | |
| getStoreSession | getStoreSession(‘key’) | 获取sessionStorage | |
| removeStoreSession | removeStoreSession(‘key’) | 删除sessionStorage | |
| clearStoreSession | clearStoreSession() | 清除所有sessionStorage |
字符串处理
| 函数名 | 用法 | 结果 | 描述 |
|---|---|---|---|
| isString | isString(‘abc’) | true | 是否是字符串 |
| strReverse | strReverse(‘abc’) | ‘cba’ | 字符串反转 |
| strTrim | strTrim(’ ccdd ') | ccdd | 字符串去除左右空格 |
| randomString | randomString(8, false) | 获取随机字符串参数一:字符串长度 参数二:是否添加时间戳 | |
| getURLParams | getURLParams(‘?key=123&from=detail’) | {“from”: “detail”, “key”: “123”} | 把url变为一个对象 |
| getObjUrl | getObjUrl({“from”: “detail”, “key”: “123”}) | “from=detail&key=123” | 把对象生成一个url |
| testIdCard | testIdCard(‘123’) | false | 判断是否是一个身份证 |
| countStrToBit | countStrToBit(‘中国’) | 4 | 计算字符串中的字符数 |
| formatValue | formatValue(100,‘元’) | ‘100.00元’ | 格式化返回值,如果是空,返回 '-'参数一:展示的值 参数二:后面的单位 参数三:空值的时候默认值,默认为‘-’ 参数四:数字格式是否格式化,默认为true 此函数主要用于详情页的显示 |
介绍
本项目基于 git 项目二次开发
https://github.com/xiaomingplus/npm-typescript-boilerplate
感谢大佬 xiaomingplus
针对计算公式,整体引入了
number-precision
作者:github.com/nefe/number-precision
文档: https://www.npmjs.com/package/number-precision/v/1.6.0?activeTab=versions
也可使用
floatAdd,floatSub,floatMul,floatDiv 对应加减乘除,不过这4个,只接受2个参数
作者
hanker
hancuiyang@163.com

被折叠的 条评论
为什么被折叠?



