Skip to content

Commit

Permalink
Add rentries, rkeys, rvalues
Browse files Browse the repository at this point in the history
Same as entries/keys/values, but in order from oldest to newest instead
of newest to oldest.
  • Loading branch information
isaacs committed Mar 14, 2022
1 parent bb01e26 commit 7eb6368
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 5 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])`

Expand Down
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,29 @@ 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; ) {
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.head) {
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; ) {
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.tail) {
break
} else {
i = this.next[i]
}
}
}
Expand All @@ -271,18 +276,33 @@ class LRUCache {
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()
Expand Down
141 changes: 141 additions & 0 deletions tap-snapshots/test/map-like.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ exports[`test/map-like.js TAP > empty, keys 1`] = `
Generator []
`

exports[`test/map-like.js TAP > empty, rentries 1`] = `
Generator []
`

exports[`test/map-like.js TAP > empty, rkeys 1`] = `
Generator []
`

exports[`test/map-like.js TAP > empty, rvalues 1`] = `
Generator []
`

exports[`test/map-like.js TAP > empty, values 1`] = `
Generator []
`
Expand Down Expand Up @@ -325,6 +337,77 @@ Generator [
]
`

exports[`test/map-like.js TAP > rentries 1`] = `
Generator [
Array [
3,
"3",
],
Array [
4,
"4",
],
Array [
5,
"5",
],
Array [
6,
"6",
],
Array [
7,
"7",
],
]
`

exports[`test/map-like.js TAP > rentries, 7 stale 1`] = `
Generator [
Array [
3,
"3",
],
Array [
5,
"5",
],
Array [
6,
"6",
],
Array [
4,
"new value 4",
],
]
`

exports[`test/map-like.js TAP > rentries, new value 4 1`] = `
Generator [
Array [
3,
"3",
],
Array [
5,
"5",
],
Array [
6,
"6",
],
Array [
7,
"7",
],
Array [
4,
"new value 4",
],
]
`

exports[`test/map-like.js TAP > rforEach, no thisp 1`] = `
Array [
Array [
Expand All @@ -346,6 +429,64 @@ Array [
]
`

exports[`test/map-like.js TAP > rkeys 1`] = `
Generator [
3,
4,
5,
6,
7,
]
`

exports[`test/map-like.js TAP > rkeys, 7 stale 1`] = `
Generator [
3,
5,
6,
4,
]
`

exports[`test/map-like.js TAP > rkeys, new value 4 1`] = `
Generator [
3,
5,
6,
7,
4,
]
`

exports[`test/map-like.js TAP > rvalues 1`] = `
Generator [
"3",
"4",
"5",
"6",
"7",
]
`

exports[`test/map-like.js TAP > rvalues, 7 stale 1`] = `
Generator [
"3",
"5",
"6",
"new value 4",
]
`

exports[`test/map-like.js TAP > rvalues, new value 4 1`] = `
Generator [
"3",
"5",
"6",
"7",
"new value 4",
]
`

exports[`test/map-like.js TAP > values 1`] = `
Generator [
"7",
Expand Down
12 changes: 12 additions & 0 deletions test/map-like.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const c = new LRU({ max: 5, maxSize: 5 })
t.matchSnapshot(c.keys(), 'empty, keys')
t.matchSnapshot(c.values(), 'empty, values')
t.matchSnapshot(c.entries(), 'empty, entries')
t.matchSnapshot(c.rkeys(), 'empty, rkeys')
t.matchSnapshot(c.rvalues(), 'empty, rvalues')
t.matchSnapshot(c.rentries(), 'empty, rentries')
t.matchSnapshot(c.dump(), 'empty, dump')

for (let i = 0; i < 8; i++) {
Expand All @@ -29,12 +32,18 @@ const e = i => ({
t.matchSnapshot(c.keys(), 'keys')
t.matchSnapshot(c.values(), 'values')
t.matchSnapshot(c.entries(), 'entries')
t.matchSnapshot(c.rkeys(), 'rkeys')
t.matchSnapshot(c.rvalues(), 'rvalues')
t.matchSnapshot(c.rentries(), 'rentries')
t.matchSnapshot(c.dump(), 'dump')

c.set(4, 'new value 4')
t.matchSnapshot(c.keys(), 'keys, new value 4')
t.matchSnapshot(c.values(), 'values, new value 4')
t.matchSnapshot(c.entries(), 'entries, new value 4')
t.matchSnapshot(c.rkeys(), 'rkeys, new value 4')
t.matchSnapshot(c.rvalues(), 'rvalues, new value 4')
t.matchSnapshot(c.rentries(), 'rentries, new value 4')
t.matchSnapshot(c.dump(), 'dump, new value 4')

// pretend an entry is stale for some reason
Expand All @@ -59,6 +68,9 @@ for (const i of c.rindexes()) {
t.matchSnapshot(c.keys(), 'keys, 7 stale')
t.matchSnapshot(c.values(), 'values, 7 stale')
t.matchSnapshot(c.entries(), 'entries, 7 stale')
t.matchSnapshot(c.rkeys(), 'rkeys, 7 stale')
t.matchSnapshot(c.rvalues(), 'rvalues, 7 stale')
t.matchSnapshot(c.rentries(), 'rentries, 7 stale')
t.matchSnapshot(c.dump(), 'dump, 7 stale')

const feArr = []
Expand Down

0 comments on commit 7eb6368

Please # to comment.