Skip to content

Commit

Permalink
feat: update to latest jest, add test, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Jan Pietal committed Apr 10, 2021
1 parent 3019639 commit 86d7ce9
Show file tree
Hide file tree
Showing 6 changed files with 1,982 additions and 1,840 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/

81 changes: 58 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
## Later, Mom
<h1 align="center">
LaterMom?
</h1>

This is really a 0 dependencies Lazy Map implementation leveraging usage of es6 data structures and lazy caching.
<div align="center">

[![npm version](https://badge.fury.io/js/latermom.svg)](https://badge.fury.io/js/latermom) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Prozi/latermom/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Prozi/latermom/?branch=master) [![Known Vulnerabilities](https://snyk.io/test/github/Prozi/latermom/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Prozi/latermom?targetFile=package.json) [![Maintainability](https://api.codeclimate.com/v1/badges/cf7828e55f51edffbe3d/maintainability)](https://codeclimate.com/github/Prozi/latermom/maintainability)
[![npm version](https://badge.fury.io/js/latermom.svg)](https://badge.fury.io/js/latermom) [![Known Vulnerabilities](https://snyk.io/test/github/Prozi/latermom/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Prozi/latermom?targetFile=package.json) [![Maintainability](https://api.codeclimate.com/v1/badges/cf7828e55f51edffbe3d/maintainability)](https://codeclimate.com/github/Prozi/latermom/maintainability)

</div>

<p align="center">
You know when you were a kid and mom told you to clean your room,<br/>
when you was busy with something else, your answer would always be the same.
</p>

<p align="center">
This library is the universal 0 dependencies lazy cache implementation,<br/>
leveraging usage of es6 data structures.
</p>

## Installation

Expand All @@ -11,40 +25,61 @@ This is really a 0 dependencies Lazy Map implementation leveraging usage of es6
## Usage

```javascript
const Mom = require('latermom')

// create Mom, with factory function that takes any number of parameters
const mom = new Mom(_ => Math.random())

const a = mom.get(1, 2, 3)

const b = mom.get(1, 2, 3)

if (a !== b) {
throw new Error('Something is not ok')
}
// require library
const Cache = require("latermom");

// create cache with factory function (may take any number of parameters)
const cache = new Cache(() => 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);

expect(a).toEqual(b); // true
expect(a).not.toEqual(c); // true
expect(a).not.toEqual(d); // true
```

## API

* constructor(factory: Function) - creates a LaterMom (Lazy Map) instance with factory function for lazy cache
- constructor(factory: Function) - creates a LaterMom (Lazy Map) instance with factory function for lazy cache

methods

* getKey(...args) - creates string key from args
* has(...args) - check if has entry at key created from args
* get(...args) - get entry at key created from args, lazy instantiated by factory
* del(...args) - deletes entry from data at key created from args
* each(callback: Function) - performs callback on each entry in data
- getKey(...args) - creates string key from args
- has(...args) - check if has entry at key created from args
- get(...args) - get entry at key created from args, lazy instantiated by factory
- del(...args) - deletes entry from data at key created from args
- each(callback: Function) - performs callback on each entry in data

properties:

* data: Map
- data: Map

## Tests

```bash
$ jest
PASS ./index.test.js
✓ Calls factory once when asked for same key many times (62 ms)
✓ Getter is able to handle multiple arguments
✓ Readme has working code (1 ms)

Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 0.946 s, estimated 1 s
Ran all test suites.
Done in 1.44s.
```

## License

MIT

## Author

Jacek Pietal @ 2019
Jacek Pietal @ 2019-2021
66 changes: 33 additions & 33 deletions index.js
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));
}
};
49 changes: 27 additions & 22 deletions index.test.js
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
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "latermom",
"version": "1.0.3",
"version": "1.0.4",
"description": "Later Mom is a lazy es6 DataStructures factory also known as cache",
"main": "index.js",
"scripts": {
Expand All @@ -27,6 +27,6 @@
"homepage": "https://github.com/Prozi/latermom#readme",
"dependencies": {},
"devDependencies": {
"jest": "^24.9.0"
"jest": "^26.6.3"
}
}

0 comments on commit 86d7ce9

Please sign in to comment.