Skip to content

Commit b7c961c

Browse files
committed
feat: added parse method & test cases
1 parent b8d6e68 commit b7c961c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Cache/LRUCache.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ class LRUCache {
8787
return null
8888
}
8989

90+
parse (json) {
91+
const { misses, hits, cache } = JSON.parse(json)
92+
93+
this.misses += misses ?? 0
94+
this.hits += hits ?? 0
95+
96+
for (const key in cache) {
97+
this.set(key, cache[key])
98+
}
99+
100+
return this
101+
}
102+
90103
toString (indent) {
91104
const replacer = (_, value) => {
92105
if (value instanceof Set) {

Cache/test/LRUCache.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ describe('Testing LRUCache', () => {
4141
size: 2
4242
})
4343

44+
const json = '{"misses":3,"hits":6,"cache":{"3":3,"4":4}}'
45+
expect(cache.toString()).toBe(json)
46+
47+
// merge with json
48+
cache.parse(json)
49+
4450
cache.capacity-- // now the capacity decreasing by one
4551

4652
expect(cache.info).toEqual({
47-
misses: 3,
48-
hits: 6,
53+
misses: 6,
54+
hits: 12,
4955
capacity: 1,
5056
size: 1
5157
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy