2014-09-01 23:44:35 -03:00
|
|
|
'use strict';
|
2014-10-29 16:21:44 -03:00
|
|
|
var _ = require('lodash');
|
2014-12-02 11:55:29 -03:00
|
|
|
var preconditions = require('preconditions').singleton();
|
2014-09-01 23:44:35 -03:00
|
|
|
|
|
|
|
|
function LocalStorage() {
|
2014-09-30 15:28:02 -03:00
|
|
|
this.type = 'DB';
|
2014-12-02 11:55:29 -03:00
|
|
|
|
|
|
|
|
preconditions.checkState(typeof localStorage !== 'undefined',
|
|
|
|
|
'localstorage not available, cannot run plugin');
|
2014-09-01 23:44:35 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LocalStorage.prototype.init = function() {
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-24 12:24:44 -03:00
|
|
|
LocalStorage.prototype.setCredentials = function(email, password, opts) {
|
2014-10-27 10:36:17 -03:00
|
|
|
this.email = email;
|
|
|
|
|
this.password = password;
|
2014-10-24 12:24:44 -03:00
|
|
|
};
|
|
|
|
|
|
2014-09-03 01:25:08 -03:00
|
|
|
LocalStorage.prototype.getItem = function(k,cb) {
|
2014-10-27 11:57:13 -03:00
|
|
|
return cb(null, localStorage.getItem(k));
|
2014-09-01 23:44:35 -03:00
|
|
|
};
|
|
|
|
|
|
2014-10-28 15:20:43 -03:00
|
|
|
/**
|
|
|
|
|
* Same as setItem, but fails if an item already exists
|
|
|
|
|
*/
|
|
|
|
|
LocalStorage.prototype.createItem = function(name, value, callback) {
|
|
|
|
|
if (localStorage.getItem(name)) {
|
|
|
|
|
return callback('EEXISTS');
|
|
|
|
|
}
|
|
|
|
|
return this.setItem(name, value, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-03 01:25:08 -03:00
|
|
|
LocalStorage.prototype.setItem = function(k,v,cb) {
|
2014-09-01 23:44:35 -03:00
|
|
|
localStorage.setItem(k,v);
|
2014-09-03 01:25:08 -03:00
|
|
|
return cb();
|
2014-09-01 23:44:35 -03:00
|
|
|
};
|
|
|
|
|
|
2014-09-03 01:25:08 -03:00
|
|
|
LocalStorage.prototype.removeItem = function(k,cb) {
|
2014-09-01 23:44:35 -03:00
|
|
|
localStorage.removeItem(k);
|
2014-09-03 01:25:08 -03:00
|
|
|
return cb();
|
2014-09-01 23:44:35 -03:00
|
|
|
};
|
|
|
|
|
|
2014-09-03 01:25:08 -03:00
|
|
|
LocalStorage.prototype.clear = function(cb) {
|
2014-09-01 23:44:35 -03:00
|
|
|
localStorage.clear();
|
2014-09-03 01:25:08 -03:00
|
|
|
return cb();
|
2014-09-01 23:44:35 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = LocalStorage;
|