Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace lru-cache dependency #697

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions classes/range.js
Expand Up @@ -198,8 +198,8 @@ class Range {

module.exports = Range

const LRU = require('lru-cache')
const cache = new LRU({ max: 1000 })
const LRU = require('../internal/lrucache')
const cache = new LRU(1000)

const parseOptions = require('../internal/parse-options')
const Comparator = require('./comparator')
Expand Down
69 changes: 69 additions & 0 deletions internal/lrucache.js
@@ -0,0 +1,69 @@
class LRUCache {
constructor (max) {
if (!Number.isInteger(max) || max < 0) {
throw new TypeError('max must be a nonnegative integer')
}
mbtools marked this conversation as resolved.
Show resolved Hide resolved

this.max = max
this.map = new Map()
}

get (key) {
const value = this.map.get(key)
if (value === undefined) {
return undefined
} else {
// Remove the key from the map and add it to the end
this.map.delete(key)
this.map.set(key, value)
return value
}
}

has (key) {
return this.map.has(key)
}

delete (key) {
if (this.map.has(key)) {
this.map.delete(key)
return true
} else {
return false
}
}

set (key, value) {
const deleted = this.delete(key)

if (!deleted && value !== undefined) {
// If cache is full, delete the least recently used item
if (this.map.size >= this.max) {
const firstKey = this.map.keys().next().value
this.delete(firstKey)
}

this.map.set(key, value)
}

return this
}

clear () {
this.map.clear()
}
mbtools marked this conversation as resolved.
Show resolved Hide resolved

capacity () {
return this.max
}
mbtools marked this conversation as resolved.
Show resolved Hide resolved

size () {
return this.map.size
}
mbtools marked this conversation as resolved.
Show resolved Hide resolved

entries () {
return this.map.entries()
}
mbtools marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = LRUCache
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -48,9 +48,6 @@
"engines": {
"node": ">=10"
},
"dependencies": {
"lru-cache": "^6.0.0"
},
"author": "GitHub Inc.",
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
Expand Down