.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / quick-lru / index.js
1 'use strict';
2
3 class QuickLRU {
4         constructor(opts) {
5                 opts = Object.assign({}, opts);
6
7                 if (!(opts.maxSize && opts.maxSize > 0)) {
8                         throw new TypeError('`maxSize` must be a number greater than 0');
9                 }
10
11                 this.maxSize = opts.maxSize;
12                 this.cache = new Map();
13                 this.oldCache = new Map();
14                 this._size = 0;
15         }
16
17         _set(key, value) {
18                 this.cache.set(key, value);
19                 this._size++;
20
21                 if (this._size >= this.maxSize) {
22                         this._size = 0;
23                         this.oldCache = this.cache;
24                         this.cache = new Map();
25                 }
26         }
27
28         get(key) {
29                 if (this.cache.has(key)) {
30                         return this.cache.get(key);
31                 }
32
33                 if (this.oldCache.has(key)) {
34                         const value = this.oldCache.get(key);
35                         this._set(key, value);
36                         return value;
37                 }
38         }
39
40         set(key, value) {
41                 if (this.cache.has(key)) {
42                         this.cache.set(key, value);
43                 } else {
44                         this._set(key, value);
45                 }
46
47                 return this;
48         }
49
50         has(key) {
51                 return this.cache.has(key) || this.oldCache.has(key);
52         }
53
54         peek(key) {
55                 if (this.cache.has(key)) {
56                         return this.cache.get(key);
57                 }
58
59                 if (this.oldCache.has(key)) {
60                         return this.oldCache.get(key);
61                 }
62         }
63
64         delete(key) {
65                 if (this.cache.delete(key)) {
66                         this._size--;
67                 }
68
69                 this.oldCache.delete(key);
70         }
71
72         clear() {
73                 this.cache.clear();
74                 this.oldCache.clear();
75                 this._size = 0;
76         }
77
78         * keys() {
79                 for (const el of this) {
80                         yield el[0];
81                 }
82         }
83
84         * values() {
85                 for (const el of this) {
86                         yield el[1];
87                 }
88         }
89
90         * [Symbol.iterator]() {
91                 for (const el of this.cache) {
92                         yield el;
93                 }
94
95                 for (const el of this.oldCache) {
96                         if (!this.cache.has(el[0])) {
97                                 yield el;
98                         }
99                 }
100         }
101
102         get size() {
103                 let oldCacheSize = 0;
104                 for (const el of this.oldCache) {
105                         if (!this.cache.has(el[0])) {
106                                 oldCacheSize++;
107                         }
108                 }
109
110                 return this._size + oldCacheSize;
111         }
112 }
113
114 module.exports = QuickLRU;