ECMAScript 6 Map-Like implementation with keys that have a defined timelife.
- Node.js v12 or higher
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/timemapconst{ strictEqual }=require("assert");constTimeMap=require("@slimio/timemap");constcol=newTimeMap(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);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 |
constmap=newTimeMap(100);map.on("expiration",(key,value)=>{console.log(`key: ${key}, value: ${value}`);});map.set("foo","bar");Following methods are members of TimeMap class. The type TimeMap.key is declared as follow:
typekey=symbol|string|number;constructor(timeLifeMs: number)
Create a new TimeMap Object. Take an argument which is the time that a key stay alive within the class.
constmap=newTimeMap(5000);map.set("foo","bar");// foo will live for the next 5,000 millisecondsThe default timeLifeMs is equal to the value of static member TimeMap.DEFAULT_TIMELIFE_MS (equal to 1000 by default).
const{ strictEqual }=require("assert");constmap=newTimeMap();strictEqual(map.timeLife,TimeMap.DEFAULT_TIMELIFE_MS);set(key: TimeMap.key, value: any): void
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");constmap=newTimeMap();constsym=Symbol("foo");map.set(sym,"bar");strictEqual(map.get(sym),"foo");has(key: TimeMap.key, refreshTimestamp?: boolean): boolean
Similar to Map.has method. Return true if the key exist within.
const{ strictEqual }=require("assert");constmap=newTimeMap(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(key: TimeMap.key): void
Delete a given key from TimeMap. The key must be a string or a symbol.
const{ strictEqual }=require("assert");constmap=newTimeMap(100);map.once("expiration",(key)=>{strictEqual(key,"hello");});map.set("foo","bar");map.set("hello","world");setTimeout(()=>{map.delete("foo");},50)get< T >(key: TimeMap.key, refreshTimestamp?: boolean): T
Get a given key from the Class. Throw an Error if the key doesn't exist in the Collection (use .has() before).
constassert=require("assert");constmap=newTimeMap(100);map.set("foo","bar");assert.strictEqual(map.get("foo"),"bar");assert.throws(()=>{map.get("world!");},{name: "Error"});clear(): void
Clear internal timer and internal data. Everything will be reset.
keys(): IterableIterator< TimeMap.key >
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");constmap=newTimeMap();map.set("foo","bar");map.set("yo","boo");deepEqual(["foo","yo"],[...map.keys()]);All following properties are readonly
.size
The size accessor property returns the number of elements in the TimeMap.
const{ strictEqual }=require("assert");constmap=newTimeMap();map.set("foo","bar");strictEqual(map.size,1);.timeLife
The timeLife accessor property return the configured time life for keys
const{ strictEqual }=require("assert");constmap=newTimeMap(2000);strictEqual(map.timeLife,2000);This project have no dependencies.
MIT