-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from svipben/execute-cb-after-clear
reject Promise or execute callback after `clear()`
- Loading branch information
Showing
13 changed files
with
564 additions
and
482 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 |
---|---|---|
|
@@ -2,3 +2,8 @@ language: node_js | |
node_js: | ||
- "node" | ||
- "lts/*" | ||
script: | ||
- "yarn run lint" | ||
- "yarn test" | ||
notifications: | ||
email: false |
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
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,161 +1 @@ | ||
const cache = new Map(); | ||
const timeouts = []; | ||
|
||
/** | ||
* Returns value from cache by given `key`. | ||
* | ||
* @param {string | number | boolean} key | ||
* | ||
* @returns {any} value from cache | ||
*/ | ||
function get(key) { | ||
return cache.get(key); | ||
} | ||
|
||
/** | ||
* Removes element from cache by given `key`. | ||
* | ||
* @param {string | number | boolean} key | ||
* | ||
* @returns {boolean} true if element has been removed, false otherwise | ||
*/ | ||
function remove(key) { | ||
return cache.delete(key); | ||
} | ||
|
||
/** | ||
* Checks if whether an element with given `key` exist. | ||
* | ||
* @param {string | number | boolean} key | ||
* | ||
* @returns {boolean} true if element exist, false otherwise | ||
*/ | ||
function has(key) { | ||
return cache.has(key); | ||
} | ||
|
||
/** | ||
* Returns the number of elements in cache. | ||
* | ||
* @returns {number} number of elements in cache | ||
*/ | ||
function size() { | ||
return cache.size; | ||
} | ||
|
||
/** | ||
* Returns all keys stored in cache. | ||
* | ||
* @returns {Array<string | number | boolean>} all keys | ||
*/ | ||
function keys() { | ||
return [...cache.keys()]; | ||
} | ||
|
||
/** | ||
* Returns all values stored in cache. | ||
* | ||
* @returns {Array<any>} all values | ||
*/ | ||
function values() { | ||
return [...cache.values()]; | ||
} | ||
|
||
/** | ||
* Returns all entries (keys and values) stored in cache. | ||
* | ||
* @returns {Array<[string | number | boolean, any]>} all entries (keys and values) | ||
*/ | ||
function entries() { | ||
return [...cache.entries()]; | ||
} | ||
|
||
/** | ||
* Removes all elements stored in cache and clears all timeouts. | ||
* | ||
* @returns {number} how much elements was removed from cache | ||
*/ | ||
function clear() { | ||
timeouts.forEach(t => clearTimeout(t)); | ||
const length = size(); | ||
cache.clear(); | ||
return length; | ||
} | ||
|
||
/** | ||
* Stores an element in-memory with specified key and value. | ||
* | ||
* If only `key` and `value` is provided it returns boolean. | ||
* true: if element was stored and key doesn't exist. | ||
* false: if key does exist. | ||
* | ||
* If additionally only `timeout` is provided it returns Promise. | ||
* Element will be deleted after given `timeout` and Promise will be resolved. | ||
* Otherwise it will be rejected if element does not exist. | ||
* | ||
* If additionally `timeout` and `callback` is provided it executes given `callback` after given `timeout`. | ||
* | ||
* @param {string | number | boolean} key can be only: `string` | `number` | `boolean` | ||
* @param {any} value can be any type | ||
* @param {number} timeout after how much time in milliseconds element will be deleted | ||
* @param {(err: Error, key: string | number | boolean, value: any, timeout: number)} callback will be executed after given `timeout` | ||
* | ||
* @returns {boolean | Promise<{ key: string | number | boolean, value: any, timeout: number }>} boolean or Promise | ||
*/ | ||
function put(key, value, timeout, callback) { | ||
// key type is incorrect | ||
if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'boolean') { | ||
throw new TypeError(`key can be only: string | number | boolean instead of ${typeof key}`); | ||
} | ||
// check if key is not NaN | ||
if (typeof key === 'number' && isNaN(key)) { | ||
throw new TypeError('key can be only: string | number | boolean instead of NaN'); | ||
} | ||
|
||
// timeout type is incorrect and/or timeout is not positive number | ||
if (timeout !== undefined && (typeof timeout !== 'number' || isNaN(timeout) || timeout <= 0)) { | ||
throw new TypeError('timeout should be positive number'); | ||
} | ||
|
||
// callback type is incorrect | ||
if (callback !== undefined && typeof callback !== 'function') { | ||
throw new TypeError(`callback should be function instead of ${typeof callback}`); | ||
} | ||
|
||
// key does exist | ||
if (has(key)) { | ||
return false; | ||
} | ||
|
||
cache.set(key, value); | ||
|
||
// return Promise | ||
if (timeout !== undefined && callback === undefined) { | ||
return new Promise((resolve, reject) => { | ||
const t = setTimeout(() => { | ||
if (cache.delete(key)) { | ||
resolve({ key, value, timeout }); | ||
} else { | ||
reject(new Error(`${key} does not exist`)); | ||
} | ||
}, timeout); | ||
timeouts.push(t); | ||
}); | ||
} | ||
|
||
// execute callback | ||
if (timeout !== undefined && callback !== undefined) { | ||
const t = setTimeout(() => { | ||
if (cache.delete(key)) { | ||
callback(null, key, value, timeout); | ||
} else { | ||
callback(new Error(`${key} does not exist`)); | ||
} | ||
}, timeout); | ||
timeouts.push(t); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { get, remove, has, size, keys, values, entries, clear, put }; | ||
module.exports = require('./src/cachimo'); |
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.