File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -193,7 +193,18 @@ describe('lru()', () => {
193193 cache . write ( 'baz' , 'boz' )
194194
195195 const results : string [ ] = [ ]
196- cache . forEach ( ( value ) => results . push ( value ) )
196+ cache . forEach ( ( key ) => results . push ( key ) )
197197 expect ( results ) . toEqual ( [ 'baz' , 'bar' , 'foo' ] )
198198 } )
199+
200+ it ( 'should not iterate over deleted head' , ( ) => {
201+ const cache = lru < string , string > ( Infinity )
202+ cache . write ( 'foo' , 'bar' )
203+ cache . write ( 'bar' , 'baz' )
204+ cache . delete ( 'bar' )
205+
206+ const results : string [ ] = [ ]
207+ cache . forEach ( ( key ) => results . push ( key ) )
208+ expect ( results ) . toEqual ( [ 'foo' ] )
209+ } )
199210} )
Original file line number Diff line number Diff line change @@ -107,6 +107,9 @@ export const lru = <Key = string, Value = any>(
107107 if ( this . size === 1 ) {
108108 this . head = undefined
109109 } else {
110+ if ( node === this . head ) {
111+ this . head = node . prev
112+ }
110113 node . prev . next = node . next
111114 node . next . prev = node . prev
112115 }
You can’t perform that action at this time.
0 commit comments