0.2.21 • Published 1 year ago

@puffmeow/utils v0.2.21

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

通用工具函数集

安装

npm install @puffmeow/utils

通用 API

类型判断

判断一个值是否为空

import { isEmpty } from '@puffmeow/utils';

isEmpty(null); // true
isEmpty(undefined); // true
isEmpty(''); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(0); // false

判断一个值是否为对象

import { isObject } from '@puffmeow/utils';

isObject({}); // true
isObject([]); // false

判断一个值是否为数组

import { isArray } from '@puffmeow/utils';

isArray([]); // true
isArray({}); // false

判断一个值是否为 Set 类型

import { isSet } from '@puffmeow/utils';

isSet(new Set()); // true
isSet({}); // false

判断一个值是否为 Map 类型

import { isMap } from '@puffmeow/utils';

isMap(new Map()); // true

通用 API

睡眠函数

import { sleep } from '@puffmeow/utils';

sleep(1000).then(() => {
  console.log('sleep done');
});

深度克隆

import { deepClone } from '@puffmeow/utils';

const obj = { a: 1, b: { c: 2 } };
const cloneObj = deepClone(obj);

防抖,支持立即执行

import { debounce } from '@puffmeow/utils';

// 参数
// fn: 需要防抖的函数
// delay: 延迟时间,单位是毫秒
// immediate: 是否立即执行
const debouncedFn = debounce(
  () => {
    console.log('debouncedFn');
  },
  300,
  true,
);

safeParse

安全执行 JSON.parse,内部 try catch,失败返回 null 或者配置的默认值

import { safeParse } from '@puffmeow/utils';

const json = '{"a": 1}';
const obj = safeParse(json); // { a: 1 }

omit 忽略对象中的指定键

import { omit } from '@puffmeow/utils';

const obj = { a: 1, b: 2, c: 3 };
const newObj = omit(obj, ['a', 'c']); // { b: 2 }

pick 提取对象中的指定键

import { pick } from '@puffmeow/utils';

const obj = { a: 1, b: 2, c: 3 };
const newObj = pick(obj, ['a', 'c']); // { a: 1, b: 2 }

to 对异步函数进行 try catch

import { to } from '@puffmeow/utils';

const asyncFn = async (params) => axios(params);

const [err, res] = await to(asyncFn);
// err 为 .catch 的内容
// res 为 .then 的内容

将字符串转换为数字

将一个字符串转换成数字类型,如果出错或者传入的类型转化为 NaN,则返回默认值 0(可以通过第二个参数进行配置)

import { toNumber } from '@puffmeow/utils';

toNumber('123'); // 123
// 转换为了 NaN,则返回第二个参数配置的默认值
toNumber('asddsa', 0); // 0

判断一个值是否为 true,包括字符串

import { isTrue } from '@puffmeow/utils';

isTrue(true); // true
isTrue('true'); // true
isTrue(1); // false
isTrue('1'); // false
isTrue('false'); // false

业务相关 API

检查手机号是否正确

import { checkPhone } from '@puffmeow/utils';

checkPhone('13812345678'); // true

检查一个字符串是否图片格式

png|jpg|jpeg|gif|webp|svg|bmp 格式会被认为是图片

import { checkImg } from '@puffmeow/utils';

checkImg('https://example.com/image.jpg'); // true

检查一个字符串是否视频格式

mp4|3gp|m3u8|flv|avi|mkv|mov|wmv|rmvb|webm 格式会被认为是视频

import { checkVideo } from '@puffmeow/utils';

checkVideo('https://example.com/video.mp4'); // true

检查一个字符串是否音频格式

mp3|wav|flac|aac|m4a|ogg|wma|amr 格式会被认为是音频

import { checkAudio } from '@puffmeow/utils';

checkAudio('https://example.com/audio.mp3'); // true
0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.10

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago