All files index.js

100% Statements 57/57
95.45% Branches 21/22
100% Functions 8/8
100% Lines 57/57
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                    1x                               92x   92x 92x 92x       92x       92x 92x       2x         62926x 62926x   62924x 62924x 62924x   37395x 37395x 37395x 37395x 37395x   62924x           18101x           18101x 18101x 18101x   17800x 17800x 17800x 17800x 17800x           1x 1x   1x 1x 1x 1x           81021x 81021x   81003x   81021x   62921x       18100x 18098x 18098x           3x   1x       2x   2x 2x         92x 92x 92x 92x 92x 92x 92x 92x     1x  
/**
 * @file LRU cached generator utility.
 *
 * @author Nahid Akbar
 * @year 2016
 * @copyright National ICT Australia (NICTA). All rights reserved.
 */
 
"use strict";
 
const stringify = require('json-stable-stringify');
 
//
// function print(t)
// {
//   while (t !== undefined)
//   {
//     console.log('item', t.key,
//       t.prev ? 'prev' : '', t.prev ? t.prev.key : '',
//       t.next ? 'next' : '', t.next ? t.next.key : '');
//     t = t.next;
//   }
// }
//
function generator(limit, global_generate, options)
{
  options = options || {};
 
  limit = Math.max(1, limit);
  var index = {};
  var top = {
    next: null,
    key: 'top'
  };
  var bottom = {
    prev: top,
    key: 'bottom'
  };
  top.next = bottom;
  var count = 0;
 
  function isCached(key)
  {
    return index[key] !== undefined;
  }
 
  function getCached(key)
  {
    var i = index[key];
    if (i !== undefined)
    {
      var n = i.next,
        p = i.prev;
      if (i.key !== top.next.key)
      {
        p.next = n;
        n.prev = p;
        top.next.prev = i;
        i.next = top.next;
        top.next = i;
      }
      return top.next.item;
    }
  }
 
  function cache(key, value)
  {
    var n = index[key] = {
      next: top.next,
      prev: top,
      item: value,
      key: key
    };
    top.next.prev = n;
    top.next = n;
    if (++count > limit)
    {
      var i = bottom.prev;
      i.prev.next = i.next;
      i.next.prev = i.prev;
      delete index[i.key];
      count--;
    }
  }
 
  function purge(key)
  {
    var i = index[key];
    Eif (i !== undefined)
    {
      i.prev.next = i.next;
      i.next.prev = i.prev
      delete index[key];
      count--;
    }
  }
 
  function sync_generate(key, local_generate)
  {
    const originalKey = key;
    if (typeof key !== 'string')
    {
      key = stringify(key);
    }
    if (index[key] !== undefined)
    {
      return getCached(key);
    }
    else
    {
      const value = (local_generate || global_generate)(originalKey, key);
      cache(key, value);
      return value;
    }
  }
 
  function async_generate(key, callback, local_generate)
  {
    if (index[key] !== undefined)
    {
      callback(getCached(key));
    }
    else
    {
      (local_generate || global_generate)(key, function (value)
      {
        cache(key, value);
        callback(value);
      });
    }
  }
 
  var result = options.async ? async_generate : sync_generate;
  result.sync = sync_generate;
  result.async = async_generate;
  result.cache = cache;
  result.purge = purge;
  result.isCached = isCached;
  result.getCached = getCached;
  return result;
}
 
module.exports = generator;