showcar-storage
Version:
This module provides an abstraction layer for storing information on the client-side.
169 lines (135 loc) • 4.64 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>storage.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Storage.html">Storage</a><ul class='methods'><li data-type='method'><a href="Storage.html#get">get</a></li><li data-type='method'><a href="Storage.html#has">has</a></li><li data-type='method'><a href="Storage.html#remove">remove</a></li><li data-type='method'><a href="Storage.html#set">set</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">storage.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
const stores = {
local: require('./stores/local.js'),
session: require('./stores/session.js'),
cookie: require('./stores/cookie.js')
};
class Storage {
/**
* @constructor
* @param {string} type The store backend to use
* @param {boolean} [silent=true] Whether to throw exceptions or fail silently returning false
*/
constructor (type, { silent = true } = {}) {
if (!(type in stores)) {
this.fail(`Storage: Unsupported type ${type}`);
}
this.silent = !!silent;
this.store = new (stores[type])();
}
/**
* Gets the stored value for a specified key
* @param {string} key The key to look up
* @param defaultValue Return this if no value has been found
* @throws {Error} If not silent
* @returns {string} The stored value or defaultValue
*/
get (key, defaultValue = null) {
try {
const result = this.store.get(key);
if (null === result) {
return defaultValue;
}
return result;
} catch (e) {
return this.fail(e);
}
}
/**
* Writes a value to the store under the specified key
* @param {string} key The key to use when storing
* @param {string} value The value to store
* @param {object} [options] A map of options. See the respective backends.
* @throws {Error} If not silent
* @returns {Storage|boolean} If silent, returns false on error. Returns this on success.
*/
set (key, value, options) {
try {
this.store.set(key, value, options);
return this;
} catch (e) {
return this.fail(e);
}
}
/**
* Checks whether the store knows about the specified key
* @param {string} key The key to check for existance
* @throws {Error} If not silent
* @returns {boolean} If silent, returns false on error (!!)
*/
has (key) {
try {
return this.store.has(key);
} catch (e) {
return this.fail(e);
}
}
/**
* Deletes the specified key and its value from the store
* @param {string} key The key to delete
* @returns {Storage|boolean} If silent, returns false on error
*/
remove (key) {
try {
this.store.remove(key);
return this;
} catch (e) {
return this.fail(e);
}
}
/**
* Wrapper for error handling
* @private
* @param {Error|string} reason What is happening?
* @returns {boolean}
*/
fail (reason) {
if (this.silent) {
return false;
}
if (!reason instanceof Error) {
reason = new Error(reason);
}
throw reason;
}
}
module.exports = Storage;
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 13 2016 17:38:26 GMT+0200 (W. Europe Daylight Time) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>