Skip to content

Commit 7eb6368

Browse files
committed
Add rentries, rkeys, rvalues
Same as entries/keys/values, but in order from oldest to newest instead of newest to oldest.
1 parent bb01e26 commit 7eb6368

File tree

4 files changed

+196
-5
lines changed

4 files changed

+196
-5
lines changed

Diff for: README.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,33 @@ If you put more stuff in it, then items will fall out.
336336

337337
* `keys()`
338338

339-
Return a generator yielding the keys in the cache.
339+
Return a generator yielding the keys in the cache, in order from most
340+
recently used to least recently used.
341+
342+
* `rkeys()`
343+
344+
Return a generator yielding the keys in the cache, in order from least
345+
recently used to most recently used.
340346

341347
* `values()`
342348

343-
Return a generator yielding the values in the cache.
349+
Return a generator yielding the values in the cache, in order from most
350+
recently used to least recently used.
351+
352+
* `rvalues()`
353+
354+
Return a generator yielding the values in the cache, in order from
355+
least recently used to most recently used.
344356

345357
* `entries()`
346358

347-
Return a generator yielding `[key, value]` pairs.
359+
Return a generator yielding `[key, value]` pairs, in order from most
360+
recently used to least recently used.
361+
362+
* `rentries()`
363+
364+
Return a generator yielding `[key, value]` pairs, in order from least
365+
recently used to most recently used.
348366

349367
* `find(fn, [getOptions])`
350368

