1.0.17 • Published 2 years ago

qc-toolkit v1.0.17

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

常用 utils 方法记录和调用

下载

npm i qc-toolkit

使用方法

import {
    delay,
    dateFormatting,
    set16ToRgb,
    setRgbTo16,
    arrGroup,
    arrFlatten,
    arrArchive,
    debounce,
    throttle,
    deepCloneObj,
    numTs,
    storage,
    showTime,
    checkType,
    UppercaseNum,
} from "qc-toolkit";

方法阐述详解

1、延时调用

/**
 * @description: 延时调用函数
 * @param {*} time 毫秒
 * @param {*} callback 回调方法
 * @return {*}
 */
export delay(time, callback)

2、时间格式处理

/**
 * @description:时间格式转换
 * @param {*} format 格式 示例 'Y-m-d' 为空时返回本方法支持的参数,
 * "ymd"可参考无值时返回的对象的key
 * @param {*} timestamp 时间戳 为空时默认当前时间戳
 * @return {*} 当format参数有值时,返回string 有格式的时间
 * 当format参数无值时,返回object 例如
 * {
    "d": "04",
    "D": "Wed",
    "j": 4,
    "l": "Wednesday",
    "N": 4,
    "S": "th",
    "w": 3,
    "z": 123,
    "W": 18,
    "F": "May",
    "m": "05",
    "M": "May",
    "n": 5,
    "t": 31,
    "L": 0,
    "Y": 2022,
    "y": "22",
    "a": "am",
    "A": "AM",
    "B": 179,
    "g": 11,
    "G": 11,
    "h": "11",
    "H": "11",
    "i": "18",
    "s": "54",
    "O": "+0800",
    "P": "+08:00",
    "c": "2022-05-04T11:18:54+08:00",
    "U": 1651634334
}
 */
export dateFormatting(format, timestamp)

3、颜色格式转换

a.16 进制转换成 rgb

/**
 * @description:颜色16进制转换rgb
 * @param {*} str 颜色16进制 String "#ffffff"
 * @return {*} String   rgb(255,255,255)
 */
export function set16ToRgb(str)

b. rdb 转换成 16 进制

/**
 * @description:颜色rgb转换16进制
 * @param {*} str 颜色rgb String "rgb(255,255,255)"
 * @return {*} String  颜色16进制  #ffffff
 */
export function setRgbTo16(str)

4、一维数组转 2 维数组 (分组)

/**
 * @description: 一维数组转2维数组 (分组)
 * @param {Array} arr:源数组
 * @param {Number} num: 基数 默认为2
 * @return {*}
 */
export function arrGroup (arr, num)

5、数组扁平化

/**
 * @description: 数组扁平化
 * @param {*} arr 源数组
 * @param {*} depth 扁平化深度 默认为1
 * @return {*}
 */
export  function arrFlatten(arr, depth)

6、对一维数组进行分组(根据 key)

/**
 * @description:对一维数组进行分组(根据 key)
 * @param {*} arr 源一维数组
 * @param {*} key String
 * @return {*}
 */
export function arrArchive (arr, key)

7、防抖

/**
 * @description: 防抖(事件绑定需要直接绑定返回值)
 * @param {*} callback 回调函数
 * @param {*} delay 防抖时间(毫秒)
 * @return {*} function
 */
export function debounce(callback, delay)

8、节流

/**
 * @description: 节流(事件绑定需要直接绑定返回值)
 * @param {*} callback 回调函数
 * @param {*} delay 节流时间
 * @return {*} function
 */
export function throttle(callback, delay)

9、深拷贝

/**
 * @description: 深拷贝(未处理原型链上的属性,只对对象本身的属性做拷贝)
 * @param {*} obj 源对象
 * @return {*} obj
 */
export function deepCloneObj(obj)

10、数字增加千分位分割符

/**
 * @description:数字增加千分位分割符(兼容小数)thousandSeparator
 * @param {Number,String} num
 * @param {*} points 是否强制加2位小数
 * @return {*}
 */
export function numTs(num, points)

11、localStorage 操作

storage 下有三个方法:存,取,移除
/**
 * @description:localStorage存储
 * @param {*} key 存储的key
 * @param {*} value 存储的的值
 * @return {*}
 */
const setItem = function(key, value)

/**
 * @description:localStorage获取
 * @param {*} key 存储的key
 * @return {*}
 */
const getItem = function(key)

/**
 * @description:localStorage移除
 * @param {*} key 存储的key
 * @return {*}
 */
const removeItem = function(key)

export const storage = { setItem, getItem, removeItem };

12、时间展示转化

/**
 * @description:时间展示转化,输入已过时间,输出x天前
 * @param { String } dateStr yyyy-MM-dd hh:mm:ss (2022-5-6 17:35:00)
 * 时间戳不支持
 * @return { String }
 */
export function showTime(dateStr)

13、值类型判断

/**
 * @description: 值类型判断
 * 可判断 {},[],"88",99,null,undefined,set,date,boolean,function,Symbol,Math
 * 内部使用Object.prototype.toString.call判断
 * @param {*} params
 * @return { String }
 */
export function checkType(params)

14、 数字转中文大写(金额)

/**
 * @description: 数字转中文大写(金额)
 * @param { Number,String } amount
 * @return { String }
 */
export function UppercaseNum(amount)

待补充...

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago