@jsir_z/fast-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

fast-utils

整理了一些自已常用的小工具函数、对象等。

安装教程

yarn add @jsir_z/fast-utils

or

npm install @jsir_z/fast-utils

使用文档

arrayUtils [Object] 数组工具

  • copy [Function] 根据配置复制一个数组

参数说明

参数 类型 必填
arr object[] true
config object false

示例

import {arrayUtils} from '@jsir_z/fast-utils';
const arr = [
    {
        name: '张三',
        id: 1
    },
    {
        name: '李四',
        id: 2
    }
];
const newArr = arrayUtils.copy(arr, {
    id: 'value',
    name: 'label'
});
console.log(newArr);
//   [
//        {
//            "value": 1,
//            "label": "张三"
//        },
//        {
//            "value": 2,
//            "label": "李四"
//        }
//    ]
  • unique [Function] 去重

参数说明

参数 类型 必填
arr any[] true
key string false

示例

import {arrayUtils} from '@jsir_z/fast-utils';
//对象数组去重
const arr1 = [
    {
        name: '李四',
        id: 1
    },
    {
        name: '李四',
        id: 1
    }
];
const result = arrayUtils.unique(arr1, 'name');
console.log(result);
// [
// 	  {
// 		  "name": "李四",
// 	  	  "id": 1
// 	  }
// ]

//普通数组去重
const arr2 = [1, 1, 2, 3, 1, 3, 4];
const result = arrayUtils.unique(arr2);
console.log(result);
//result
//[1,2,3,4]
  • flat [Function] 扁平化数组

参数说明

参数 类型 必填 默认值
arr any[] true -
childrenKey string false children

示例

import {arrayUtils} from '@jsir_z/fast-utils';
const arr = [
    {
        title: '西安市',
        child: [
            {
                title: '未央区',
                child: []
            }
        ]
    }
];
const result = arrayUtils.flat(arr, 'child');
console.log(result);
//result
//{
//     "title": "西安市",
//     "child": [
//         {
//             "title": "未央区",
//             "child": []
//         }
//     ]
// }
  • find [Function] 从指定数组中查找

参数说明

参数 类型 必填
arr any[] true
value any true
key string false

示例

import {arrayUtils} from '@jsir_z/fast-utils';

//对象数组根据key-value查找
const arr = [
    {
        title: '西安市',
        child: []
    },
    {
        title: '宝鸡市',
        child: []
    }
];
const result = arrayUtils.find(arr, '西安市', 'title');
console.log(result);
//result
//{
//     "result": {
//         "title": "西安市",
//         "child": []
//     },
//     "index": 0
// }

//普通数组查找
const arr2 = ['西安市', '宝鸡市'];
const result = arrayUtils.find(arr2, '西安市');
console.log(result);
//result
//{
//     "result": "西安市",
//     "index": 0
// }

cookieUtils [Function] cookie 工具

  • setCookie [Function] 设置 cookie

参数说明

参数 类型 必填
key string true
value string true
expire number true

示例

import {cookieUtils} from '@jsir_z/fast-utils';

cookieUtils.setCookie('token', 'asdakjshdashdkajshd', 1000 * 60 * 60);
  • getCookie [Function] 获取 cookie

参数说明

参数 类型 必填
key string true

示例

import {cookieUtils} from '@jsir_z/fast-utils';

cookieUtils.getCookie('token');
  • deleteCookie [Function] 删除 cookie

参数说明

参数 类型 必填
key string true

示例

import {cookieUtils} from '@jsir_z/fast-utils';

cookieUtils.deleteCookie('token');

dateUtils [Object] 时间工具

特别说明 该对象下的所有方法支持链式调用 主要是为了保持 moment.js 的使用习惯

  • moment [Function] 获取当前时间

参数说明

参数 类型 必填
key timeString | Date false

示例

import {dateUtils} from '@jsir_/fast-utils';

1;
const time = new Date();
const now = dateUtils.moment(time);
console.log(now);
//now
//Wed Jun 15 2022 13:45:26 GMT+0800 (中国标准时间)

2;

const time = dateUtils.moment('2020-12-10 17:11:22');
console.log(time);
//time
//Thu Dec 10 2020 17:11:22 GMT+0800 (中国标准时间)
  • format [Funtion] 格式化时间

参数说明

| 参数 |类型| 必填 | 默认值 | -- | :--: | :--: || | type| string | false | YYYY-MM-DD HH:mm:ss|

匹配说明

类型
YY
MM
DD
HH 时(24 小时制)
hh 时(12 小时制)
mm
ss

使用

import {dateUtils} from '@jsir_/fast-utils';

const time1 = dateUtils.moment('2020-12-10 17:11:22').format('YYYY/MM/DD HH:mm:ss');
console.log(time);
//time1
//2020/12/10 17:11:22

const time2 = dateUtils.moment('2020-12-10 17:11:22').format('YYYY/MM/DD hh:mm:ss');
console.log(time);
//time
//2020/12/10 05:11:22
  • dayStart [Function] 某一天的开始时间

使用

import {dateUtils} from '@jsir_/fast-utils';

const time = dateUtils.moment('2020-12-10 17:11:22').dayStart();
console.log(time);
//time
//Thu Dec 10 2020 00:00:00 GMT+0800 (中国标准时间)
time.format('YYYY-MM-DD HH:mm:ss'); // 2020-12-10 00:00:00
  • dayEnd [Function] 某一天的结束时间

使用

import {dateUtils} from '@jsir_/fast-utils';

const time = dateUtils.moment('2020-12-10 17:11:22').dayEnd();
console.log(time);
//time
//Thu Dec 10 2020 23:59:59 GMT+0800 (中国标准时间)
time.format('YYYY-MM-DD HH:mm:ss'); // 2020-12-10 23:59:59
  • add [Function] 向后获取某个时间

参数说明

参数 类型 必填
num number true
unit Unit(单位) true

单位取值说明

类型
y
q 季度
M
d
w
m 分钟
h 小时
s
ms 毫秒

使用

import {dateUtils} from '@jsir_z/fast-utils';

//向后一年
const time = dateUtils.moment('2020-01-01 12:00:00').add(1, 'y').format('YYYY-MM-DD HH:mm:ss');
console.log(time);
//time
//2021-01-01 12:00:00

//向后一季度
const time = dateUtils.moment('2020-01-01 12:00:00').add(1, 'q').format('YYYY-MM-DD HH:mm:ss');
console.log(time);
//time
//2020-04-01 12:00:00

//向后两个月

const time = dateUtils.moment('2020-01-01 12:00:00').add(2, 'M').format('YYYY-MM-DD HH:mm:ss');
console.log(time);
//time
//2020-03-01 12:00:00

//向后一天
const time = dateUtils.moment('2020-01-01 12:00:00').add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
console.log(time);
//time
//2020-01-02 12:00:00

//向后一小时

const time = dateUtils.moment('2020-01-01 12:00:00').add(1, 'h').format('YYYY-MM-DD HH:mm:ss');
console.log(time);
//time
//2020-01-01 13:00:00
  • subtract [Function] 向前获取时间

参数、使用同dateUtils.add

  • distance [Function] 获取时间间隔

使用

import {dateUtils} from '@jsir_z/fast-utils';

const time = dateUtils.moment().format('YYYY-MM-DD HH:mm:ss'); //now
const time1 = dateUtils.moment('2020-01-01 12:00:00').distance();
const time2 = dateUtils.moment('2022-01-01 12:00:00').distance();
const time3 = dateUtils.moment('2022-06-15 12:00:00').distance();
const time4 = dateUtils.moment('2022-06-15 14:00:00').distance();
const time5 = dateUtils.moment('2022-06-15 14:26:00').distance();
const time6 = dateUtils.moment('2022-06-16 14:26:00').distance();
console.log({time, time1, time2, time3, time4, time5, time6});
// time: "2022-06-15 14:26:01"
// time1: "2年前"
// time2: "5月前"
// time3: "2小时前"
// time4: "26分钟前"
// time5: "1秒前"
// time6: "23小时后"
  • getYears [Function] 获取当前年份
  • value [Function] 获取当前毫秒值

使用

import {dateUtils} from '@jsir_z/fast-utils';

dateUtils.moment().getYears(); //2022
dateUtils.moment().value(); //1655274639905
  • getMonthes [Function] 获取当前月份
  • getDays [Function] 获取当前日期
  • getMinute [Function] 获取当前分钟
  • getSecond [Function] 获取当前秒数

参数说明

参数 类型 必填 默认值 说明
prefix boolean false false 隐藏 10 位数的 0

使用

import {dateUtils} from '@jsir_z/fast-utils';

const M = dateUtils.moment().getMonthes(true);
const d = dateUtils.moment().getDays();
const m = dateUtils.moment().getMinute();
const s = dateUtils.moment().getSecond();
console.log(M, d, m, s);
//6 15 07 37

treeUtils [Object] 树形结构数据的操作

  • flatTreeData [Function] 扁平化树形结构

参数说明

参数 类型 必填 说明
treeData any[] true 数据源
childrenKey string false 子集的 key 默认为 children

使用

const dataSource = [
    {
        name: '陕西省',
        id: '1',
        children: [
            {
                name: '西安市',
                id: '1-1',
                children: [
                    {
                        name: '未央区',
                        id: '1-1-1'
                    }
                ]
            },
            {
                name: '宝鸡市',
                id: '1-2',
                children: [
                    {
                        name: '陈仓区',
                        id: '1-2-1'
                    }
                ]
            }
        ]
    },
    {
        name: '山西省',
        id: '2',
        children: [
            {
                name: '大同市',
                id: '2-1',
                children: []
            }
        ]
    }
];
const result = treeUtils.flatTreeData(dataSource);
console.log(result);
//result
//[
//     {
//         "name": "陕西省",
//         "id": "1",
//         "children": [
//             {
//                 "name": "西安市",
//                 "id": "1-1",
//                 "children": [
//                     {
//                         "name": "未央区",
//                         "id": "1-1-1"
//                     }
//                 ]
//             },
//             {
//                 "name": "宝鸡市",
//                 "id": "1-2",
//                 "children": [
//                     {
//                         "name": "陈仓区",
//                         "id": "1-2-1"
//                     }
//                 ]
//             }
//         ]
//     },
//     {
//         "name": "西安市",
//         "id": "1-1",
//         "children": [
//             {
//                 "name": "未央区",
//                 "id": "1-1-1"
//             }
//         ]
//     },
//     {
//         "name": "未央区",
//         "id": "1-1-1"
//     },
//     {
//         "name": "宝鸡市",
//         "id": "1-2",
//         "children": [
//             {
//                 "name": "陈仓区",
//                 "id": "1-2-1"
//             }
//         ]
//     },
//     {
//         "name": "陈仓区",
//         "id": "1-2-1"
//     },
//     {
//         "name": "山西省",
//         "id": "2",
//         "children": [
//             {
//                 "name": "大同市",
//                 "id": "2-1",
//                 "children": []
//             }
//         ]
//     },
//     {
//         "name": "大同市",
//         "id": "2-1",
//         "children": []
//     }
// ]
  • getTreeNodeByKey [Function] 通过 key 值查找树节点

deepCopy [Function] 深拷贝

参数 类型 必填
obj any true
cache WeakMap false

download [Function] 下载

参数 类型 必填
url string true
fileName string true
callback function false

getQueryParamFromLocation [Function] 从 location 中获取 query 参数

参数 类型 必填 默认值
str string true location.href

使用

import {getQueryParamFromLocation} from '@jsir_z/fast-utils';
const str = 'https://www.baidu.com?id=jisr_z&name=jsir';
const result = getQueryParamFromLocation(str);
console.log(result);
//result
//{
//     "id": "jisr_z",
//     "name": "jsir"
// }

randomHexColor [Function] 获取随机颜色

outOfNum [Function] 数字超出显示+号

参数 类型 必填
val number true
maxNum number true

trim [Function] 字符串去除空格

参数 类型 必填 说明
str string true
isAll boolean false 是否去除全部空格

turnCase [Function] 字符串切换大小写

参数 类型 必填 说明
str string true
type A , a ,Aa true A-全部大写 a-全部小写 Aa-首字母大写

formatMoney [Function] 金钱格式化成 1,212,221

参数 类型 必填 说明
num number true

detectDeviceType [Function] 获取设备类型

Readme

Keywords

none

Package Sidebar

Install

npm i @jsir_z/fast-utils

Weekly Downloads

0

Version

1.0.9

License

none

Unpacked Size

51.8 kB

Total Files

7

Last publish

Collaborators

  • jsir_z