Diff for: index.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,29 @@ class LRUCache {
243243

244244
*indexes ({ allowStale = this.allowStale } = {}) {
245245
if (this.size) {
246-
for (let i = this.tail; true; i = this.prev[i]) {
246+
for (let i = this.tail, j; true; ) {
247247
if (allowStale || !this.isStale(i)) {
248248
yield i
249249
}
250250
if (i === this.head) {
251251
break
252+
} else {
253+
i = this.prev[i]
252254
}
253255
}
254256
}
255257
}
258+
256259
*rindexes ({ allowStale = this.allowStale } = {}) {
257260
if (this.size) {
258-
for (let i = this.head; true; i = this.next[i]) {
261+
for (let i = this.head, j; true; ) {
259262
if (allowStale || !this.isStale(i)) {
260263
yield i
261264
}
262265
if (i === this.tail) {
263266
break
267+
} else {
268+
i = this.next[i]
264269
}
265270
}
266271
}
@@ -271,18 +276,33 @@ class LRUCache {
271276
yield [this.keyList[i], this.valList[i]]
272277
}
273278
}
279+
*rentries () {
280+
for (const i of this.rindexes()) {
281+
yield [this.keyList[i], this.valList[i]]
282+
}
283+
}
274284

275285
*keys () {
276286
for (const i of this.indexes()) {
277287
yield this.keyList[i]
278288
}
279289
}
290+
*rkeys () {
291+
for (const i of this.rindexes()) {
292+
yield this.keyList[i]
293+
}
294+
}
280295

281296
*values () {
282297
for (const i of this.indexes()) {
283298
yield this.valList[i]
284299
}
285300
}
301+
*rvalues () {
302+
for (const i of this.rindexes()) {
303+
yield this.valList[i]
304+
}
305+
}
286306

287307
[Symbol.iterator] () {
288308
return this.entries()

Diff for: tap-snapshots/test/map-like.js.test.cjs

+141
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ exports[`test/map-like.js TAP > empty, keys 1`] = `
134134
Generator []
135135
`
136136

137+
exports[`test/map-like.js TAP > empty, rentries 1`] = `
138+
Generator []
139+
`
140+
141+
exports[`test/map-like.js TAP > empty, rkeys 1`] = `
142+
Generator []
143+
`
144+
145+
exports[`test/map-like.js TAP > empty, rvalues 1`] = `
146+
Generator []
147+
`
148+
137149
exports[`test/map-like.js TAP > empty, values 1`] = `
138150
Generator []
139151
`
@@ -325,6 +337,77 @@ Generator [
325337
]
326338
`
327339

340+
exports[`test/map-like.js TAP > rentries 1`] = `
341+
Generator [
342+
Array [
343+
3,
344+
"3",
345+
],
346+
Array [
347+
4,
348+
"4",
349+
],
350+
Array [
351+
5,
352+
"5",
353+
],
354+
Array [
355+
6,
356+
"6",
357+
],
358+
Array [
359+
7,
360+
"7",
361+
],
362+
]
363+
`
364+
365+
exports[`test/map-like.js TAP > rentries, 7 stale 1`] = `
366+
Generator [
367+
Array [
368+
3,
369+
"3",
370+
],
371+
Array [
372+
5,
373+
"5",
374+
],
375+
Array [
376+
6,
377+
"6",
378+
],
379+
Array [
380+
4,
381+
"new value 4",
382+
],
383+
]
384+
`
385+
386+
exports[`test/map-like.js TAP > rentries, new value 4 1`] = `
387+
Generator [
388+
Array [
389+
3,
390+
"3",
391+
],
392+
Array [
393+
5,
394+
"5",
395+
],
396+
Array [
397+
6,
398+
"6",
399+
],
400+
Array [
401+
7,
402+
"7",
403+
],
404+
Array [
405+
4,
406+
"new value 4",
407+
],
408+
]
409+
`
410+
328411
exports[`test/map-like.js TAP > rforEach, no thisp 1`] = `
329412
Array [
330413
Array [
@@ -346,6 +429,64 @@ Array [
346429
]
347430
`
348431

432+
exports[`test/map-like.js TAP > rkeys 1`] = `
433+
Generator [
434+
3,
435+
4,
436+
5,
437+
6,
438+
7,
439+
]
440+
`
441+
442+
exports[`test/map-like.js TAP > rkeys, 7 stale 1`] = `
443+
Generator [
444+
3,
445+
5,
446+
6,
447+
4,
448+
]
449+
`
450+
451+
exports[`test/map-like.js TAP > rkeys, new value 4 1`] = `
452+
Generator [
453+
3,
454+
5,
455+
6,
456+
7,
457+
4,
458+
]
459+
`
460+
461+
exports[`test/map-like.js TAP > rvalues 1`] = `
462+
Generator [
463+
"3",
464+
"4",
465+
"5",
466+
"6",
467+
"7",
468+
]
469+
`
470+
471+
exports[`test/map-like.js TAP > rvalues, 7 stale 1`] = `
472+
Generator [
473+
"3",
474+
"5",
475+
"6",
476+
"new value 4",
477+
]
478+
`
479+
480+
exports[`test/map-like.js TAP > rvalues, new value 4 1`] = `
481+
Generator [
482+
"3",
483+
"5",
484+
"6",
485+
"7",
486+
"new value 4",
487+
]
488+
`
489+
349490
exports[`test/map-like.js TAP > values 1`] = `
350491
Generator [
351492
"7",

Diff for: test/map-like.js

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const c = new LRU({ max: 5, maxSize: 5 })
1010
t.matchSnapshot(c.keys(), 'empty, keys')
1111
t.matchSnapshot(c.values(), 'empty, values')
1212
t.matchSnapshot(c.entries(), 'empty, entries')
13+
t.matchSnapshot(c.rkeys(), 'empty, rkeys')
14+
t.matchSnapshot(c.rvalues(), 'empty, rvalues')
15+
t.matchSnapshot(c.rentries(), 'empty, rentries')
1316
t.matchSnapshot(c.dump(), 'empty, dump')
1417

1518
for (let i = 0; i < 8; i++) {
@@ -29,12 +32,18 @@ const e = i => ({
2932
t.matchSnapshot(c.keys(), 'keys')
3033
t.matchSnapshot(c.values(), 'values')
3134
t.matchSnapshot(c.entries(), 'entries')
35+
t.matchSnapshot(c.rkeys(), 'rkeys')
36+
t.matchSnapshot(c.rvalues(), 'rvalues')
37+
t.matchSnapshot(c.rentries(), 'rentries')
3238
t.matchSnapshot(c.dump(), 'dump')
3339

3440
c.set(4, 'new value 4')
3541
t.matchSnapshot(c.keys(), 'keys, new value 4')
3642
t.matchSnapshot(c.values(), 'values, new value 4')
3743
t.matchSnapshot(c.entries(), 'entries, new value 4')
44+
t.matchSnapshot(c.rkeys(), 'rkeys, new value 4')
45+
t.matchSnapshot(c.rvalues(), 'rvalues, new value 4')
46+
t.matchSnapshot(c.rentries(), 'rentries, new value 4')
3847
t.matchSnapshot(c.dump(), 'dump, new value 4')
3948

4049
// pretend an entry is stale for some reason
@@ -59,6 +68,9 @@ for (const i of c.rindexes()) {
5968
t.matchSnapshot(c.keys(), 'keys, 7 stale')
6069
t.matchSnapshot(c.values(), 'values, 7 stale')
6170
t.matchSnapshot(c.entries(), 'entries, 7 stale')
71+
t.matchSnapshot(c.rkeys(), 'rkeys, 7 stale')
72+
t.matchSnapshot(c.rvalues(), 'rvalues, 7 stale')
73+
t.matchSnapshot(c.rentries(), 'rentries, 7 stale')
6274
t.matchSnapshot(c.dump(), 'dump, 7 stale')
6375

6476
const feArr = []

0 commit comments

Comments
 (0)