Comparing version 2.0.5 to 2.0.6
161
boxcrate.js
@@ -83,4 +83,2 @@ function _typeof(obj) { | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {string} | ||
@@ -94,4 +92,2 @@ * | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
@@ -122,6 +118,2 @@ * | ||
* BoxCrate works with data as it is without you having to worry about stringifying it to save it. | ||
* | ||
* @author Robert Corponoi <robertcorponoi@gmail.com> | ||
* | ||
* @version 2.0.0 | ||
*/ | ||
@@ -134,3 +126,3 @@ var BoxCrate = | ||
* | ||
* @since 2.0.0 | ||
* @private | ||
* | ||
@@ -143,3 +135,3 @@ * @property {Options} | ||
* | ||
* @since 2.0.0 | ||
* @private | ||
* | ||
@@ -152,4 +144,2 @@ * @property {number} | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
@@ -161,4 +151,2 @@ */ | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
@@ -170,4 +158,2 @@ */ | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @property {Storage} | ||
@@ -179,4 +165,2 @@ */ | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @property {number} | ||
@@ -193,27 +177,22 @@ */ | ||
_defineProperty(this, "options", void 0); | ||
_defineProperty(this, "_options", void 0); | ||
_defineProperty(this, "timer", 0); | ||
_defineProperty(this, "_timer", 0); | ||
_defineProperty(this, "previousCheckTime", 0); | ||
_defineProperty(this, "_previousCheckTime", 0); | ||
_defineProperty(this, "currentCheckTime", 0); | ||
_defineProperty(this, "_currentCheckTime", 0); | ||
_defineProperty(this, "storage", window.localStorage); | ||
_defineProperty(this, "_storage", window.localStorage); | ||
_defineProperty(this, "count", 0); | ||
_defineProperty(this, "_count", 0); | ||
this.options = new Options(options); | ||
this.boot(); | ||
this._options = new Options(options); | ||
this._boot(); | ||
} | ||
/** | ||
* Saves an item. | ||
* Returns the storage oboject. | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The unique id of this item used to modify or retrieve it. | ||
* @param {*} value The data to save. | ||
* @param {number} [msToExpire=Infinity] The amount of time, in milliseconds, until this item is considered expired. | ||
* | ||
* @returns {BoxCrate} Returns this for chaining. | ||
* @returns {Storage} | ||
*/ | ||
@@ -224,2 +203,12 @@ | ||
key: "setItem", | ||
/** | ||
* Saves an item. | ||
* | ||
* @param {string} id The unique id of this item used to modify or retrieve it. | ||
* @param {*} value The data to save. | ||
* @param {number} [msToExpire=Infinity] The amount of time, in milliseconds, until this item is considered expired. | ||
* | ||
* @returns {BoxCrate} Returns this for chaining. | ||
*/ | ||
value: function setItem(id, value) { | ||
@@ -294,4 +283,6 @@ var msToExpire = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Infinity; | ||
item.expires = msToExpire; | ||
this.storage.setItem(id, JSON.stringify(item)); | ||
this.count++; | ||
this._storage.setItem(id, JSON.stringify(item)); | ||
this._count++; | ||
return this; | ||
@@ -302,4 +293,2 @@ } | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The id of the item to retrieve from storage. | ||
@@ -313,7 +302,7 @@ * | ||
value: function getItem(id) { | ||
if (this.storage.length === 0) return; | ||
var item = JSON.parse(this.storage.getItem(id)); | ||
if (this._storage.length === 0) return; | ||
var item = JSON.parse(this._storage.getItem(id)); | ||
if (this.options.expiredCheckType === 'passive' && item.expires) { | ||
if (this.itemIsExpired(item)) { | ||
if (this._options.expiredCheckType === 'passive' && item.expires) { | ||
if (this._itemIsExpired(item)) { | ||
this.removeItem(id); | ||
@@ -324,3 +313,3 @@ return; | ||
return this.parseItem(item.type, item.data); | ||
return this._parseItem(item.type, item.data); | ||
} | ||
@@ -330,4 +319,2 @@ /** | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The id of the item to remove from storage. | ||
@@ -341,4 +328,5 @@ * | ||
value: function removeItem(id) { | ||
this.storage.removeItem(id); | ||
this.count--; | ||
this._storage.removeItem(id); | ||
this._count--; | ||
return this; | ||
@@ -349,4 +337,2 @@ } | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @returns {BoxCrate} Returns this for chaining. | ||
@@ -358,4 +344,5 @@ */ | ||
value: function clear() { | ||
this.storage.clear(); | ||
this.count = 0; | ||
this._storage.clear(); | ||
this._count = 0; | ||
return this; | ||
@@ -366,4 +353,2 @@ } | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -378,4 +363,4 @@ * | ||
}, { | ||
key: "parseItem", | ||
value: function parseItem(type, data) { | ||
key: "_parseItem", | ||
value: function _parseItem(type, data) { | ||
switch (type) { | ||
@@ -404,3 +389,3 @@ case 'string': | ||
var item = _step2.value; | ||
original.push(this.convertString(item)); | ||
original.push(this._convertString(item)); | ||
} | ||
@@ -432,4 +417,2 @@ } catch (err) { | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -443,4 +426,4 @@ * | ||
}, { | ||
key: "convertString", | ||
value: function convertString(value) { | ||
key: "_convertString", | ||
value: function _convertString(value) { | ||
switch (value) { | ||
@@ -470,4 +453,2 @@ case 'true': | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -481,4 +462,4 @@ * | ||
}, { | ||
key: "itemIsExpired", | ||
value: function itemIsExpired(item) { | ||
key: "_itemIsExpired", | ||
value: function _itemIsExpired(item) { | ||
if (window.performance.now() - item.timestamp >= item.expires) return true; | ||
@@ -490,4 +471,2 @@ return false; | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @private | ||
@@ -497,19 +476,19 @@ */ | ||
}, { | ||
key: "checkForExpiredItems", | ||
value: function checkForExpiredItems() { | ||
key: "_checkForExpiredItems", | ||
value: function _checkForExpiredItems() { | ||
var _this = this; | ||
this.currentCheckTime = window.performance.now(); | ||
this._currentCheckTime = window.performance.now(); | ||
if (this.currentCheckTime - this.previousCheckTime >= this.options.expiredCheckInterval) { | ||
for (var key in this.storage) { | ||
if (this.storage.hasOwnProperty(key) && this.itemIsExpired(JSON.parse(this.storage[key]))) this.removeItem(key); | ||
if (this._currentCheckTime - this._previousCheckTime >= this._options.expiredCheckInterval) { | ||
for (var key in this._storage) { | ||
if (this._storage.hasOwnProperty(key) && this._itemIsExpired(JSON.parse(this._storage[key]))) this.removeItem(key); | ||
} | ||
this.previousCheckTime = this.currentCheckTime; | ||
this._previousCheckTime = this._currentCheckTime; | ||
} | ||
this.timer = window.setTimeout(function () { | ||
_this.checkForExpiredItems(); | ||
}, this.options.expiredCheckInterval); | ||
this._timer = window.setTimeout(function () { | ||
_this._checkForExpiredItems(); | ||
}, this._options.expiredCheckInterval); | ||
} | ||
@@ -519,4 +498,2 @@ /** | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @private | ||
@@ -526,12 +503,28 @@ */ | ||
}, { | ||
key: "boot", | ||
value: function boot() { | ||
key: "_boot", | ||
value: function _boot() { | ||
var _this2 = this; | ||
if (this.options.expiredCheckType === 'active') { | ||
this.timer = window.setTimeout(function () { | ||
_this2.checkForExpiredItems(); | ||
}, this.options.expiredCheckInterval); | ||
if (this._options.expiredCheckType === 'active') { | ||
this._timer = window.setTimeout(function () { | ||
_this2._checkForExpiredItems(); | ||
}, this._options.expiredCheckInterval); | ||
} | ||
} | ||
}, { | ||
key: "storage", | ||
get: function get() { | ||
return this._storage; | ||
} | ||
/** | ||
* Returns the amount of items in storage. | ||
* | ||
* @returns {number} | ||
*/ | ||
}, { | ||
key: "count", | ||
get: function get() { | ||
return this._count; | ||
} | ||
}]); | ||
@@ -538,0 +531,0 @@ |
@@ -0,4 +1,14 @@ | ||
2.0.6 / 2019-12-13 | ||
================== | ||
* [MISC] Updated dependencies to their latest versions. | ||
* [MISC] Added examples to properties in README. | ||
* [MISC] Added more badges to README. | ||
* [MISC] Added tests section to README. | ||
* [MISC] Removed unnecessary jsdoc comments. | ||
* [MISC] Normalized private properties and methods by adding a _ to all of them. | ||
* [FEATURE] Made properties private and added the necessary getters/setters. | ||
2.0.4 / 2019-11-03 | ||
================== | ||
* [FEATURE] Updated dependencies to their latest versions. | ||
* [MISC] Updated dependencies to their latest versions. | ||
* [MISC] Changed CHANGELOG to be a markdown file. | ||
@@ -5,0 +15,0 @@ * [MISC] Changed rollup bundle from 'es' to 'esm'. |
{ | ||
"name": "boxcrate", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "A smart wrapper for the browser's localStorage that allows you to set and get items as they are with optional expiration times.", | ||
@@ -8,11 +8,11 @@ "main": "boxcrate.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.6.4", | ||
"@babel/core": "^7.6.4", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.6.2", | ||
"@babel/preset-env": "^7.6.3", | ||
"@babel/preset-typescript": "^7.6.0", | ||
"@babel/cli": "^7.7.5", | ||
"@babel/core": "^7.7.5", | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", | ||
"@babel/preset-env": "^7.7.6", | ||
"@babel/preset-typescript": "^7.7.4", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.2", | ||
"rollup": "^1.26.3", | ||
"rollup": "^1.27.12", | ||
"rollup-plugin-babel": "^4.3.3", | ||
@@ -22,3 +22,3 @@ "rollup-plugin-commonjs": "^10.1.0", | ||
"sinon": "^7.5.0", | ||
"typescript": "^3.6.4" | ||
"typescript": "^3.7.3" | ||
}, | ||
@@ -25,0 +25,0 @@ "types": "lib/index.d.ts", |
@@ -10,6 +10,11 @@ <p align="center"> | ||
<div align="center"> | ||
<a href="https://badge.fury.io/js/boxcrate"><img src="https://badge.fury.io/js/boxcrate.svg" alt="npm version" height="18"></a> | ||
<a href="https://badge.fury.io/js/boxcrate"><img src="https://img.shields.io/badge/build-passing-brightgreen.svg" alt="build" height="18"></a> | ||
[](https://www.npmjs.com/package/boxcrate) | ||
[](https://snyk.io/test/github/robertcorponoi/boxcrate) | ||
 | ||
[](https://www.npmjs.com/package/boxcrate) | ||
<a href="https://badge.fury.io/js/boxcrate"><img src="https://img.shields.io/github/issues/robertcorponoi/boxcrate.svg" alt="issues" height="18"></a> | ||
<a href="https://badge.fury.io/js/boxcrate"><img src="https://img.shields.io/github/license/robertcorponoi/boxcrate.svg" alt="license" height="18"></a> | ||
[](https://gitter.im/robertcorponoi) | ||
</div> | ||
@@ -69,2 +74,12 @@ | ||
### **storage** | ||
Returns a reference to the storage. Note, this should not be modified as it will affect the original storage also. | ||
**example:** | ||
```js | ||
const storage = boxcrate.storage; | ||
``` | ||
### **count** | ||
@@ -74,2 +89,8 @@ | ||
**example:** | ||
```js | ||
const numOfItems = boxcrate.count; | ||
``` | ||
### **setItem** | ||
@@ -89,2 +110,4 @@ | ||
**example:** | ||
```js | ||
@@ -104,2 +127,4 @@ const pizzaToppings = ['Cheese', 'Pepperoni', 'Spinach']; | ||
**example:** | ||
```js | ||
@@ -120,2 +145,4 @@ const toppings = boxcrate.getItem('toppings'); | ||
**example:** | ||
```js | ||
@@ -129,2 +156,4 @@ boxcrate.removeItem('toppings'); | ||
**example:** | ||
```js | ||
@@ -134,4 +163,14 @@ boxcrate.clear(); | ||
## **Tests** | ||
Since BoxCrate's tests are run in the browser, you have to run: | ||
```bash | ||
$ npm run test | ||
``` | ||
and then in your browser, go to `http://localhost:8888/test/index.html` to run the test suite. | ||
## **License** | ||
MIT |
125
src/index.ts
@@ -12,6 +12,2 @@ 'use strict' | ||
* BoxCrate works with data as it is without you having to worry about stringifying it to save it. | ||
* | ||
* @author Robert Corponoi <robertcorponoi@gmail.com> | ||
* | ||
* @version 2.0.0 | ||
*/ | ||
@@ -23,7 +19,7 @@ export default class BoxCrate { | ||
* | ||
* @since 2.0.0 | ||
* @private | ||
* | ||
* @property {Options} | ||
*/ | ||
options: Options; | ||
private _options: Options; | ||
@@ -33,7 +29,7 @@ /** | ||
* | ||
* @since 2.0.0 | ||
* @private | ||
* | ||
* @property {number} | ||
*/ | ||
timer: number = 0; | ||
private _timer: number = 0; | ||
@@ -43,7 +39,5 @@ /** | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
*/ | ||
previousCheckTime: number = 0; | ||
private _previousCheckTime: number = 0; | ||
@@ -53,7 +47,5 @@ /** | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
*/ | ||
currentCheckTime: number = 0; | ||
private _currentCheckTime: number = 0; | ||
@@ -63,7 +55,5 @@ /** | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @property {Storage} | ||
*/ | ||
storage: Storage = window.localStorage; | ||
private _storage: Storage = window.localStorage; | ||
@@ -73,7 +63,5 @@ /** | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @property {number} | ||
*/ | ||
count: number = 0; | ||
private _count: number = 0; | ||
@@ -87,5 +75,5 @@ /** | ||
this.options = new Options(options); | ||
this._options = new Options(options); | ||
this.boot(); | ||
this._boot(); | ||
@@ -95,6 +83,18 @@ } | ||
/** | ||
* Returns the storage oboject. | ||
* | ||
* @returns {Storage} | ||
*/ | ||
get storage(): Storage { return this._storage; } | ||
/** | ||
* Returns the amount of items in storage. | ||
* | ||
* @returns {number} | ||
*/ | ||
get count(): number { return this._count; } | ||
/** | ||
* Saves an item. | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The unique id of this item used to modify or retrieve it. | ||
@@ -159,5 +159,5 @@ * @param {*} value The data to save. | ||
this.storage.setItem(id, JSON.stringify(item)); | ||
this._storage.setItem(id, JSON.stringify(item)); | ||
this.count++; | ||
this._count++; | ||
@@ -171,4 +171,2 @@ return this; | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The id of the item to retrieve from storage. | ||
@@ -180,9 +178,9 @@ * | ||
if (this.storage.length === 0) return; | ||
if (this._storage.length === 0) return; | ||
const item: Item = JSON.parse(this.storage.getItem(id)!); | ||
const item: Item = JSON.parse(this._storage.getItem(id)!); | ||
if (this.options.expiredCheckType === 'passive' && item.expires) { | ||
if (this._options.expiredCheckType === 'passive' && item.expires) { | ||
if (this.itemIsExpired(item)) { | ||
if (this._itemIsExpired(item)) { | ||
@@ -197,3 +195,3 @@ this.removeItem(id); | ||
return this.parseItem(item.type, item.data); | ||
return this._parseItem(item.type, item.data); | ||
@@ -205,4 +203,2 @@ } | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {string} id The id of the item to remove from storage. | ||
@@ -214,5 +210,5 @@ * | ||
this.storage.removeItem(id); | ||
this._storage.removeItem(id); | ||
this.count--; | ||
this._count--; | ||
@@ -226,4 +222,2 @@ return this; | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @returns {BoxCrate} Returns this for chaining. | ||
@@ -233,5 +227,5 @@ */ | ||
this.storage.clear(); | ||
this._storage.clear(); | ||
this.count = 0; | ||
this._count = 0; | ||
@@ -245,4 +239,2 @@ return this; | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -255,3 +247,3 @@ * | ||
*/ | ||
private parseItem(type: string, data: any): any { | ||
private _parseItem(type: string, data: any): any { | ||
@@ -275,3 +267,3 @@ switch (type) { | ||
for (const item of saved) original.push(this.convertString(item)); | ||
for (const item of saved) original.push(this._convertString(item)); | ||
@@ -290,4 +282,2 @@ return original; | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -299,3 +289,3 @@ * | ||
*/ | ||
private convertString(value: string): any { | ||
private _convertString(value: string): any { | ||
@@ -330,4 +320,2 @@ switch (value) { | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @private | ||
@@ -339,3 +327,3 @@ * | ||
*/ | ||
private itemIsExpired(item: Item): boolean { | ||
private _itemIsExpired(item: Item): boolean { | ||
@@ -351,27 +339,25 @@ if (window.performance.now() - item.timestamp >= item.expires) return true; | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @private | ||
*/ | ||
private checkForExpiredItems() { | ||
private _checkForExpiredItems() { | ||
this.currentCheckTime = window.performance.now(); | ||
this._currentCheckTime = window.performance.now(); | ||
if (this.currentCheckTime - this.previousCheckTime >= this.options.expiredCheckInterval) { | ||
if (this._currentCheckTime - this._previousCheckTime >= this._options.expiredCheckInterval) { | ||
for (const key in this.storage) { | ||
for (const key in this._storage) { | ||
if (this.storage.hasOwnProperty(key) && this.itemIsExpired(JSON.parse(this.storage[key]))) this.removeItem(key); | ||
if (this._storage.hasOwnProperty(key) && this._itemIsExpired(JSON.parse(this._storage[key]))) this.removeItem(key); | ||
} | ||
this.previousCheckTime = this.currentCheckTime; | ||
this._previousCheckTime = this._currentCheckTime; | ||
} | ||
this.timer = window.setTimeout(() => { | ||
this._timer = window.setTimeout(() => { | ||
this.checkForExpiredItems(); | ||
this._checkForExpiredItems(); | ||
}, this.options.expiredCheckInterval); | ||
}, this._options.expiredCheckInterval); | ||
@@ -383,17 +369,12 @@ } | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @private | ||
*/ | ||
private boot() { | ||
private _boot() { | ||
if (this.options.expiredCheckType === 'active') { | ||
if (this._options.expiredCheckType === 'active') { | ||
this.timer = window.setTimeout(() => { | ||
this._timer = window.setTimeout(() => { | ||
this._checkForExpiredItems(); | ||
}, this._options.expiredCheckInterval); | ||
this.checkForExpiredItems(); | ||
}, this.options.expiredCheckInterval); | ||
} | ||
@@ -400,0 +381,0 @@ |
@@ -5,19 +5,8 @@ 'use strict' | ||
* Defines the structure for an item saved to the storage. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
export default interface Item { | ||
// The primitive or complex data type of the item. | ||
type: string; | ||
// The timestamp of the when this item was saved. | ||
timestamp: number; | ||
// The timestamp of when this item expires. | ||
expires: number; | ||
// The data associated with this item. | ||
data: any; | ||
} |
@@ -5,4 +5,2 @@ 'use strict' | ||
* Defines the options available and their defaults for BoxCrate. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
@@ -21,4 +19,2 @@ export default class Options { | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {string} | ||
@@ -33,4 +29,2 @@ * | ||
* | ||
* @since 2.0.0 | ||
* | ||
* @property {number} | ||
@@ -37,0 +31,0 @@ * |
100064
168
1658