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

js-utils-collection

v2.0.0

Published

js-utils-collection

Downloads

11

Readme

js-utils-collection

常用工具函数整理,使用 typescript 实现,类型提示友好

安装方式

npm install js-utils-collection
yarn js-utils-collection
pnpm install js-utils-collection

工具类 & 工具函数

EventBus

事件总线类, 在 ts 中使用,能传入类型变量, 以此获得的事件名、回调参数类型提示。

import { EventBus } from "js-utils-collection";

interface EventMap {
  count: number;
}

const bus = new EventBus<EventMap>();

bus.on("count", (value) => {
  // typescript will known `value` is number or undefined
});

bus.fire("count", 2023);
// if your code is '2023' string, ts will throw a type error

| 方法 | 类型 | 描述 | | ------ | -------------------------------------------------- | ------------------------------- | | on | (eventName: string, callback: function) => void | 监听事件 | | fire | (eventName: string, value: unknown) => void | 触发事件 | | off | (eventName: string, callback: function) => void | 取消监听事件 | | once | (eventName: string, callback: function) => void | 监听事件,触发一次后自动取消监听 |

工厂函数 import { newEventBus } from "js-utils-collection";

Storage

扩展原 Web Storage 的setItem``getItem接口, 在 ts 中使用,能传入类型变量, 以此获得的 key,value 类型提示。

import { Storage } from "js-utils-collection";

interface KeyValType {
  id: number;
  username: string;
}

const storage = new Storage<KeyValType>("sessionStorage");

storage.setItem("id", 2);
// if you write storage.setItem('id', '2'), typescript will throw type error

storage.getItem("id");
// if you write storage.getItem('ids'), typescript will throw type error

工厂函数 import { storageBuilder } from "js-utils-collection";


wait

返回一个等待一段时间的 promise 实例

import { wait } from "js-utils-collection";

wait(4000).then(() => {
  // 4s后执行...
});

| params | 类型 | 描述 | | ------- | -------- | ----------------- | | delay | number | 等待时间, 默认 1s |

fakeRequest

模拟 ajax 异步返回值

import { fakeRequest } from "js-utils-collection";

fakeRequest({ msg: "hello world." }, 4000).then((data) => {
  // 4s后执行...
  console.log(data);
  // log: { msg: “hello world." }
});

| params | 类型 | 描述 | | ------- | -------- | ----------------------------- | | data | any | 等待一段时间后被 resolve 的值 | | delay | number | 等待时间, 默认 1s |

interval

setTimeout 模拟 setInterval 功能

import { interval } from "js-utils-collection";

StringBox

用来实现文字输入效果

import { StringBox } from "js-utils-collection";

new StringBox("abcd").pipelineChar((str, next) => {
  console.log(str, next)
})

// 每200ms打印一次
// a ture
// ab ture
// abc ture
// abcd false
interface TPipelineOption {
  speed?: number;
  signal?: AbortSignal;
  mode?: "append" | "single";
}
declare class StringBox extends String {
  pipelineChar: (cb: (str: string, next: boolean) => void, optionOuter?: TPipelineOption) => Promise<void>;
}

环境常量

isNode

import { isNode } from "js-utils-collection";

| value | 类型 | 描述 | | ------- | --------- | ---------------------------- | | true | boolean | 当前 js 运行在 node 环境 | | false | boolean | 当前 js 没有运行在 node 环境 |

isBrowser

import { isBrowser } from "js-utils-collection";

| value | 类型 | 描述 | | ------- | --------- | ---------------------------- | | true | boolean | 当前 js 运行在浏览器环境 | | false | boolean | 当前 js 没有运行在浏览器环境 |