-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update to latest jest, add test, update readme
- Loading branch information
Jacek Jan Pietal
committed
Apr 10, 2021
1 parent
3019639
commit 86d7ce9
Showing
6 changed files
with
1,982 additions
and
1,840 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
// Lazy Cache based on Map | ||
module.exports = class LaterMom { | ||
// factory for creating entries | ||
constructor(factory) { | ||
this.factory = factory | ||
this.data = new Map() | ||
} | ||
// factory for creating entries | ||
constructor(factory) { | ||
this.factory = factory; | ||
this.data = new Map(); | ||
} | ||
|
||
// creates a string from args | ||
getKey(...args) { | ||
return args.length > 1 ? args.join(',') : args[0] | ||
} | ||
// creates a string from args | ||
getKey(...args) { | ||
return args.length > 1 ? args.join(",") : args[0]; | ||
} | ||
|
||
// has entry at [args] | ||
has(...args) { | ||
const key = this.getKey(...args) | ||
return this.data.has(key) | ||
} | ||
|
||
// get entry at [args] | ||
get(...args) { | ||
const key = this.getKey(...args) | ||
let val = this.data.get(key) | ||
if (!val) { | ||
val = this.factory(...args) | ||
this.data.set(key, val) | ||
} | ||
return val | ||
} | ||
// has entry at [args] | ||
has(...args) { | ||
const key = this.getKey(...args); | ||
return this.data.has(key); | ||
} | ||
|
||
// perform callback on each entry | ||
each(callback) { | ||
return this.data.forEach(callback) | ||
// get entry at [args] | ||
get(...args) { | ||
const key = this.getKey(...args); | ||
let val = this.data.get(key); | ||
if (!val) { | ||
val = this.factory(...args); | ||
this.data.set(key, val); | ||
} | ||
return val; | ||
} | ||
|
||
// delete entry at [args] | ||
del(...args) { | ||
return this.data.delete(this.getKey(...args)) | ||
} | ||
} | ||
// perform callback on each entry | ||
each(callback) { | ||
return this.data.forEach(callback); | ||
} | ||
|
||
// delete entry at [args] | ||
del(...args) { | ||
return this.data.delete(this.getKey(...args)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
const LazyMap = require('.') | ||
const Cache = require("."); | ||
|
||
test('Calls factory once when asked for same key many times', () => { | ||
const hashCache = new LazyMap(n => n.toString(36).split('.').pop()) | ||
const createHashSpy = jest.spyOn(hashCache, 'factory') | ||
test("Calls factory once when asked for same key many times", () => { | ||
const hashCache = new Cache((n) => n.toString(36).split(".").pop()); | ||
const createHashSpy = jest.spyOn(hashCache, "factory"); | ||
|
||
new Array(1024).fill(() => expect(hashCache.get(123)).toBeTruthy()).map(fn => fn()) | ||
new Array(1024) | ||
.fill(() => expect(hashCache.get(123)).toBeTruthy()) | ||
.map((fn) => fn()); | ||
|
||
expect(createHashSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
expect(createHashSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('Getter is able to handle multiple arguments', () => { | ||
const positionCache = new LazyMap((x, y, z) => `${x}__${y}__${z}`) | ||
test("Getter is able to handle multiple arguments", () => { | ||
const positionCache = new Cache((x, y, z) => `${x}__${y}__${z}`); | ||
|
||
expect(positionCache.get(13, 3, 7)).toBe('13__3__7') | ||
expect(positionCache.data.get('13,3,7')).toBe('13__3__7') | ||
}) | ||
expect(positionCache.get(13, 3, 7)).toBe("13__3__7"); | ||
expect(positionCache.data.get("13,3,7")).toBe("13__3__7"); | ||
}); | ||
|
||
test('Readme has working code', () => { | ||
const Mom = require('latermom') | ||
test("Readme has working code", () => { | ||
// create cache with factory function (may take any number of parameters) | ||
const cache = new Cache(() => Math.random()); | ||
|
||
// create Mom, with factory function that takes any number of parameters | ||
const mom = new Mom(_ => Math.random()) | ||
// each time you call the cache.get with same parameters | ||
// you will get the same once lazy cached answer | ||
const a = cache.get(1, 2, 3); | ||
const b = cache.get(1, 2, 3); | ||
const c = cache.get(1, 2); | ||
const d = cache.get(1, 2, 4); | ||
|
||
const a = mom.get(1, 2, 3) | ||
|
||
const b = mom.get(1, 2, 3) | ||
|
||
expect(a).toBe(b) | ||
}) | ||
expect(a).toEqual(b); // true | ||
expect(a).not.toEqual(c); // true | ||
expect(a).not.toEqual(d); // true | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.