Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.4.5
Choose a base ref
...
head repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.5.0
Choose a head ref
  • 4 commits
  • 7 files changed
  • 2 contributors

Commits on Mar 11, 2022

  1. build: remove bundler and unnecessary package.json fields

    PR-URL: #211
    Credit: @SuperOleg39
    Close: #211
    Reviewed-by: @isaacs
    o.drapeza authored and isaacs committed Mar 11, 2022
    Copy the full SHA
    bb01e26 View commit details

Commits on Mar 14, 2022

  1. Add rentries, rkeys, rvalues

    Same as entries/keys/values, but in order from oldest to newest instead
    of newest to oldest.
    isaacs committed Mar 14, 2022
    Copy the full SHA
    7eb6368 View commit details
  2. defend against mutation while iterating

    Fix: #212
    isaacs committed Mar 14, 2022
    Copy the full SHA
    c1ad5da View commit details
  3. 7.5.0

    isaacs committed Mar 14, 2022
    Copy the full SHA
    6406220 View commit details
Showing with 910 additions and 2,042 deletions.
  1. +21 −3 README.md
  2. +33 −9 index.js
  3. +630 −2,016 package-lock.json
  4. +5 −14 package.json
  5. +141 −0 tap-snapshots/test/map-like.js.test.cjs
  6. +68 −0 test/delete-while-iterating.js
  7. +12 −0 test/map-like.js
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -336,15 +336,33 @@ If you put more stuff in it, then items will fall out.

* `keys()`

Return a generator yielding the keys in the cache.
Return a generator yielding the keys in the cache, in order from most
recently used to least recently used.

* `rkeys()`

Return a generator yielding the keys in the cache, in order from least
recently used to most recently used.

* `values()`

Return a generator yielding the values in the cache.
Return a generator yielding the values in the cache, in order from most
recently used to least recently used.

* `rvalues()`

Return a generator yielding the values in the cache, in order from
least recently used to most recently used.

* `entries()`

Return a generator yielding `[key, value]` pairs.
Return a generator yielding `[key, value]` pairs, in order from most
recently used to least recently used.

* `rentries()`

Return a generator yielding `[key, value]` pairs, in order from least
recently used to most recently used.

* `find(fn, [getOptions])`

42 changes: 33 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -243,46 +243,74 @@ class LRUCache {

*indexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.tail; true; i = this.prev[i]) {
for (let i = this.tail, j; true; ) {
j = i === this.head
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.head) {
// either head now, or WAS head and head was deleted
if (i === this.head || j && !this.isValidIndex(i)) {
break
} else {
i = this.prev[i]
}
}
}
}

*rindexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.head; true; i = this.next[i]) {
for (let i = this.head, j; true; ) {
j = i === this.tail
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.tail) {
// either the tail now, or WAS the tail, and deleted
if (i === this.tail || j && !this.isValidIndex(i)) {
break
} else {
i = this.next[i]
}
}
}
}

isValidIndex (index) {
return this.keyMap.get(this.keyList[index]) === index
}

*entries () {
for (const i of this.indexes()) {
yield [this.keyList[i], this.valList[i]]
}
}
*rentries () {
for (const i of this.rindexes()) {
yield [this.keyList[i], this.valList[i]]
}
}

*keys () {
for (const i of this.indexes()) {
yield this.keyList[i]
}
}
*rkeys () {
for (const i of this.rindexes()) {
yield this.keyList[i]
}
}

*values () {
for (const i of this.indexes()) {
yield this.valList[i]
}
}
*rvalues () {
for (const i of this.rindexes()) {
yield this.valList[i]
}
}

[Symbol.iterator] () {
return this.entries()
@@ -315,16 +343,12 @@ class LRUCache {

purgeStale () {
let deleted = false
const toDelete = []
for (const i of this.rindexes({ allowStale: true })) {
if (this.isStale(i)) {
toDelete.push(this.keyList[i])
this.delete(this.keyList[i])
deleted = true
}
}
for (const k of toDelete) {
this.delete(k)
}
return deleted
}

Loading