-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (50 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*global window*/
/**
* Module dependencies.
*/
var assert = require('assert');
var debug = require('debug')('simple-local');
/**
* Get the current value.
*
* @param {String} key
* @return {Any}
* @api public
*/
exports.get = function(key) {
assert(window.localStorage, 'simple-local: window.localStorage should exist');
assert.equal(typeof key, 'string', 'simple-local: namespace should be a string');
var val = _get(key);
debug('get ', val);
return val;
};
/**
* Update the current val.
*
* @param {String} key
* @param {Any} value
* @api public
*/
exports.set = function(key, val) {
assert(window.localStorage, 'simple-local: window.localStorage should exist');
assert.equal(typeof key, 'string', 'simple-local: key should be a string');
assert.notEqual(typeof val, 'undefined', 'simple-local: val should exist');
var oldVal = _get(key);
var newVal = JSON.stringify(val)
debug('set ', newVal, oldVal);
window.localStorage[key] = newVal;
};
/**
* Get an object from localStorage
*
* @param {String} key
* @return {Any}
* @api private
*/
function _get(key) {
var val = window.localStorage[key];
val = val.length
? JSON.parse(val)
: undefined;
return val;
}