All files lruCache.js

93.33% Statements 14/15
75% Branches 6/8
100% Functions 4/4
92.86% Lines 13/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  2x     11x     11x 3x     3x 2x 2x     3x       8x       4x 4x 4x           2x    
export default function lruCache(limit, equals) {
  const entries = []
 
  function get(key) {
    const cacheIndex = entries.findIndex(entry => equals(key, entry.key))
 
    // We found a cached entry
    if (cacheIndex > -1) {
      const entry = entries[cacheIndex]
 
      // Cached entry not at top of cache, move it to the top
      if (cacheIndex > 0) {
        entries.slice(cacheIndex, 1)
        entries.unshift(entry)
      }
 
      return entry.value
    }
 
    // No entry found in cache, return null
    return undefined
  }
 
  function put(key, value) {
    Eif (!get(key)) {
      entries.unshift({ key, value })
      Iif (entries.length > limit) {
        entries.pop()
      }
    }
  }
 
  return { get, put }
}