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

latermom

v2.0.1

Published

simple, zero-dependency, in-memory, lazy cache for javascript

Downloads

244

Readme

npm version Known Vulnerabilities

Installation

yarn add latermom --save

Usage

const { Cache } = require("latermom")

const maxSize = 2 // will limit max cache size upon cache.get()
const getUser = async (id) => {
  const response = await fetch("https://dummyjson.com/users/" + id)
  const { firstName, lastName, address } = await response.json()

  return {
    id,
    name: `${firstName} ${lastName}`,
    address,
  }
}

const usersCached = new Cache(getUser, maxSize)

Promise.all([
  usersCached.get(1),
  usersCached.get(2),
  usersCached.get(3),
  usersCached.get(4),
]).then((users) => {
  console.log(usersCached.data) // contains only last maxSize users (3 and 4)
  console.log(usersCached.size) // 2
})
const { Cache } = require("latermom")
// create cache with factory function (may take any number of parameters)
const cache = new Cache(() => Math.random())

// each time you call the cache.get with same parameters
// you will get the same once lazy cached answer
const a = cache.get(1, 2, 3)
const b = cache.get(1, 2, 3) // this will return the same as above
const c = cache.get(1, 2)
const d = cache.get(1, 2, 4)

console.log(a === b) // true
console.log(a !== c) // true
console.log(a !== d) // true

API

class Cache<T = unknown> {
  constructor(factoryFunction: (...args: any[]) => T, maxSize: number = Infinity) {
    //
  }
}
  • creates a LaterMom (Lazy Map) instance with factory function for lazy cache

methods

  • get(...args: any[]): T - get entry at key created from args, lazy instantiated by factory
  • createKey(...args: any[]): string - creates string key
  • create(...args: any[]): T - wrapped factory function
  • hasKey(key: string): boolean - check if has entry at key
  • has(...args: any[]): boolean - check if has entry at key created from args
  • deleteKey(key: string): boolean - deletes entry from data at key
  • delete(...args): boolean - deletes entry from data at key created from args
  • size: number - returns size of data
  • maxSize: number - on insert the oldest items are deleted until reached maxSize

properties:

  • data: Map

Tests

$ jest
 PASS  ./index.test.js
  ✓ Calls factory once when asked for same key many times (62 ms)
  ✓ Getter is able to handle multiple arguments
  ✓ Readme has working code (1 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        0.946 s, estimated 1 s
Ran all test suites.
Done in 1.44s.

License

MIT

Author

Jacek Pietal @ 2019-2021