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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sugaz/utils

v1.0.5

Published

common project utils

Readme

@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);