1.0.5 • Published 2 years ago

@sugaz/utils v1.0.5

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

@sugaz/utils 介绍


1. 安装

npm install --save @sugaz/utils

2. 介绍

isDef: 判断传入的值是否为真值

isDef<T = any>(value: T): value is NonNullable

import { isDef } from "@sugaz/utils";

isDef(123);
// => true
isDef(null);
// => false

isObject: 判断传入的值是否为对象

isObject(val: unknown): val is Record<any, any>

import { isObject } from "@sugaz/utils";

isObject({ test: 123 });
// => true
isObject(123);
// => false

isObject: 过滤掉对象中的 undefined | null,浅拷贝

mapToDefObject<T = any>(obj: T): Fix<T, undefined | null>

import { mapToDefObject } from "@sugaz/utils";

mapToDefObject({ test: 123, a: undefined, b: null, e: [1, "sd"] });
// => {test: 123, e: [1, "sd"]}

getExtension: 获取文件的类型

getExtension(filename: string): string

import { getExtension } from "@sugaz/utils";

getExtension("asd.cvxv.jpeg");
// => jpeg

parseTime: 转化时间格式 Date | "1548221490638" -> {y}-{m}-{d} {h}:{i}:{s}

parseTime(time: Date | string | number, cFormat?: string): string

import { parseTime } from "@sugaz/utils";

parseTime(1627816169200);
// => 2021-08-01 19:09:29

formatTime: 格式化时间 传入时间戳 -> xx 分钟前 | {y}-{m}-{d} {h}:{i}:{s}

formatTime(time: number | string, option?: string): string

import { formatTime } from "@sugaz/utils";

const time1 = new Date().getTime();
const time2 = new Date().getTime() - 2 * 60 * 1000;

formatTime(time1);
// => 刚刚

formatTime(time2);
// => 2分钟前

getMonthStartAndEnd: 获取当月的开始和结束日期 格式 {y}-{m}-{d}

getMonthStartAndEnd(): { start: string; end: string }

import { getMonthStartAndEnd } from "@sugaz/utils";

getMonthStartAndEnd();

numToFix: 保留数字的后几位数,默认两位

numToFix(value: number | string, fix = number, toString: string): string | nulber

import { numToFix } from "@sugaz/utils";

numToFix(0.1234);
// => 0.12

toThousands: 格式化数据千位数加逗号

toThousands(value: number): string

import { toThousands } from "@sugaz/utils";

numToFix(1000000.01);
// => 1,000,000.01

throttle: 节流

(func: Function, delay: number, init: boolean): Function

import { throttle } from "@sugaz/utils";

/**
 * @description: 节流throttle代码(时间戳+定时器)
 * @param {Function} func 回调函数
 * @param {number} delay 延迟时间
 * @param {boolean} init 是否在第一次立刻执行
 * @return {Function}
 */
const cb = throttle(function() {
    console.log("move");
}, 200, true);
1.0.5

2 years ago

1.0.2

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

3 years ago