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

@zimi/utils

v0.17.1

Published

common utils

Downloads

5

Readme

@zimi/utils

一些常用功能函数

install

yarn add @zimi/utils

examples

createSsrStore_createJsonStorage
ExactClickChecker
genePromiseOnce
historyStateManager
resizeTo
sleepMs
withStatic


createSsrStore_createJsonStorage

It created for Next.js ssr,
match server-side rendering with client-side rendering,
and apply local stored value a little later.


import { create } from 'zustand'

const defaultCounter = {
  count: 0,
  privateKey: 'This value wont be stored'
}

const useCounter = createSsrStore(() => defaultCounter, {
  name: 'key-of-storage',
  storage: createJsonStorage({
    partialKeys: ['count'],
  })
})

function App() {
  const { count } = useCounter()

  const inc = () => {
    useCounter.setState((prev) => prev + 1)
  }

  return <div>
    <p> count: {count} </p>
    <Button onClick={inc}> inc </Button>
  </div>
}

↑ all examples ↑

ExactClickChecker


const clickChecker = new ExactClickChecker()
clickChecker.bindEvents()

// or you can check in mouseup / touchend / click / dblclick
canvas.addEventListener('pointerup', () => {
  if (clickChecker.checkIsClick()) {
    // is exact click
  }
  if (clickChecker.checkIsClick({ durationMs: 300 })) {
    // is exact click
  }
  if (clickChecker.checkIsDoubleClick()) {
    // is exact double click
  }
})

↑ all examples ↑

genePromiseOnce


// expensiveButStorableAsyncFunction will be called only once forever, unless rejected
const promiseOnce = genePromiseOnce(expensiveButStorableAsyncFunction)

window.addEventListener('resize', () => {
  promiseOnce()
    .then((res) => {
      console.log(`promise successed: ${res}`)
    })
    .catch(() => {
      console.log('catch')
    })
})

↑ all examples ↑

historyStateManager


// you should call this function as early as possible
// WARNING: this function includes `window.history.replaceState(xxx)`
historyStateManager.init()

function App() {
  const [num, setNum] = useState(0)
  return (
    <Button
      onClick={() => {
        setNum((prev) => prev + 1)
        historyStateManager.push(async () => {
          if (window.confirm('Are you sure?')) {
            setNum((prev) => prev - 1)
          } else {
            Toastify({ text: 'User rejected' }).showToast()
            throw new Error('reject')
          }
        })
      }}
    >
      {num}
    </Button>
  )
}

↑ all examples ↑

resizeTo


const src = {
  width: 100,
  height: 200,
}

const target = {
  width: 300,
  height: 400,
}

// srcFitCover is {
//   width: 300,
//   height: 600,
// }
const srcFitCover = resizeTo({
  src,
  target,
  fit: 'cover',
})

// srcFitContain is {
//   width: 200,
//   height: 400,
// }
const srcFitContain = resizeTo({
  src,
  target,
  fit: 'contain',
})

↑ all examples ↑

sleepMs


async function() {
  await sleepMs(500)
}

↑ all examples ↑

withStatic


import { create } from 'zustand'

const useRawCounter = create(() => ({
  count: 0,
}))

const useCounter = withStatic(useRawCounter, {
  inc() {
    useRawCounter.setState((prev) => prev + 1),
  },
  dec() {
    useRawCounter.setState((prev) => prev - 1),
  },
})

function App() {
  const { count } = useCounter()

  return <div>
    <Button onClick={useCounter.inc}> inc </Button>
    <p> count: {count} </p>
    <Button onClick={useCounter.dec}> dec </Button>
  </div>
}

↑ all examples ↑