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

@slimio/timemap

v1.0.0

Published

Map implementation with timelife keys

Downloads

12

Readme

TimeMap

version Maintenance MIT dep size Known Vulnerabilities Build Status

ECMAScript 6 Map-Like implementation with keys that have a defined timelife.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/timemap
# or
$ yarn add @slimio/timemap

Usage example

const { strictEqual } = require("assert");
const TimeMap = require("@slimio/timemap");

const col = new TimeMap(1000);
col.on("expiration", (key, value) => {
    console.log(`col key ${key} has expired!`);
});

col.set("foo", "bar");
col.set("test", true);
strictEqual(col.has("foo"), true);

setTimeout(() => {
    col.set("hello", "world!");
    strictEqual(col.has("foo", true), true);
}, 500);

setTimeout(() => {
    strictEqual(col.has("foo"), true);
}, 500)

setTimeout(() => {
    strictEqual(col.has("foo"), false);
    strictEqual(col.has("test"), false);
    strictEqual(col.has("hello"), true);
}, 1100);

Events

TimeMap class is extended by a Node.js EventEmitter. The class can trigger several events:

| event name | description | | --- | --- | | expiration | Expired key are emitted before deletion |

const map = new TimeMap(100);
map.on("expiration", (key, value) => {
    console.log(`key: ${key}, value: ${value}`);
});

map.set("foo", "bar");

API

Following methods are members of TimeMap class. The type TimeMap.key is declared as follow:

type key = symbol | string | number;

Create a new TimeMap Object. Take an argument which is the time that a key stay alive within the class.

const map = new TimeMap(5000);
map.set("foo", "bar"); // foo will live for the next 5,000 milliseconds

The default timeLifeMs is equal to the value of static member TimeMap.DEFAULT_TIMELIFE_MS (equal to 1000 by default).

const { strictEqual } = require("assert");

const map = new TimeMap();
strictEqual(map.timeLife, TimeMap.DEFAULT_TIMELIFE_MS);

Set a new key in the Collection. Inner timer will be initialized by the first key. The key must be a string or a symbol (no other primitive are accepted).

const { strictEqual } = require("assert");

const map = new TimeMap();
const sym = Symbol("foo");
map.set(sym, "bar");
strictEqual(map.get(sym), "foo");

Similar to Map.has method. Return true if the key exist within.

const { strictEqual } = require("assert");

const map = new TimeMap(100);
map.set("foo", "bar");
setTimeout(() => {
    strictEqual(map.has("foo", true), true);
}, 50);

setTimeout(() => {
    strictEqual(map.has("foo"), true);
}, 50);

setTimeout(() => {
    strictEqual(map.has("foo"), false);
}, 50);

Delete a given key from TimeMap. The key must be a string or a symbol.

const { strictEqual } = require("assert");

const map = new TimeMap(100);
map.once("expiration", (key) => {
    strictEqual(key, "hello");
});
map.set("foo", "bar");
map.set("hello", "world");

setTimeout(() => {
    map.delete("foo");
}, 50)

Get a given key from the Class. Throw an Error if the key doesn't exist in the Collection (use .has() before).

const assert = require("assert");

const map = new TimeMap(100);
map.set("foo", "bar");

assert.strictEqual(map.get("foo"), "bar");
assert.throws(() => {
    map.get("world!");
}, { name: "Error" });

Clear internal timer and internal data. Everything will be reset.

The keys() method returns a new Iterator object that contains the keys for each element in the TimeMap object in insertion order.

const { deepEqual } = require("assert");

const map = new TimeMap();
map.set("foo", "bar");
map.set("yo", "boo");

deepEqual(["foo", "yo"], [...map.keys()]);

Properties

All following properties are readonly

The size accessor property returns the number of elements in the TimeMap.

const { strictEqual } = require("assert");

const map = new TimeMap();
map.set("foo", "bar");
strictEqual(map.size, 1);

The timeLife accessor property return the configured time life for keys

const { strictEqual } = require("assert");

const map = new TimeMap(2000);
strictEqual(map.timeLife, 2000);

Dependencies

This project have no dependencies.

License

MIT