Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 | 1x 11x 9x 3x 8x 2x 11x 11x 3x 3x 2x 2x 3x 8x 4x 4x 4x 2x | // @flow import type { Entry, Equals } from "."; const findIndex = (arr: any[], fn: any => any) => { for (let i = 0; i < arr.length; i++) { if (fn(arr[i])) { return i; } } return -1; }; export default function lruCache(limit: number, equals: Equals) { const entries: Entry[] = []; function get(key: any) { const cacheIndex = findIndex(entries, 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.splice(cacheIndex, 1); entries.unshift(entry); } return entry.value; } // No entry found in cache, return null return undefined; } function put(key: any, value: any) { Eif (!get(key)) { entries.unshift({ key, value }); Iif (entries.length > limit) { entries.pop(); } } } return { get, put }; } |