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

@co-hooks/util

v1.0.11

Published

util functions and types

Downloads

181

Readme

Util - 工具函数、通用类型

这里包含了很多通用的小函数和通用的类型定义

guid

生成一个全局唯一的字符串,如果需要id在每一次请求的时候不变,需要要useMemo。

  • 定义 function guid(): string
  • 用法
    import React, {useMemo} from 'react'
    import {guid} from '@co-hooks/util'
    
    function DemoComponent() {
    
        const id = useMemo(() => guid(), []);
    
        return (
            <div id={id}/>
        )
    }

type

获取一个变量的系统预设类型,不推荐使用此函数,因为他会影响typescript的类型推导。 对于基础类型建议使用typeof,对于系统类型,比如数组,用Array.isArray, 这样可以获取更好的typescript支持。

  • 定义 function type(obj: any): string
  • 用法
    import {type} from '@co-hooks/util'
    
    type(1); // number
    type(''); // string
    type(Date.now()) // date

clone

克隆一个可以转换为json的对象,使用了内置的api。 使用JSON.stringify来实现的复制。

  • 定义 function clone<T>(obj: T): T

  • 泛型参数

    • T 表示分组要克隆值的类型,通常不需要传递,利用TypeScript推导
  • 用法

    import {clone} from '@co-hooks/util'
    
    clone(1); // 1
    clone('abc'); // abc

deepClone

克隆任何对象,支持函数等非JSON类型

  • 定义 function deepClone<T>(obj: T): T
  • 泛型参数
    • T 表示分组要克隆值的类型,通常不需要传递,利用TypeScript推导
  • 用法
    import {deepClone} from '@co-hooks/util'
    
    deepClone(1); // 1
    deepClone('abc'); // abc
    const fn = deepClone(() => 1);
    fn() // 1

UnionOmit

一个安全的交叉类型,UnionOmit会交叉两个类型,当名称有冲突的时,选取第一个,而不是报错。

  • 定义 UnionOmit<T, K> = T & Omit<K, keyof T>
  • 用法
    
        import {UnionOmit} from '@co-hooks/util'
    
        interface Demo {
            foo: string;
            bar: string;
        }
    
        interface Other {
            foo: number;
            baz: string;
        }
    
        type DemoUnionOther = UnionOmit<Demo, Other> // {foo: sting, bar: string; baz: string}
        type OtherUnionDemo = UnionOmit<Other, Demo> // {foo: number, bar: string; baz: string}