Socket
Socket
Sign inDemoInstall

@hscmap/cache-map

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hscmap/cache-map

CacheMap is an Es6's Map-like collection class with a limit of number of items. The limit must be specified at initialization time. ## Install ```sh npm install --save @hscmap/cache-map ```


Version published
Maintainers
1
Install size
7.22 kB
Created

Readme

Source

Introduction

CacheMap is an Es6's Map-like collection class with a limit of number of items. The limit must be specified at initialization time.

Install

npm install --save @hscmap/cache-map

Example

import { CacheMap } from "@hscmap/cache-map"
import * as _ from 'lodash'


const assert = (test: boolean) => console.assert(test)


!(function withoutPersistentItem() {
    const dropLog: number[] = []
    const cm = new CacheMap<string, number>(3, v => dropLog.push(v))

    cm.set('one', 1)
    cm.set('two', 2)
    cm.set('three', 3)

    assert(cm.get('one') == 1)
    assert(cm.get('two') == 2)
    assert(cm.get('three') == 3)

    cm.set('four', 4)
    assert(cm.get('four') == 4)
    assert(cm.get('one') == undefined)

    assert(cm.get('two') == 2)
    cm.set('five', 5)
    assert(cm.peek('two') == 2)
    assert(cm.peek('three') == undefined)

    assert(cm.size == 3)
    assert(_.isEqual(dropLog, [1, 3]))

    cm.clear()
    assert(cm.size == 0)
    assert(_.isEqual(dropLog, [1, 3, 4, 2, 5]))

    cm.set('one', 1)
    cm.set('two', 2)

    cm.delete('one')
    assert(cm.get('one') == undefined)
    assert(cm.size == 1)

})()


!(function test2() {
    const dropLog: number[] = []
    const cm = new CacheMap<string, number>(3, v => dropLog.push(v))

    cm.set('one', 1, true)
    cm.set('two', 2)
    cm.set('three', 3, true)
    cm.set('four', 4)
    cm.set('five', 5)
    cm.set('six', 6)
    cm.set('seven', 7)
    assert(cm.peek('one') == 1)
    assert(cm.peek('two') == undefined)
    assert(cm.peek('three') == 3)
    assert(cm.peek('four') == undefined)
    assert(cm.size == 5)
    assert(_.isEqual(dropLog, [2, 4]))
})()

FAQs

Last updated on 07 Jun 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc