npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@puffmeow/utils

v0.2.21

Published

通用工具函数

Downloads

6

Readme

通用工具函数集

安装

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